From 565d736569017f3ef836ee3d65b3bf274a9d32da Mon Sep 17 00:00:00 2001 From: Laura Mendoza Date: Fri, 15 Nov 2019 18:59:55 +0100 Subject: [PATCH 1/4] [Issue #252] trying something --- examples/tutorials/plot_custom_emissivity.py | 9 ++++- tofu/geom/_core.py | 39 +++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/examples/tutorials/plot_custom_emissivity.py b/examples/tutorials/plot_custom_emissivity.py index 349aa3d45..0cb041359 100644 --- a/examples/tutorials/plot_custom_emissivity.py +++ b/examples/tutorials/plot_custom_emissivity.py @@ -51,6 +51,11 @@ def emissivity(pts, t=None, vect=None): e = np.reshape(e, (1, -1)) return e +def ff(Pts, t=None): + print("pts =", Pts) + print("t=", t) + print("--------") + return np.zeros(Pts.shape[1])+1 y = np.linspace(2, 3, num=90) z = np.linspace(-0.5, 0.5, num=100) @@ -81,11 +86,11 @@ def project_to_2D(xyz): time_vector = np.linspace(0, 2 * np.pi, num=100) -sig, units = cam2d.calc_signal(emissivity, +sig, units = cam2d.calc_signal(ff, res=0.01, reflections=False, newcalc=True, plot=False, - t=time_vector) + t=None) sig.plot(ntMax=1) plt.show() diff --git a/tofu/geom/_core.py b/tofu/geom/_core.py index 487c9d149..874c0b8fc 100644 --- a/tofu/geom/_core.py +++ b/tofu/geom/_core.py @@ -5763,22 +5763,22 @@ def calc_min_rho_from_Plasma2D(self, plasma, t=None, log='min', def get_inspector(self, ff): out = inspect.signature(ff) pars = out.parameters.values() - na = np.sum([(pp.kind==pp.POSITIONAL_OR_KEYWORD + na = np.sum([(pp.kind == pp.POSITIONAL_OR_KEYWORD and pp.default is pp.empty) for pp in pars]) - kw = [pp.name for pp in pars if (pp.kind==pp.POSITIONAL_OR_KEYWORD + kw = [pp.name for pp in pars if (pp.kind == pp.POSITIONAL_OR_KEYWORD and pp.default is not pp.empty)] return na, kw def check_ff(self, ff, t=None, ani=None): time_steps = -1 - # .. Checking basic definition of function ............................. + # .. Checking basic definition of function ............................ str_error = "Input emissivity function (ff): " assert hasattr(ff, '__call__'), (str_error + " must be a callable (function)!") npos_args, kw = self.get_inspector(ff) - assert npos_args==1, (str_error - + " must take only one positional argument:" - + " ff(Pts)!") + assert npos_args == 1, (str_error + + " must take only one positional argument:" + + " ff(Pts)!") assert 't' in kw, (str_error + " must have kwarg 't=None' for time vector!") is_t_type_valid = (type(t) in [int, float, np.int64, np.float64] @@ -5786,7 +5786,7 @@ def check_ff(self, ff, t=None, ani=None): assert t is None or is_t_type_valid, (str_error + "Arg t must be None," + " a scalar or an iterable!") - # .. Testing outputs ................................................... + # .. Testing outputs .................................................. test_pts = np.array([[1, 2], [3, 4], [5, 6]]) npts = test_pts.shape[1] try: @@ -5802,13 +5802,18 @@ def check_ff(self, ff, t=None, ani=None): + " t a len()=nt iterable," + " must return a (nt, npts) np.ndarray!") assert (type(out) is np.ndarray - and out.shape==(time_steps, npts)), err_msg + and out.shape == (time_steps, npts)), err_msg else: err_msg = (str_error + " When t=None or t is a scalar," + " ff must return a 2D (1, npts) np.ndarray!") - assert type(out) is np.ndarray and out.shape==(1, npts), err_msg - + assert ((out.shape == (1, npts) or out.shape == (npts,)) + and type(out) is np.ndarray), err_msg + if out.shape == (npts,): + def wrapped(ff, *args, **kwargs): + return np.reshape(ff(*args, **kwargs), (1,npts)) + loc_ff = wrapped(ff, *args, **kwargs) + return loc_ff is_ani = ('vect' in kw) if ani is None else ani if is_ani: err_msg = (str_error @@ -5831,14 +5836,20 @@ def check_ff(self, ff, t=None, ani=None): + " np.ndarray when Pts is (3,N), vect is provided" + " and t is a list (nt,)") assert (type(out) is np.ndarray - and out.shape==(time_steps, npts)), err_msg + and out.shape == (time_steps, npts)), err_msg else: err_msg = (str_error + "If ani=True, ff must return a (1, npts)" + " np.ndarray when Pts is (3, npts), vect is" + " provided and t is None or a scalar") - assert type(out) is np.ndarray and out.shape==(1, npts), err_msg - return + assert (type(out) is np.ndarray + and (out.shape == (1, npts) + or out.shape == (npts,))), err_msg + if out.shape == (npts,): + def wrapped(*args, **kwargs): + return np.reshape(ff(*args, **kwargs), (1,npts)) + return wrapped(ff) + return ff def _calc_signal_preformat(self, ind=None, DL=None, t=None, out=object, Brightness=True): @@ -6046,7 +6057,7 @@ def calc_signal( # Launch # NB : find a way to exclude cases with DL[0,:]>=DL[1,:] !! # Exclude Rays not seeing the plasma if newcalc: - self.check_ff(func, t=t, ani=ani) + func = self.check_ff(func, t=t, ani=ani) s = _GG.LOS_calc_signal( func, Ds, From 3cf115dc749b58cfbfb13b268256b9a01d37b6c9 Mon Sep 17 00:00:00 2001 From: Laura Mendoza Date: Mon, 18 Nov 2019 14:44:00 +0100 Subject: [PATCH 2/4] [#252] right definition of wrapped function --- tofu/geom/_core.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tofu/geom/_core.py b/tofu/geom/_core.py index 874c0b8fc..756aa0a6b 100644 --- a/tofu/geom/_core.py +++ b/tofu/geom/_core.py @@ -5771,6 +5771,7 @@ def get_inspector(self, ff): def check_ff(self, ff, t=None, ani=None): time_steps = -1 + wrapped_ff = ff # .. Checking basic definition of function ............................ str_error = "Input emissivity function (ff): " assert hasattr(ff, '__call__'), (str_error @@ -5810,10 +5811,10 @@ def check_ff(self, ff, t=None, ani=None): assert ((out.shape == (1, npts) or out.shape == (npts,)) and type(out) is np.ndarray), err_msg if out.shape == (npts,): - def wrapped(ff, *args, **kwargs): - return np.reshape(ff(*args, **kwargs), (1,npts)) - loc_ff = wrapped(ff, *args, **kwargs) - return loc_ff + def wrapped_ff(*args, **kwargs): + res_ff = ff(*args, **kwargs) + npts_loc = res_ff.size + return np.reshape(res_ff, (1, npts_loc)) is_ani = ('vect' in kw) if ani is None else ani if is_ani: err_msg = (str_error @@ -5846,10 +5847,11 @@ def wrapped(ff, *args, **kwargs): and (out.shape == (1, npts) or out.shape == (npts,))), err_msg if out.shape == (npts,): - def wrapped(*args, **kwargs): - return np.reshape(ff(*args, **kwargs), (1,npts)) - return wrapped(ff) - return ff + def wrapped_ff(*args, **kwargs): + res_ff = ff(*args, **kwargs) + npts_loc = res_ff.size + return np.reshape(res_ff, (1, npts_loc)) + return wrapped_ff def _calc_signal_preformat(self, ind=None, DL=None, t=None, out=object, Brightness=True): From 677d6496883ef1df263a9f04cc7e04ee0f0b3209 Mon Sep 17 00:00:00 2001 From: Laura Mendoza Date: Mon, 18 Nov 2019 16:12:52 +0100 Subject: [PATCH 3/4] [bisect 252] undid changes in tuto --- examples/tutorials/tuto_plot_custom_emissivity.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/tutorials/tuto_plot_custom_emissivity.py b/examples/tutorials/tuto_plot_custom_emissivity.py index 710f82080..2b63672da 100644 --- a/examples/tutorials/tuto_plot_custom_emissivity.py +++ b/examples/tutorials/tuto_plot_custom_emissivity.py @@ -31,6 +31,7 @@ # Now, we define an emissivity function that depends on r and z coordinates. # We can plot its profile in the (0, X, Z) plane. + def emissivity(pts, t=None, vect=None): """Custom emissivity as a function of geometry. @@ -51,11 +52,6 @@ def emissivity(pts, t=None, vect=None): e = np.reshape(e, (1, -1)) return e -def ff(Pts, t=None): - print("pts =", Pts) - print("t=", t) - print("--------") - return np.zeros(Pts.shape[1])+1 y = np.linspace(2, 3, num=90) z = np.linspace(-0.5, 0.5, num=100) @@ -86,7 +82,7 @@ def project_to_2D(xyz): time_vector = np.linspace(0, 2 * np.pi, num=100) -sig, units = cam2d.calc_signal(ff, +sig, units = cam2d.calc_signal(emissivity, res=0.01, reflections=False, minimize="hybrid", From 67fdeeec76a121ee89b50df7dfce9fcac7822801 Mon Sep 17 00:00:00 2001 From: Laura Mendoza Date: Mon, 18 Nov 2019 16:53:00 +0100 Subject: [PATCH 4/4] [feature] cleaner way of defining 2nd dim --- tofu/geom/_core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tofu/geom/_core.py b/tofu/geom/_core.py index a763c25d3..13357a8c9 100644 --- a/tofu/geom/_core.py +++ b/tofu/geom/_core.py @@ -5848,8 +5848,7 @@ def check_ff(self, ff, t=None, ani=None): if nt == 1 and out.shape == (npts,): def wrapped_ff(*args, **kwargs): res_ff = ff(*args, **kwargs) - npts_loc = res_ff.size - return np.reshape(res_ff, (1, npts_loc)) + return np.reshape(res_ff, (1, -1)) return is_ani, wrapped_ff