Skip to content

Commit 3c41ed3

Browse files
committed
More annotations in stagyydata module
1 parent d913560 commit 3c41ed3

File tree

1 file changed

+67
-46
lines changed

1 file changed

+67
-46
lines changed

stagpy/stagyydata.py

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,29 @@
1717

1818
from . import conf, error, parfile, phyvars, stagyyparsers, _helpers, _step
1919
from ._helpers import CachedReadOnlyProperty as crop
20+
from ._step import Step
2021
from .datatypes import Rprof, Tseries, Vart
2122

2223
if typing.TYPE_CHECKING:
23-
from typing import Tuple, List
24-
from pandas import DataFrame
24+
from typing import Tuple, List, Dict, Optional, Union, Sequence, Iterator
25+
from numpy import ndarray
26+
from pandas import DataFrame, Series
27+
StepIndex = Union[int, slice]
28+
29+
30+
@typing.overload
31+
def _as_view_item(obj: Sequence[StepIndex]) -> Sequence[StepIndex]:
32+
...
33+
34+
35+
@typing.overload
36+
def _as_view_item(obj: slice) -> Sequence[slice]:
37+
...
38+
39+
40+
@typing.overload
41+
def _as_view_item(obj: int) -> None:
42+
...
2543

2644

2745
def _as_view_item(obj):
@@ -176,16 +194,16 @@ class _Tseries:
176194
automatically scaled if conf.scaling.dimensional is True.
177195
178196
Attributes:
179-
sdat (:class:`StagyyData`): the StagyyData instance owning the
180-
:class:`_Tseries` instance.
197+
sdat: the :class:`StagyyData` instance owning the :class:`_Tseries`
198+
instance.
181199
"""
182200

183-
def __init__(self, sdat):
201+
def __init__(self, sdat: StagyyData):
184202
self.sdat = sdat
185-
self._cached_extra = {}
203+
self._cached_extra: Dict[str, Tseries] = {}
186204

187205
@crop
188-
def _data(self):
206+
def _data(self) -> Optional[DataFrame]:
189207
timefile = self.sdat.filename('TimeSeries.h5')
190208
data = stagyyparsers.time_series_h5(
191209
timefile, list(phyvars.TIME.keys()))
@@ -199,12 +217,12 @@ def _data(self):
199217
return data
200218

201219
@property
202-
def _tseries(self):
220+
def _tseries(self) -> DataFrame:
203221
if self._data is None:
204222
raise error.MissingDataError(f'No tseries data in {self.sdat}')
205223
return self._data
206224

207-
def __getitem__(self, name):
225+
def __getitem__(self, name: str) -> Tseries:
208226
if name in self._tseries.columns:
209227
series = self._tseries[name].values
210228
time = self.time
@@ -223,15 +241,16 @@ def __getitem__(self, name):
223241
time, _ = self.sdat.scale(time, 's')
224242
return Tseries(series, time, meta)
225243

226-
def tslice(self, name, tstart=None, tend=None):
244+
def tslice(self, name: str, tstart: Optional[float] = None,
245+
tend: Optional[float] = None) -> Tseries:
227246
"""Return a Tseries between specified times.
228247
229248
Args:
230-
name (str): time variable.
231-
tstart (float): starting time. Set to None to start at the
232-
beginning of available data.
233-
tend (float): ending time. Set to None to stop at the end of
249+
name: time variable.
250+
tstart: starting time. Set to None to start at the beginning of
234251
available data.
252+
tend: ending time. Set to None to stop at the end of available
253+
data.
235254
"""
236255
data, time, meta = self[name]
237256
istart = 0
@@ -243,19 +262,19 @@ def tslice(self, name, tstart=None, tend=None):
243262
return Tseries(data[istart:iend], time[istart:iend], meta)
244263

245264
@property
246-
def time(self):
265+
def time(self) -> ndarray:
247266
"""Time vector."""
248267
return self._tseries['t'].values
249268

250269
@property
251-
def isteps(self):
270+
def isteps(self) -> ndarray:
252271
"""Step indices.
253272
254273
This is such that time[istep] is at step isteps[istep].
255274
"""
256275
return self._tseries.index.values
257276

258-
def at_step(self, istep):
277+
def at_step(self, istep: int) -> Series:
259278
"""Time series output for a given step."""
260279
return self._tseries.loc[istep]
261280

@@ -270,16 +289,16 @@ class _RprofsAveraged(_step._Rprofs):
270289
returns time-averaged profiles instead.
271290
272291
Attributes:
273-
steps (:class:`_StepsView`): the object owning the
274-
:class:`_RprofsAveraged` instance
292+
steps: the :class:`_StepsView` owning the :class:`_RprofsAveraged`
293+
instance.
275294
"""
276295

277-
def __init__(self, steps):
296+
def __init__(self, steps: _StepsView):
278297
self.steps = steps.filter(rprofs=True)
279-
self._cached_data = {}
298+
self._cached_data: Dict[str, Rprof] = {}
280299
super().__init__(next(iter(self.steps)))
281300

282-
def __getitem__(self, name):
301+
def __getitem__(self, name: str) -> Rprof:
283302
# the averaging method has two shortcomings:
284303
# - does not take into account time changing geometry;
285304
# - does not take into account time changing timestep.
@@ -322,21 +341,26 @@ class _Steps:
322341
# iterate through steps 0, 3, 5 and the last two
323342
do_something(step)
324343
325-
Args:
326-
sdat (:class:`StagyyData`): the StagyyData instance owning the
327-
:class:`_Steps` instance.
328344
Attributes:
329-
sdat (:class:`StagyyData`): the StagyyData instance owning the
330-
:class:`_Steps` instance.
345+
sdat: the StagyyData instance owning the :class:`_Steps` instance.
331346
"""
332347

333-
def __init__(self, sdat):
348+
def __init__(self, sdat: StagyyData):
334349
self.sdat = sdat
335-
self._data = {}
350+
self._data: Dict[int, Step] = {}
336351

337352
def __repr__(self):
338353
return f'{self.sdat!r}.steps'
339354

355+
@typing.overload
356+
def __getitem__(self, istep: int) -> Step:
357+
...
358+
359+
@typing.overload
360+
def __getitem__(self,
361+
istep: Union[slice, Sequence[StepIndex]]) -> _StepsView:
362+
...
363+
340364
def __getitem__(self, istep):
341365
keys = _as_view_item(istep)
342366
if keys is not None:
@@ -354,38 +378,38 @@ def __getitem__(self, istep):
354378
self.sdat, istep,
355379
f'Last istep is {len(self) - 1}')
356380
if istep not in self._data:
357-
self._data[istep] = _step.Step(istep, self.sdat)
381+
self._data[istep] = Step(istep, self.sdat)
358382
return self._data[istep]
359383

360-
def __delitem__(self, istep):
384+
def __delitem__(self, istep: Optional[int]):
361385
if istep is not None and istep in self._data:
362386
self.sdat._collected_fields = [
363387
(i, f) for i, f in self.sdat._collected_fields if i != istep]
364388
del self._data[istep]
365389

366390
@crop
367-
def _len(self):
391+
def _len(self) -> int:
368392
# not necessarily the last one...
369393
return self.sdat.tseries.isteps[-1] + 1
370394

371-
def __len__(self):
395+
def __len__(self) -> int:
372396
return self._len
373397

374-
def __iter__(self):
398+
def __iter__(self) -> Iterator[Step]:
375399
return iter(self[:])
376400

377-
def at_time(self, time, after=False):
401+
def at_time(self, time: float, after: bool = False) -> Step:
378402
"""Return step corresponding to a given physical time.
379403
380404
Args:
381-
time (float): the physical time requested.
382-
after (bool): when False (the default), the returned step is such
383-
that its time is immediately before the requested physical
384-
time. When True, the returned step is the next one instead (if
385-
it exists, otherwise the same step is returned).
405+
time: the physical time requested.
406+
after: when False (the default), the returned step is such that its
407+
time is immediately before the requested physical time. When
408+
True, the returned step is the next one instead (if it exists,
409+
otherwise the same step is returned).
386410
387411
Returns:
388-
:class:`~stagpy._step.Step`: the relevant step.
412+
the relevant step.
389413
"""
390414
itime = _helpers.find_in_sorted_arr(time, self.sdat.tseries.time,
391415
after)
@@ -408,12 +432,9 @@ class _Snaps(_Steps):
408432
409433
This class inherits from :class:`_Steps`.
410434
411-
Args:
412-
sdat (:class:`StagyyData`): the StagyyData instance owning the
413-
:class:`_Snaps` instance.
414435
Attributes:
415-
sdat (:class:`StagyyData`): the StagyyData instance owning the
416-
:class:`_Snaps` instance.
436+
sdat: the :class:`StagyyData` instance owning the :class:`_Snaps`
437+
instance.
417438
"""
418439

419440
def __init__(self, sdat):

0 commit comments

Comments
 (0)