Skip to content

Commit

Permalink
[#983] Re-clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
dvezinet committed Nov 13, 2024
1 parent e4c6775 commit 34c13b1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
14 changes: 11 additions & 3 deletions tofu/imas2tofu/_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
raise Exception('imas not available')


# Useful scalar types
_NINT = (np.int32, np.int64)
_INT = (int,) + _NINT
_NFLOAT = (np.float32, np.float64)
_FLOAT = (float,) + _NFLOAT
_NUMB = _INT + _FLOAT


_DSHORT = _defimas2tofu._dshort
_DCOMP = _defimas2tofu._dcomp
_DDUNITS = imas.dd_units.DataDictionaryUnits()
Expand Down Expand Up @@ -255,7 +263,7 @@ def fsig(obj, indt=None, indch=None, stack=None, dcond=dcond):
),
(
stack and nsig > 1
and type(sig[0]) in [int, float, np.int_, np.float64, str]
and isinstance(sig[0], _NUMB + (str,))
),
(
stack and nsig == 1
Expand Down Expand Up @@ -494,7 +502,7 @@ def _checkformat_getdata_indch(indch, nch):

lc0 = [
indch is None,
isinstance(indch, int),
isinstance(indch, _INT),
hasattr(indch, '__iter__') and not isinstance(indch, str),
]

Expand Down Expand Up @@ -588,7 +596,7 @@ def _check_data(data, pos=None, nan=None, isclose=None, empty=None):
for ii in range(0, len(data)):
c0 = (
isinstance(data[ii], np.ndarray)
and any([ss in data[ii].dtype.name for ss in ['int', 'float']])
and data.dtype in _NUMB
)
if c0 is True:
# Make sure to test only non-nan to avoid warning
Expand Down
50 changes: 37 additions & 13 deletions tofu/imas2tofu/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""


# Built-ins
import sys
import os
Expand All @@ -18,11 +19,13 @@
import warnings
import traceback


# Standard
import numpy as np
import matplotlib as mpl
import datetime as dtm


# tofu
pfe = os.path.join(os.path.expanduser('~'), '.tofu', '_imas2tofu_def.py')
if os.path.isfile(pfe):
Expand Down Expand Up @@ -50,13 +53,15 @@
from . import _comp_toobjects as _comp_toobjects
from . import _comp_mesh as _comp_mesh


# imas
try:
import imas
from imas import imasdef
except Exception as err:
raise Exception('imas not available')


__all__ = [
'check_units_IMASvsDSHORT',
'MultiIDSLoader',
Expand All @@ -70,6 +75,14 @@
_ROOT = _ROOT[:_ROOT.index('tofu')+len('tofu')]


# Useful scalar types
_NINT = (np.int32, np.int64)
_INT = (int,) + _NINT
_NFLOAT = (np.float32, np.float64)
_FLOAT = (float,) + _NFLOAT
_NUMB = _INT + _FLOAT


#############################################################
# Preliminary units check
#############################################################
Expand Down Expand Up @@ -360,7 +373,7 @@ def _get_diddids(cls, dids, defidd=None):
v = dids[k]

# Check / format occ and deduce nocc
assert type(dids[k]['occ']) in [int, list]
assert isinstance(dids[k]['occ'], _INT + (list,)), type(dids[k]['occ'])
dids[k]['occ'] = np.r_[dids[k]['occ']].astype(int)
dids[k]['nocc'] = dids[k]['occ'].size
v = dids[k]
Expand Down Expand Up @@ -936,7 +949,7 @@ def _checkformat_idd(

if lc[0]:
# check type is int or numpy int
assert type(shot) == int, type(shot)
assert isinstance(shot, _INT), type(shot)

params = dict(
shot=int(shot), run=run, refshot=refshot, refrun=refrun,
Expand Down Expand Up @@ -1203,7 +1216,7 @@ def _checkformat_ids(

if occ is None:
occ = 0
lc = [type(occ) == int, hasattr(occ, '__iter__')]
lc = [isinstance(occ, _INT), hasattr(occ, '__iter__')]
assert any(lc), occ

if lc[0]:
Expand Down Expand Up @@ -1484,13 +1497,12 @@ def _checkformat_getdata_indt(self, indt):
msg += " - None: all channels used\n"
msg += " - int: times to use (index)\n"
msg += " - array of int: times to use (indices)"
lc = [type(indt) is None, type(indt) is int, hasattr(indt,'__iter__')]
lc = [type(indt) is None, isinstance(indt, _INT), hasattr(indt,'__iter__')]
if not any(lc):
raise Exception(msg)
if lc[1] or lc[2]:
indt = np.r_[indt].rave()
lc = [indt.dtype == int]
if not any(lc):
if indt.dtype not in _NINT:
raise Exception(msg)
assert np.all(indt>=0)
return indt
Expand Down Expand Up @@ -1673,18 +1685,26 @@ def get_lidsidd_shotExp(self, lidsok,
dids=self._dids, didd=self._didd)

def _get_t0(self, t0=None, ind=None):

if ind is None:
ind = False
assert ind is False or isinstance(ind, int)
assert ind is False or isinstance(ind, _INT)

if t0 is None:
t0 = _defimas2tofu._T0

elif t0 != False:
if type(t0) in [int, float, np.float64]:

if isinstance(t0, _NUMB):
t0 = float(t0)

elif type(t0) is str:
t0 = t0.strip()
c0 = (len(t0.split('.')) <= 2
and all([ss.isdecimal() for ss in t0.split('.')]))
c0 = (
len(t0.split('.')) <= 2
and all([ss.isdecimal() for ss in t0.split('.')])
)

if 'pulse_schedule' in self._dids.keys():
events = self.get_data(
dsig={'pulse_schedule': ['events_names',
Expand Down Expand Up @@ -1714,6 +1734,7 @@ def _get_t0(self, t0=None, ind=None):
t0 = False
else:
t0 = False

if t0 is False:
msg = "t0 set to False because could not be interpreted !"
warnings.warn(msg)
Expand Down Expand Up @@ -2318,21 +2339,24 @@ def get_tlim(
"""
names, times = None, None
c0 = (isinstance(tlim, list)
and all([type(tt) in [float, int, np.float64]
for tt in tlim]))
c0 = (
isinstance(tlim, list)
and all([isinstance(tt, _NUMB) for tt in tlim])
)
if not c0 and 'pulse_schedule' in self._dids.keys():
try:
names, times = self.get_events(verb=False, returnas=tuple)
except Exception as err:
msg = (str(err)
+ "\nEvents not loaded from ids pulse_schedule!")
warnings.warn(msg)

if 'pulse_schedule' in self._dids.keys():
idd = self._dids['pulse_schedule']['idd']
Exp = self._didd[idd]['params']['database']
else:
Exp = None

return _comp_toobjects.data_checkformat_tlim(t, tlim=tlim,
names=names, times=times,
indevent=indevent,
Expand Down
6 changes: 4 additions & 2 deletions tofu/imas2tofu/_mat2ids2calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
+ " => Maybe corrupted data ?\n")


_LTYPES = (int, float, np.integer, np.float64)


# ####################################################
# Utility
# ####################################################
Expand All @@ -34,8 +37,7 @@ def _get_indtlim(t, tlim=None, shot=None, out=bool):
tlim = [-np.inf, np.inf]
else:
assert len(tlim) == 2
ls = [int, float, np.float64] # , str
assert all([tt is None or type(tt) in ls for tt in tlim])
assert all([tt is None or isinstance(tt, _LTYPES) for tt in tlim])
tlim = list(tlim)
for (ii, sgn) in [(0, -1.), (1, 1.)]:
if tlim[ii] is None:
Expand Down
4 changes: 2 additions & 2 deletions tofu/nist2tofu/_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
}


_LTYPES = [int, float, np.float64]
_LTYPES = (int, float, np.integer, np.float64)


_DCERTIFICATES_BUNDLE = {
Expand Down Expand Up @@ -221,7 +221,7 @@ def _get_totalurl(
if v0 is None:
dlamb[k0] = ''
else:
c0 = type(v0) in _LTYPES
c0 = isinstance(v0, _LTYPES)
if not c0:
msg = (
"Arg {} must be a float!\n".format(k0)
Expand Down
6 changes: 3 additions & 3 deletions tofu/spectro/_analysis_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]


_LTYPES = [int, float, np.int_, np.float64]
_LTYPES = (int, float, np.integer, np.float64)

_GITHUB = 'https://github.com/ToFuProject/tofu/issues'
_WINTIT = f'tofu-{__version__}\treport issues / requests at {_GITHUB}'
Expand Down Expand Up @@ -127,7 +127,7 @@ def _get_localextrema_1d_check(
width = 0.
else:
width = False
c0 = width is False or (type(width) in _LTYPES and width >= 0.)
c0 = width is False or (isinstance(width, _LTYPES) and width >= 0.)
if not c0:
msg = (
"Arg width must be a float\n"
Expand All @@ -146,7 +146,7 @@ def _get_localextrema_1d_check(

if rel_height is None:
rel_height = 0.8
if not (type(rel_height) in _LTYPES and 0 <= rel_height <= 1.):
if not (isinstance(rel_height, _LTYPES) and 0 <= rel_height <= 1.):
msg = (
"Arg rel_height must be positive float in [0, 1]!\n"
+ "Provided: {}".format(rel_height)
Expand Down

0 comments on commit 34c13b1

Please sign in to comment.