def test_multiple_getter_with_conversion(): # test N successive coordinate conversions N = 500 # get list of available coordinate systems coords = Coordinates() systems = coords._systems() # get reference points in cartesian coordinate system points = [ 'positive_x', 'positive_y', 'positive_z', 'negative_x', 'negative_y', 'negative_z' ] pts = np.array([systems['cart']['right'][point] for point in points]) # init the system coords.set_cart(pts[:, 0], pts[:, 1], pts[:, 2]) # list of domains domains = list(systems) for ii in range(N): # randomly select a coordinate system domain = domains[np.random.randint(len(domains))] conventions = list(systems[domain]) convention = conventions[np.random.randint(len(conventions))] # convert points to selected system pts = eval(f"coords.get_{domain}('{convention}', convert=True)") # get the reference ref = np.array( [systems[domain][convention][point] for point in points]) # check npt.assert_allclose(pts, ref, atol=1e-15) # print print(f"Tolerance met in iteration {ii}")
def test_coordinates_init_val_no_convention_no_unit(): # get list of available coordinate systems coords = Coordinates() systems = coords._systems() # test constructor with all systems, units, and convention=None for domain in systems: Coordinates(0, 0, 0, domain)
def test_coordinates_init_val_no_convention(): # get list of available coordinate systems coords = Coordinates() systems = coords._systems() # test constructor with all systems, units, and convention=None for domain in systems: convention = list(systems[domain])[0] for unit in systems[domain][convention]['units']: Coordinates(0, 0, 0, domain, unit=unit[0][0:3])
def test_coordinates_init_val_and_system(): # get list of available coordinate systems coords = Coordinates() systems = coords._systems() # test constructor with all systems for domain in systems: for convention in systems[domain]: for unit in systems[domain][convention]['units']: Coordinates(0, 0, 0, domain, convention, unit[0][0:3])
def test_setter_and_getter_with(): # get list of available coordinate systems coords = Coordinates() systems = coords._systems() # test points contained in system definitions points = [ 'positive_x', 'positive_y', 'positive_z', 'negative_x', 'negative_y', 'negative_z' ] # test setter and getter with all systems and default unit for domain_in in list(systems): for convention_in in list(systems[domain_in]): for domain_out in list(systems): for convention_out in list(systems[domain_out]): for point in points: # for debugging print(f"{domain_in}({convention_in}) -> " f"{domain_out}({convention_out}): {point}") # in and out points p_in = systems[domain_in][convention_in][point] p_out = systems[domain_out][convention_out][point] # empty object c = Coordinates() # --- set point --- eval(f"c.set_{domain_in}(p_in[0], p_in[1], p_in[2], \ '{convention_in}')") # check point p = c._points npt.assert_allclose(p.flatten(), p_in, atol=1e-15) # --- test without conversion --- p = eval(f"c.get_{domain_out}('{convention_out}')") # check internal and returned point npt.assert_allclose(c._points.flatten(), p_in, atol=1e-15) npt.assert_allclose(p.flatten(), p_out, atol=1e-15) # check if system was converted assert c._system["domain"] == domain_in assert c._system["convention"] == convention_in # --- test with conversion --- p = eval(f"c.get_{domain_out}('{convention_out}', \ convert=True)") # check point npt.assert_allclose(p.flatten(), p_out, atol=1e-15) # check if system was converted assert c._system["domain"] == domain_out assert c._system["convention"] == convention_out
def test_coordinate_names(): # check if units agree across coordinates that appear more than once # get all coordinate systems c = Coordinates() systems = c._systems() # get unique list of coordinates and their properties coords = {} # loop across domains and conventions for domain in systems: for convention in systems[domain]: # loop across coordinates for cc, coord in enumerate( systems[domain][convention]['coordinates']): # units of the current coordinate cur_units = [ u[cc] for u in systems[domain][convention]['units'] ] # add coordinate to coords if coord not in coords: coords[coord] = {} coords[coord]['domain'] = [domain] coords[coord]['convention'] = [convention] coords[coord]['units'] = [cur_units] else: coords[coord]['domain'].append(domain) coords[coord]['convention'].append(convention) coords[coord]['units'].append(cur_units) # check if units agree across coordinates that appear more than once for coord in coords: # get unique first entry units = coords[coord]['units'].copy() units_ref, idx = np.unique(units[0], True) units_ref = units_ref[idx] for cc in range(1, len(units)): # get nex entry for comparison units_test, idx = np.unique(units[cc], True) units_test = units_test[idx] # compare assert all(units_ref == units_test), \ f"'{coord}' has units {units_ref} in "\ f"{coords[coord]['domain'][0]} "\ f"({coords[coord]['convention'][0]}) but units {units_test} "\ f"in {coords[coord]['domain'][cc]} "\ f"({coords[coord]['convention'][cc]})"
def test__systems(): # get all coordinate systems coords = Coordinates() systems = coords._systems() # check object type assert isinstance(systems, dict) # check completeness of systems for domain in systems: for convention in systems[domain]: assert "description_short" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'description_short'" assert "coordinates" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'coordinates'" assert "units" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'units'" assert "description" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'description'" assert "positive_x" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'positive_x'" assert "positive_y" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'positive_y'" assert "negative_x" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'negative_'" assert "negative_y" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'negative_y'" assert "positive_z" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'positive_z'" assert "negative_z" in systems[domain][convention], \ f"{domain} ({convention}) is missing entry 'negative_z'" for coord in systems[domain][convention]['coordinates']: assert coord in systems[domain][convention], \ f"{domain} ({convention}) is missing entry '{coord}'" assert systems[domain][convention][coord][0] in \ ["unbound", "bound", "cyclic"], \ f"{domain} ({convention}), {coord}[0] must be 'unbound', "\ "'bound', or 'cyclic'."