示例#1
0
    def test_coordinate_transformations_work_when_z_is_a_scalar(self):
        # This just tests that the transformations work, not that they
        # are in the shape (N, 3), as some of the calculations prefer
        # to leave z as a scalar if it starts as one (e.g. mielens)
        np.random.seed(12)
        x, y = np.random.randn(2, 10)
        z = np.random.randn()

        rho = np.sqrt(x**2 + y**2)
        phi = np.arctan2(y, x)

        versions_to_check = [
            ('cartesian', 'spherical', [x, y, z]),
            ('cartesian', 'cylindrical', [x, y, z]),
            ('cylindrical', 'cartesian', [rho, phi, z]),
            ('cylindrical', 'spherical', [rho, phi, z]),
        ]
        for *version_to_check, coords in versions_to_check:
            method = find_transformation_function(*version_to_check)
            try:
                result = method(coords)
            except:
                msg = '_to_'.join(version_to_check) + ' failed'
                self.assertTrue(False, msg=msg)
        pass
示例#2
0
 def test_find_transformation_function(self):
     desired = [
         ('cartesian', 'spherical', transform_cartesian_to_spherical),
         ('cartesian', 'cylindrical', transform_cartesian_to_cylindrical),
         ('spherical', 'cartesian', transform_spherical_to_cartesian),
         ('cylindrical', 'cartesian', transform_cylindrical_to_cartesian),
         ('spherical', 'cylindrical', transform_spherical_to_cylindrical),
         ('cylindrical', 'spherical', transform_cylindrical_to_spherical),
     ]
     for initial, final, correct_method in desired:
         self.assertTrue(
             find_transformation_function(initial, final) is correct_method)
示例#3
0
 def _transform_to_desired_coordinates(cls, detector, origin, wavevec=1):
     if hasattr(detector, 'theta') and hasattr(detector, 'phi'):
         original_coordinate_system = 'spherical'
         original_coordinate_values = [
             (detector.r.values * wavevec if hasattr(detector, 'r') else
              np.full(detector.theta.values.shape, np.inf)),
             detector.theta.values,
             detector.phi.values,
         ]
     else:
         original_coordinate_system = 'cartesian'
         f = flat(detector)  # 1.6 ms
         original_coordinate_values = [
             wavevec * (f.x.values - origin[0]),
             wavevec * (f.y.values - origin[1]),
             wavevec * (origin[2] - f.z.values),
             # z is defined opposite light propagation, so we invert
         ]
     method = find_transformation_function(original_coordinate_system,
                                           cls.desired_coordinate_system)
     return method(original_coordinate_values)
示例#4
0
 def test_find_transformation_function_when_same(self):
     np.random.seed(12)
     xyz = np.random.randn(3, 10)
     for which in ['cartesian', 'spherical', 'cylindrical']:
         method = find_transformation_function(which, which)
         self.assertTrue(np.allclose(xyz, method(xyz), **TOLS))