From 4a7a4e6796d43c0a6e64666d985a94726542d1e1 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Mon, 2 Dec 2019 15:36:57 +0100 Subject: [PATCH 1/3] [FasterLOSCalcSumCalls] Replace loop on partial sum by ufinc np.add.reduceat and made Plasma2D.get_intu() more robust vs one-time step --- tofu/data/_comp.py | 1 + tofu/data/_core.py | 12 ++++++---- tofu/geom/_core.py | 57 ++++++++++++++++------------------------------ 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/tofu/data/_comp.py b/tofu/data/_comp.py index 77abb52cb..2bbe96d4e 100644 --- a/tofu/data/_comp.py +++ b/tofu/data/_comp.py @@ -413,6 +413,7 @@ def func(pts, vect=None, t=None, ntall=ntall, shapeval = list(pts.shape) shapeval[0] = ntall if t is None else t.size val = np.full(tuple(shapeval), fill_value) + if t is None: for ii in range(0,ntall): val[ii,...] = mplTriLinInterp(mpltri, diff --git a/tofu/data/_core.py b/tofu/data/_core.py index 8913c41cd..dae5917c3 100644 --- a/tofu/data/_core.py +++ b/tofu/data/_core.py @@ -3677,11 +3677,15 @@ def _get_indtu(t=None, tall=None, tbinall=None, # Get indt (t with respect to tbinall) indt, indtu = None, None if t is not None: - indt = np.digitize(t, tbinall) - indtu = np.unique(indt) + if len(t) == len(tall) and np.allclose(t, tall): + indt = np.arange(0, tall.size) + indtu = indt + else: + indt = np.digitize(t, tbinall) + indtu = np.unique(indt) + # Update + tall = tall[indtu] - # Update - tall = tall[indtu] if idref1d is not None: assert indtr1 is not None indtr1 = indtr1[indtu] diff --git a/tofu/geom/_core.py b/tofu/geom/_core.py index 4e4021a11..6f36050f2 100644 --- a/tofu/geom/_core.py +++ b/tofu/geom/_core.py @@ -6025,7 +6025,7 @@ def calc_signal( reflections=True, coefs=None, ind=None, - out=object, + returnas=object, plot=True, dataname=None, fs=None, @@ -6086,7 +6086,7 @@ def calc_signal( # Format input indok, Ds, us, DL, E = self._calc_signal_preformat( - ind=ind, DL=DL, out=out, Brightness=Brightness + ind=ind, DL=DL, out=returnas, Brightness=Brightness ) if Ds is None: @@ -6180,19 +6180,8 @@ def calc_signal( # and interferometer) val = func(pts, t=t, vect=vect) # Integrate - if val.ndim == 2: - sig = np.full((val.shape[0], self.nRays), np.nan) - else: - sig = np.full((1, self.nRays), np.nan) - indpts = np.r_[0, indpts, pts.shape[1]] - for ii in range(0, self.nRays): - sig[:, ii] = ( - np.nansum( - val[:, indpts[ii]:indpts[ii + 1]], - axis=-1 - ) - * reseff[ii] - ) + sig = np.add.reduceat(val, np.r_[0, indpts], axis=-1)*reseff[None, :] + # Format output return self._calc_signal_postformat( sig, @@ -6202,7 +6191,7 @@ def calc_signal( E=E, units=units, plot=plot, - out=out, + out=returnas, fs=fs, dmargin=dmargin, wintit=wintit, @@ -6236,7 +6225,7 @@ def calc_signal_from_Plasma2D( reflections=True, coefs=None, ind=None, - out=object, + returnas=object, plot=True, dataname=None, fs=None, @@ -6250,7 +6239,7 @@ def calc_signal_from_Plasma2D( # Format input indok, Ds, us, DL, E = self._calc_signal_preformat( - ind=ind, out=out, t=t, Brightness=Brightness + ind=ind, out=returnas, t=t, Brightness=Brightness ) if Ds is None: @@ -6262,12 +6251,12 @@ def calc_signal_from_Plasma2D( # Get time vector if t is None: out = plasma2d._checkformat_qr12RPZ( - quant=quant, - ref1d=ref1d, - ref2d=ref2d, - q2dR=q2dR, - q2dPhi=q2dPhi, - q2dZ=q2dZ, + quant=quant, + ref1d=ref1d, + ref2d=ref2d, + q2dR=q2dR, + q2dPhi=q2dPhi, + q2dZ=q2dZ, ) t = plasma2d._get_tcom(*out[:4])[0] else: @@ -6368,6 +6357,8 @@ def funcbis(*args, **kwdargs): indpts[0], np.diff(indpts), pts.shape[1] - indpts[-1] ] vect = np.repeat(self.u, nbrep, axis=1) + if fill_value is None: + fill_value = 0. # Get quantity values at ptsRZ # This is the slowest step (~3.8 s with res=0.02 @@ -6388,19 +6379,9 @@ def funcbis(*args, **kwdargs): fill_value=fill_value, ) - # Integrate - if val.ndim == 2: - sig = np.full((val.shape[0], self.nRays), np.nan) - else: - sig = np.full((1, self.nRays), np.nan) - - indpts = np.r_[0, indpts, pts.shape[1]] - for ii in range(0, self.nRays): - sig[:, ii] = ( - np.nansum(val[:, indpts[ii]:indpts[ii + 1]], - axis=-1) - * reseff[ii] - ) + # Integrate using ufunc reduceat for speed + # (cf. https://stackoverflow.com/questions/59079141) + sig = np.add.reduceat(val, np.r_[0, indpts], axis=-1)*reseff[None, :] # Format output # this is the secod slowest step (~0.75 s) @@ -6412,7 +6393,7 @@ def funcbis(*args, **kwdargs): E=E, units=units, plot=plot, - out=out, + out=returnas, fs=fs, dmargin=dmargin, wintit=wintit, From e9af491fb8f046bedbc64fd0a725b8dd09c6fdb4 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Mon, 2 Dec 2019 16:05:35 +0100 Subject: [PATCH 2/3] [(FasterLOSCalcSignalSumCalls] Corrected tests --- tofu/tests/tests01_geom/tests03_core.py | 2 +- tofu/tests/tests02_data/tests03_core.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tofu/tests/tests01_geom/tests03_core.py b/tofu/tests/tests01_geom/tests03_core.py index 8fdd22356..70c1930bc 100644 --- a/tofu/tests/tests01_geom/tests03_core.py +++ b/tofu/tests/tests01_geom/tests03_core.py @@ -874,7 +874,7 @@ def ffT(Pts, t=None, vect=None): resMode=rm, method=dm, minimize=mmz, ind=ind, - plot=False, out=np.ndarray, + plot=False, returnas=np.ndarray, fs=(12, 6), connect=connect) sig, units = out assert not np.all(np.isnan(sig)), str(ii) diff --git a/tofu/tests/tests02_data/tests03_core.py b/tofu/tests/tests02_data/tests03_core.py index 1d526102f..a10d8ed89 100644 --- a/tofu/tests/tests02_data/tests03_core.py +++ b/tofu/tests/tests02_data/tests03_core.py @@ -475,7 +475,7 @@ def setup_class(cls, nch=30, nt=50, SavePath='./', verb=False): lData = [None for ii in range(0,len(lc))] for ii in range(0,len(lc)): sig = lc[ii].calc_signal(emiss, t=t, res=0.01, method=lm[ii], - plot=False, out=np.ndarray)[0] + plot=False, returnas=np.ndarray)[0] sig = sig[:,:,None]*flamb[None,None,:] cla = eval('tfd.DataCam%sDSpectral'%('2' if lc[ii]._is2D() else '1')) data = cla(data=sig, Name='All', Diag='Test', From 5d7d5f1d9fce1472d757e66d5cc3c1c901ea8fec Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Mon, 2 Dec 2019 16:20:24 +0100 Subject: [PATCH 3/3] [FasterLOSCalcSignalSumCalls] PEP8 Compliance --- tofu/geom/_core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tofu/geom/_core.py b/tofu/geom/_core.py index 6f36050f2..627ede3de 100644 --- a/tofu/geom/_core.py +++ b/tofu/geom/_core.py @@ -6180,7 +6180,8 @@ def calc_signal( # and interferometer) val = func(pts, t=t, vect=vect) # Integrate - sig = np.add.reduceat(val, np.r_[0, indpts], axis=-1)*reseff[None, :] + sig = np.add.reduceat(val, np.r_[0, indpts], + axis=-1)*reseff[None, :] # Format output return self._calc_signal_postformat( @@ -6381,7 +6382,8 @@ def funcbis(*args, **kwdargs): # Integrate using ufunc reduceat for speed # (cf. https://stackoverflow.com/questions/59079141) - sig = np.add.reduceat(val, np.r_[0, indpts], axis=-1)*reseff[None, :] + sig = np.add.reduceat(val, np.r_[0, indpts], + axis=-1)*reseff[None, :] # Format output # this is the secod slowest step (~0.75 s)