def test_Kamodo_reassignment(): a, b, c, x, y, z, r = symbols('a b c x y z r') kamodo = Kamodo('f(x) = 3*x+5', verbose=True) kamodo['r(x,y)'] = x + y kamodo['r(x,y)'] = "3*x + y" assert kamodo.r(1, 1) != 2 assert kamodo.r(1, 1) == 4
def test_simple_figure(): @kamodofy(units='kg', hidden_args=['ions']) def f_N(x_N): return x_N**2 kamodo = Kamodo(f_N=f_N,verbose=True) kamodo.plot(f_N=dict(x_N=np.linspace(-4, 3, 30)))
def test_eval_no_defaults(): kamodo = Kamodo(f='x', verbose=True) kamodo['g'] = lambda x=3: x kamodo['h'] = kamodofy(lambda x=[2,3,4]: (kamodofy(lambda x_=np.array([1]): x_**2) for x_ in x)) assert kamodo.evaluate('f', x=3)['x'] == 3 assert kamodo.evaluate('g')['x'] == 3 assert kamodo.evaluate('h')['x_'][-2] == 1.
def test_unavailable_4d_plot_type(): def g(x=np.array([1]), y=np.array([1]), z=np.array([1]), t=np.array([1])): return x**2 + y**2 + z**2 + t**2 kamodo = Kamodo(g=g, verbose=True) with pytest.raises(KeyError): kamodo.plot('g')
def test_tri_surface_plot(): R = 1 theta = np.linspace(.2 * np.pi, .8 * np.pi, 40) phi = np.linspace(0, 2 * np.pi, 50) theta_, phi_ = np.meshgrid(theta, phi) r = (R + .1 * (np.cos(10 * theta_) * np.sin(14 * phi_))) xx = r * np.sin(theta_) * np.cos(phi_) yy = r * np.sin(theta_) * np.sin(phi_) zz = r * np.cos(theta_) def tri_surface(x_NM=xx, y_NM=yy, z_NM=zz): return .1 * x_NM + x_NM ** 2 + y_NM ** 2 + z_NM ** 2 title = '$\\operatorname{h_{NM}}{\\left(x_{NM},y_{NM},z_{NM} \\right)} = \\lambda{\\left(x_{NM},y_{NM},' \ 'z_{NM} \\right)}$ ' kamodo = Kamodo(h_NM=tri_surface) results = kamodo.evaluate('h_NM') titles = dict(variable='h_NM', title=title) traces, chart_type, layout = tri_surface_plot(results, titles, True) assert chart_type == '3d-surface' assert layout['title']['text'] == title
def test_symbol_key(): f = symbols('f', cls=UndefinedFunction) x = Symbol('x') f_ = f(x) kamodo = Kamodo(verbose=True) kamodo[f_] = x ** 2 assert kamodo.f(3) == 9
def test_Kamodo_ambiguous_expr(): a, b, c, x, y, z = symbols('a b c x y z') with pytest.raises(NameError): kamodo = Kamodo() kamodo['a(b,c)'] = x ** 2 + y ** 2 kamodo = Kamodo() kamodo['a(x, y)'] = x ** 2 + y ** 2 assert kamodo.a(3, 4) == 3 ** 2 + 4 ** 2
def test_kamodo_inline_merge(): k1 = Kamodo(f='x**2') k2 = Kamodo(g=lambda y: y-1) # create a local namespace holding both kamodo objects ns = {'k1':k1, 'k2': k2} k3 = Kamodo(myf = sympify('k1.f(x) + k2.g(y)', locals=ns)) assert k3.myf(x=3, y=4) == 3**2 + 4 - 1
def test_unit_composition_registration(): server = Kamodo(**{'M': kamodofy(lambda r=3: r, units='kg'), 'V[m^3]': (lambda r: r**3)}, verbose=True) user = Kamodo(mass=server.M, vol=server.V, **{'rho(r)[g/cm^3]':'mass/vol'}, verbose=True) result = (3/3**3)*(1000)*(1/10**6) assert np.isclose(user.rho(3), result)
def test_unit_composition_conversion(): kamodo = Kamodo('a(x[kg])[m] = x', verbose=True) kamodo['b(x[cm])[g]'] = 'x' kamodo['c'] = 'b(a)' assert kamodo.c.meta['units'] == 'g' assert kamodo.c.meta['arg_units']['x'] == 'kg' assert kamodo.c(3) == kamodo.b(100*kamodo.a(3))
def test_Kamodo_str(): kamodo = Kamodo('f(a,x,b) = a*x+b') try: assert kamodo.f(3, 4, 5) == 3 * 4 + 5 except: print(kamodo.signatures) print(list(kamodo.keys())) raise
def test_Kamodo_get(): kamodo = Kamodo('$f(a,x,b) = a^x+b $') try: assert kamodo['f'](3, 4, 5) == 3 ** 4 + 5 except: print(kamodo.signatures) print(list(kamodo.keys())) raise
def test_Kamodo_expr(): a, b, c, x, y, z = symbols('a b c x y z') kamodo = Kamodo(a=x ** 2, verbose=True) try: assert kamodo.a(3) == 3 ** 2 except: print(kamodo.symbol_registry) raise
def test_default_forwarding(): x = np.linspace(-5, 5, 12) def f(x=x): return x**2 k = Kamodo(f=f) k['g'] = 'f+2' assert len(k.g()) == 12
def test_image_plot(): def img(i=np.arange(33),j=np.arange(35)): ii, jj, kk = np.meshgrid(i,j,[100, 200, 255], indexing='ij') return kk kamodo = Kamodo(img=img) results = kamodo.evaluate('img') titles = dict(variable='img', title='mytitle') [trace], chart_type, layout = image(results, titles, True)
def test_compose(): k1 = Kamodo(f='x') k2 = Kamodo(g='y**2', h = kamodofy(lambda x: x**3)) k3 = compose(m1=k1, m2=k2) assert k3.f_m1(3) == 3 assert k3.g_m2(3) == 3**2 assert k3.h_m2(3) == 3**3 k3['h(f_m1)'] = 'f_m1' assert k3.h(3) == 3
def test_Kamodo_assign_by_callable(): kamodo = Kamodo(f=lambda x: x ** 2, verbose=False) assert kamodo['f'](3) == 3 ** 2 assert kamodo.f(3) == 3 ** 2 Kamodo(f=lambda x, y: x + y) def rho(x, y): return x + y kamodo['g'] = rho assert kamodo.g(3, 4) == 3 + 4
def test_scatter_plot(): title = 'test plot title' kamodo_obj = Kamodo(f='x**3') result = kamodo_obj.evaluate('f', x=np.random.random((100, 3))) titles = dict(variable='x', title=title) [trace], plot_type, layout = scatter_plot(result, titles, True) assert trace['mode'] == 'markers' assert layout['title']['text'] == title assert plot_type == '3d-scatter'
def test_del_function(): kamodo = Kamodo(f='x', g='y', h='y', verbose=True) del(kamodo.f) assert 'f' not in kamodo del(kamodo['g']) assert 'g' not in kamodo del(kamodo['h(y)']) print(kamodo.keys()) assert 'h(y)' not in kamodo with pytest.raises(AttributeError): del(kamodo.y)
def test_line_plot_line(): kamodo = Kamodo(f='x**3') result = kamodo.evaluate('f', x=np.linspace(-1, 1, 100)) titles = dict(variable='f', title='$f(x)=x^3$', title_lhs='f', title_short='f') traces, plot_type, layout = line_plot(result, titles, True) assert traces[0]['name'] == 'f' assert layout['title']['text'] == '$f(x)=x^3$' assert plot_type == 'line'
def test_compose_unit_add(): kamodo = Kamodo(verbose=True) @kamodofy(units='m', arg_units={'x': 'kg'}) def a(x): return x kamodo['a'] = a kamodo['b(y[cm])[km]'] = 'y' kamodo['c(x,y)[km]'] = '2*a + 3*b' assert kamodo.c.meta['arg_units']['x'] == str(get_abbrev(get_unit('kg'))) assert kamodo.c.meta['arg_units']['y'] == str(get_abbrev(get_unit('cm'))) result = 2*(3)/1000 + 3*(3) assert kamodo.c(3, 3) == result
def test_Kamodo_composition(): # f, g = symbols('f g', cls=UndefinedFunction) # x = Symbol('x') kamodo = Kamodo(f="$x^2$", verbose=True) kamodo['g(x)'] = "$f(x) + x^2$" kamodo['h(x)'] = 'g**2 + x**2' try: assert kamodo.f(3) == 3 ** 2 assert kamodo.g(3) == 3 ** 2 + 3 ** 2 assert kamodo.h(3) == (3 ** 2 + 3 ** 2) ** 2 + 3 ** 2 except: print(kamodo) raise
def test_scatter_plot_pd(): kamodo = Kamodo(f='x**3') r = pd.DataFrame({'x': np.linspace(0, 1, 100), 'y': np.linspace(-1, 1, 100), 'z': np.linspace(-1, 0, 100)}) results = kamodo.evaluate('f', x=r) title = '$f{\\left(x \\right)} = x^{3}$' titles = dict(variable='f', title=title, title_lhs='f', title_short='f') [trace], plot_type, layout = scatter_plot(results, titles, True) assert trace['mode'] == 'markers' assert plot_type == '3d-scatter' assert layout['title']['text'] == title
def test_to_latex(): kamodo = Kamodo(f='x**2', verbose=True) assert str(kamodo.to_latex()) == r'\begin{equation}f{\left(x \right)} = x^{2}\end{equation}' kamodo = Kamodo(g='x', verbose=True) assert str(kamodo.to_latex()) == r'\begin{equation}g{\left(x \right)} = x\end{equation}' kamodo['f(x[cm])[kg]'] = 'x**2' kamodo['g'] = kamodofy(lambda x: x**2, units='kg', arg_units=dict(x='cm'), equation='$x^2$') kamodo.to_latex()
def test_coutour_plot_xy_index_2d_grid(): def f_NcommaM(x_N=np.linspace(0, 8 * np.pi, 100), y_M=np.linspace(0, 5, 100)): x, y = np.meshgrid(x_N, y_M, indexing='xy') return np.sin(x) * y title = '$\\operatorname{f_{N,M}}{\\left(x_{N},y_{M} \\right)} = \\lambda{\\left(x_{N},y_{M} \\right)}$' kamodo = Kamodo(f_NcommaM=f_NcommaM) results = kamodo.evaluate('f_NcommaM') titles = dict(variable='f_NcommaM', title=title) traces, chart_type, layout = contour_plot(results, titles, 'xy', True) assert chart_type == '2d-grid' assert layout['title']['text'] == title
def test_arg_shape_pd(): def fvec(t): x = t * np.sin(t) ** 3 y = t * np.cos(t) * 2 return pd.DataFrame(dict(X_GSE=x, Y_GSE=y)) kamodo = Kamodo(fvec=fvec) results = kamodo.evaluate('fvec', t=np.linspace(-np.pi, np.pi, 100)) f = results['fvec'] shape = get_arg_shapes(f) assert type(f) == pd.DataFrame assert shape == [(100, 2)]
def test_multi_unit_composition(): kamodo = Kamodo('a(x[s])[km] = x', verbose=True) kamodo['b(x[cm])[g]'] = 'x' kamodo['c'] = 'b(a)' print(kamodo.c.meta) assert kamodo.c.meta['units'] == 'g' assert kamodo.c.meta['arg_units']['x'] == str(get_abbrev(get_unit('s')))
def test_compose_unit_raises(): with pytest.raises(NameError): kamodo = Kamodo('a(x[kg])[m] = x', 'b(y[cm])[km] = y', verbose=True) # this should fail since x is registered with kg kamodo['c(x[cm],y[cm])[km]'] = '2*a + 3*b'
def test_kamodofy_update_attribute(): @kamodofy(units='cm', update='y') def f(x): return x # pragma: no cover kamodo = Kamodo(f=f) assert f.update == 'y'
def test_komodofy_register(): @kamodofy(units='kg/cm^3') def my_density(x, y, z): return x + y + z kamodo = Kamodo(rho=my_density) assert kamodo.rho.meta['units'] == 'kg/cm^3'