22# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
33# @author Simon Heybrock
44
5+ from collections .abc import Sequence
6+
57import numpy as np
68import scipp as sc
79
1012
1113def convert_time_to_datetime64 (
1214 raw_times : sc .Variable ,
13- start : str | None = None ,
14- scaling_factor : float | np .float64 = None ,
15+ start : sc . Variable ,
16+ scaling_factor : float | np .float64 | None = None ,
1517) -> sc .Variable :
1618 """
1719 The nexus standard allows an arbitrary scaling factor to be inserted
@@ -24,14 +26,16 @@ def convert_time_to_datetime64(
2426
2527 See https://manual.nexusformat.org/classes/base_classes/NXlog.html
2628
27- Args:
28- raw_times: The raw time data from a nexus file.
29- start: Optional, the start time of the log in an ISO8601
30- string. If not provided, defaults to the beginning of the
31- unix epoch (1970-01-01T00:00:00).
32- scaling_factor: Optional, the scaling factor between the provided
33- time series data and the unit of the raw_times Variable. If
34- not provided, defaults to 1 (a no-op scaling factor).
29+ Parameters
30+ ----------
31+ raw_times:
32+ The raw time data from a nexus file.
33+ start:
34+ The start time of the log.
35+ scaling_factor:
36+ Optional, the scaling factor between the provided
37+ time series data and the unit of the raw_times Variable. If
38+ not provided, defaults to 1 (a no-op scaling factor).
3539 """
3640 if (
3741 raw_times .dtype in (sc .DType .float64 , sc .DType .float32 )
@@ -53,10 +57,18 @@ def convert_time_to_datetime64(
5357 )
5458
5559
56- def _to_canonical_select (dims : list [str ], select : ScippIndex ) -> dict [str , int | slice ]:
60+ def has_time_unit (obj : sc .Variable ) -> bool :
61+ if (unit := obj .unit ) is None :
62+ return False
63+ return unit .to_dict ().get ('powers' ) == {'s' : 1 }
64+
65+
66+ def to_canonical_select (
67+ dims : Sequence [str ], select : ScippIndex
68+ ) -> dict [str , int | slice ]:
5769 """Return selection as dict with explicit dim labels"""
5870
59- def check_1d ():
71+ def check_1d () -> None :
6072 if len (dims ) != 1 :
6173 raise sc .DimensionError (
6274 f"Dataset has multiple dimensions { dims } , "
@@ -67,8 +79,8 @@ def check_1d():
6779 return {}
6880 if isinstance (select , tuple ) and len (select ) == 0 :
6981 return {}
70- if isinstance (select , tuple ) and isinstance (select [0 ], str ):
71- key , sel = select
82+ if isinstance (select , tuple ) and isinstance (select [0 ], str ): # type: ignore[misc] # incorrect narrowing
83+ key , sel = select # type: ignore[misc] # incorrect narrowing
7284 return {key : sel }
7385 if isinstance (select , tuple ):
7486 check_1d ()
@@ -77,7 +89,7 @@ def check_1d():
7789 f"Dataset has single dimension { dims } , "
7890 "but multiple indices {select} were specified."
7991 )
80- return {dims [0 ]: select [0 ]}
92+ return {dims [0 ]: select [0 ]} # type: ignore[unreachable] # incorrect narrowing
8193 elif isinstance (select , int | sc .Variable ) or isinstance (select , slice ):
8294 check_1d ()
8395 return {dims [0 ]: select }
@@ -86,12 +98,14 @@ def check_1d():
8698 return select .copy ()
8799
88100
89- def to_plain_index (dims : list [str ], select : ScippIndex ) -> int | slice | tuple :
101+ def to_plain_index (
102+ dims : Sequence [str ], select : ScippIndex
103+ ) -> int | slice | tuple [int | slice , ...]:
90104 """
91105 Given a valid "scipp" index 'select', return an equivalent plain numpy-style index.
92106 """
93- select = _to_canonical_select (dims , select )
94- index = [slice (None )] * len (dims )
107+ select = to_canonical_select (dims , select )
108+ index : list [ int | slice ] = [slice (None )] * len (dims )
95109 for key , sel in select .items ():
96110 if key not in dims :
97111 raise sc .DimensionError (
@@ -104,8 +118,8 @@ def to_plain_index(dims: list[str], select: ScippIndex) -> int | slice | tuple:
104118
105119
106120def to_child_select (
107- dims : list [str ],
108- child_dims : list [str ],
121+ dims : Sequence [str ],
122+ child_dims : Sequence [str ],
109123 select : ScippIndex ,
110124 bin_edge_dim : str | None = None ,
111125) -> ScippIndex :
@@ -115,7 +129,7 @@ def to_child_select(
115129
116130 This removes any selections that apply to the parent but not the child.
117131 """
118- select = _to_canonical_select (dims , select )
132+ select = to_canonical_select (dims , select )
119133 for d in dims :
120134 if d not in child_dims and d in select :
121135 del select [d ]
0 commit comments