-
Notifications
You must be signed in to change notification settings - Fork 13
Faster los calc signal sum calls for newcalc=False #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6025,7 +6025,7 @@ def calc_signal( | |
| reflections=True, | ||
| coefs=None, | ||
| ind=None, | ||
| out=object, | ||
| returnas=object, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much more explicit variable name, especially since we tend to use 'out' to store temporary output inside functions. |
||
| 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,9 @@ 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], | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More concise and numerically faster, only advantages :-) Compatible with parallelization ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, only functions that don't require gil can be parallelized, calling numpy functions requires the gil, so it is not possible. This is why it wasn't parallelized with newcalc=True |
||
| axis=-1)*reseff[None, :] | ||
|
|
||
| # Format output | ||
| return self._calc_signal_postformat( | ||
| sig, | ||
|
|
@@ -6202,7 +6192,7 @@ def calc_signal( | |
| E=E, | ||
| units=units, | ||
| plot=plot, | ||
| out=out, | ||
| out=returnas, | ||
| fs=fs, | ||
| dmargin=dmargin, | ||
| wintit=wintit, | ||
|
|
@@ -6236,7 +6226,7 @@ def calc_signal_from_Plasma2D( | |
| reflections=True, | ||
| coefs=None, | ||
| ind=None, | ||
| out=object, | ||
| returnas=object, | ||
| plot=True, | ||
| dataname=None, | ||
| fs=None, | ||
|
|
@@ -6250,7 +6240,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 +6252,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 +6358,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: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid nans, they are not handled by np.add.reduceat() => we set fill_value to 0 instead in this case (if not forced by the user) |
||
| fill_value = 0. | ||
|
|
||
| # Get quantity values at ptsRZ | ||
| # This is the slowest step (~3.8 s with res=0.02 | ||
|
|
@@ -6388,19 +6380,10 @@ 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 +6395,7 @@ def funcbis(*args, **kwdargs): | |
| E=E, | ||
| units=units, | ||
| plot=plot, | ||
| out=out, | ||
| out=returnas, | ||
| fs=fs, | ||
| dmargin=dmargin, | ||
| wintit=wintit, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handle trivial case first, particularly suited for cases with t.size = 1