Skip to content

Commit

Permalink
Merge pull request #1010 from ToFuProject/Issue1009_IMASdtype
Browse files Browse the repository at this point in the history
[#1009] imas2tofu mMore robust vs dtype
  • Loading branch information
Didou09 authored Jan 16, 2025
2 parents 0f5f3a1 + be214ad commit b8c4f73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
66 changes: 48 additions & 18 deletions tofu/imas2tofu/_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
_NFLOAT = (np.float32, np.float64)
_FLOAT = (float,) + _NFLOAT
_NUMB = _INT + _FLOAT
_BOOL = (bool, np.bool_)


_DSHORT = _defimas2tofu._dshort
Expand Down Expand Up @@ -209,14 +210,23 @@ def fsig(obj, indt=None, indch=None, stack=None, dcond=dcond):

if ind is None:
ind = range(0, nb)

if nsig > 1:
if isinstance(ind, str) or len(ind) != 1:
msg = ('ind should be have len() = 1\n'
+ '\t- ind: {}'.format(ind))
raise Exception(msg)

if len(ind) == 1:
sig[jj] = sig[jj][ind[0]]
if len(sig[jj]) < ind[0] + 1:
msg = (
f"dcond[{ii}]['ind'] = {dcond[ii]['ind']} "
f"so ind = {ind} "
f"but len(sig[{jj}]) = {len(sig[jj])}"
)
raise Exception(msg)
else:
sig[jj] = sig[jj][ind[0]]
else:
if nsig != 1:
msg = ("nsig should be 1!\n"
Expand Down Expand Up @@ -533,8 +543,11 @@ def _checkformat_getdata_indch(indch, nch):
# make numpy array
indch = np.r_[indch].ravel()

# get dtype
lc1 = ['int' in indch.dtype.name, 'bool' in indch.dtype.name]
# get dtype (may also be a list)
lc1 = [
isinstance(indch[0], _INT),
isinstance(indch[0], _BOOL),
]

if not any(lc1):
raise Exception(msg)
Expand Down Expand Up @@ -596,14 +609,17 @@ 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 data.dtype in _NUMB
and data[ii].dtype in _NUMB
)
if c0 is True:
# Make sure to test only non-nan to avoid warning
ind = (~np.isnan(data[ii])).nonzero()
ind2 = np.abs(data[ii][ind]) > 1.e30
ind = tuple([ii[ind2] for ii in ind])
data[ii][ind] = np.nan
if np.any(ind2):
ind = tuple([ii[ind2] for ii in ind])
if data[ii].dtype in _INT:
data[ii] = data[ii].astype(float)
data[ii][ind] = np.nan

# data supposed to be positive only (nan otherwise)
if pos is True:
Expand All @@ -629,7 +645,7 @@ def _get_data_units(ids=None, sig=None, occ=None,
stack=None, isclose=None, flatocc=None,
nan=None, pos=None, empty=None,
dids=None, dcomp=None, dshort=None, dall_except=None,
data=True, units=True):
data=True, units=True, strict=None):
""" Reference method for getting data and units, using shortcuts
For a given ids, sig (shortcut) and occurence (occ)
Expand Down Expand Up @@ -682,21 +698,23 @@ def _get_data_units(ids=None, sig=None, occ=None,
data=True, units=False, indt=indt, stack=stack,
flatocc=False, nan=nan, pos=pos, warn=False,
dids=dids, dcomp=dcomp, dshort=dshort,
dall_except=dall_except)[ids]
dall_except=dall_except,
strict=strict,
)[ids]
out = [dcomp[ids][sig]['func'](
*[ddata[kk]['data'][nn] for kk in lstr],
**kargs)
for nn in range(0, nocc)]
if pos is None:
pos = dcomp[ids][sig].get('pos', False)
except Exception as err:
errdata = str(err)
errdata = err

if units is True:
try:
unit = dcomp[ids][sig].get('units', None)
except Exception as err:
errunits = str(err)
errunits = err

# Data available from ids
else:
Expand All @@ -709,13 +727,13 @@ def _get_data_units(ids=None, sig=None, occ=None,
if pos is None:
pos = dshort[ids][sig].get('pos', False)
except Exception as err:
errdata = str(err)
errdata = err

if units is True:
try:
unit = get_units(ids, sig)
except Exception as err:
errunits = str(err)
errunits = err

# Check data
isempty = None
Expand All @@ -725,7 +743,7 @@ def _get_data_units(ids=None, sig=None, occ=None,
isclose=isclose, empty=empty)
if np.all(isempty):
msg = ("empty data in {}.{}".format(ids, sig))
errdata = msg
errdata = Exception(msg)
elif nocc == 1 and flatocc is True:
out = out[0]
isempty = isempty[0]
Expand Down Expand Up @@ -803,7 +821,9 @@ def get_data_units(dsig=None, occ=None,
data=data, units=units,
nan=nan, pos=pos, empty=empty,
dids=dids, dcomp=dcomp, dshort=dshort,
dall_except=dall_except)
dall_except=dall_except,
strict=strict,
)

lc = [dout[ids][sigi]['errdata'] is not None,
dout[ids][sigi]['errunits'] is not None]
Expand All @@ -815,27 +835,37 @@ def get_data_units(dsig=None, occ=None,
dfail[ids][sigi+' units'] = dout[ids][sigi]['errunits']

except Exception as err:
dfail[ids][sigi] = str(err)
dfail[ids][sigi] = err
anyfail = True

if len(dfail[ids]) == 0:
del dfail[ids]

# ---------------------
# Print if any failure

if anyfail:

if strict is True:
for ids, vids in dfail.items():
for sigi, verr in vids.items():
print(f"\n{ids}\t{sigi}\t{verr}")
raise verr

if data is True:
for ids in dfail.keys():
for sigi in list(dout[ids].keys()):
if dout[ids][sigi]['errdata'] is not None:
del dout[ids][sigi]

if warn:
msg = "The following data could not be retrieved:"
for ids in dfail.keys():
nmax = np.max([len(k1) for k1 in dfail[ids].keys()])
msg += "\n\t- {}:".format(ids)
for sigi, msgi in dfail[ids].items():
msg += "\n\t\t{0}: {1}".format(sigi.ljust(nmax),
msgi.replace('\n', ' '))
for sigi, erri in dfail[ids].items():
msgi = str(erri).replace('\n', ' ')
msg += f"\n\t\t{sigi.ljust(nmax)}: {msgi}"
warnings.warn(msg)

# return
Expand Down
5 changes: 3 additions & 2 deletions tofu/imas2tofu/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
_NFLOAT = (np.float32, np.float64)
_FLOAT = (float,) + _NFLOAT
_NUMB = _INT + _FLOAT
_BOOL = (bool, np.bool_)


#############################################################
Expand Down Expand Up @@ -2021,7 +2022,7 @@ class with which the output shall be returned
pos=pos,
empty=empty,
isclose=isclose,
strict=True,
strict=strict,
return_all=False,
)

Expand Down Expand Up @@ -3193,7 +3194,7 @@ def _fill_idsproperties(ids, com, tfversion, nt=None):
ids.code.version = tfversion
if nt is None:
nt = 1
ids.code.output_flag = np.zeros((nt,),dtype=int)
ids.code.output_flag = np.zeros((nt,), dtype=int)
ids.code.parameters = ""

def _put_ids(idd, ids, shotfile, occ=0, cls_name=None,
Expand Down

0 comments on commit b8c4f73

Please sign in to comment.