def test___new__(self): """Test method ``__new__``.""" # --------------- # Can't have the "key" argument with pytest.raises(ValueError, match="Can't specify 'key'"): self.obj( PotentialWrapper(self.potential), self.meshgrid, key="not None", ) # --------------- # AOK msamp = self.obj( PotentialWrapper(self.potential, frame="icrs"), self.meshgrid, ) assert self.obj is not sample.PotentialSampler assert isinstance(msamp, self.obj) assert isinstance(msamp, sample.PotentialSampler) assert not hasattr(msamp, "_instance") assert msamp._potential == self.potential
def setup_class(cls): """Setup fixtures for testing.""" super().setup_class() nx = ny = nz = 76 # must be int and even nxr0 = nyr0 = nzr0 = 2.3 * 2 X, Y, Z = (np.array( np.meshgrid( np.linspace(-nxr0 / 2, nxr0 / 2, nx), np.linspace(-nyr0 / 2, nyr0 / 2, ny), np.linspace(-nzr0 / 2, nzr0 / 2, nz), indexing="ij", ), ) * 1) XYZ = coord.CartesianRepresentation(X, Y, Z, unit=u.kpc) # THIRD PARTY import galpy.potential as gpot cls.potential = gpot.HernquistPotential() cls.meshgrid = XYZ cls.inst = cls.obj( PotentialWrapper(cls.potential), cls.meshgrid, total_mass=10 * u.solMass, )
def test___init__(self): """Test method ``__init__``.""" with pytest.raises(ValueError, match="mass is divergent"): self.obj( PotentialWrapper(self.potential), meshgrid=self.meshgrid, total_mass=np.inf * u.solMass, )
def test___init__(self): """Test method ``__init__``.""" # run tests on super super().test___init__() if self.obj is not sample.PotentialSampler: with pytest.raises(ValueError, match="mass is divergent"): self.obj( PotentialWrapper(self.potential), total_mass=np.inf * u.solMass, ) # -------------------------- pass # for subclasses. The setup_class actually tests this for here.
def setup_class(cls): """Setup fixtures for testing.""" cls.potential = object() # register a unittest examples class SubClassUnitTest(cls.obj, key="unittest"): def __call__(self, n, *, random=None, **kwargs): # Get preferred frames frame = self.frame representation_type = self.representation_type if random is None: random = np.random elif isinstance(random, int): random = np.random.default_rng(random) # return rep = coord.UnitSphericalRepresentation( lon=random.uniform(size=n) * u.deg, lat=2 * random.uniform(size=n) * u.deg, ) if representation_type is None: representation_type = rep.__class__ sample = coord.SkyCoord( frame.realize_frame( rep, representation_type=representation_type, ), copy=False, ) sample.cache["mass"] = np.ones(n) sample.cache["potential"] = self.potential return sample cls.SubClassUnitTest = SubClassUnitTest # /class # make instance. It depends. if cls.obj is sample.PotentialSampler: cls.inst = cls.obj( PotentialWrapper(cls.potential), key="unittest", total_mass=10 * u.solMass, )
def setup_class(cls): """Set up fixtures for testing.""" cls.mass = 1e12 * u.solMass cls.r0 = 10 * u.kpc # scale factor # sampling a potential that lives in a galactocentric frame # and enforcing a Cartesian representation cls.sampler = PotentialSampler( PotentialWrapper(object(), frame="galactocentric"), key="unittest", frame="galactocentric", representation_type="cartesian", total_mass=10 * u.solMass, ) # but when we measure, it's 1% errors in icrs, Spherical cls.measurer = MeasurementErrorSampler( c_err=1 * u.percent, method=("rvs", "Gaussian"), frame="icrs", representation_type="spherical", ) # fitting is done with an SCF potential cls.fitter = PotentialFitter( object(), key="unittest", frame=cls.sampler.frame, representation_type=cls.sampler.representation_type, ) # the residual function cls.residualer = None # the statistic function cls.statistic = None cls.inst = pipeline.Pipeline( sampler=cls.sampler, measurer=cls.measurer, fitter=cls.fitter, residualer=cls.residualer, statistic=cls.statistic, )
def __call__(self, c, **kwargs): c.represent_as(coord.CartesianRepresentation) return PotentialWrapper(object(), frame=None)
def test___new__(self): """Test method ``__new__``. This is a wrapper class that acts differently when instantiating a MeasurementErrorSampler than one of it's subclasses. """ # there are no tests on super # super().test___new__() # Need the "potential" argument # potential must be a PotentialWrapper with pytest.raises(TypeError, match="must be a PotentialWrapper"): self.obj(object) # -------------------------- if self.obj is sample.PotentialSampler: # --------------- # Need the "potential" argument with pytest.raises(TypeError, match="argument: 'potential'"): self.obj() # -------------------------- # for object not in registry with pytest.raises(ValueError, match="key: builtins"): self.obj(PotentialWrapper(self.potential)) # --------------- # with subclass try: # see if galpy is working key = "galpy" klass = self.obj._registry[key] except KeyError: key, klass = tuple(self.obj._registry.items())[0] potential = self.potential msamp = self.obj( PotentialWrapper(potential), total_mass=10 * u.solMass, key=key, ) else: msamp = self.obj( PotentialWrapper(self.potential), total_mass=10 * u.solMass, df=TestDF, key=key, ) # test class type assert isinstance(msamp, klass) assert isinstance(msamp, self.obj) # test inputs assert msamp._potential == self.potential # --------------- # potential is PotentialWrapper try: key = "galpy" klass = self.obj._registry[key] except KeyError: key, klass = tuple(self.obj._registry.items())[0] potential = self.potential msamp = self.obj( PotentialWrapper(potential), total_mass=10 * u.solMass, key=key, ) else: msamp = self.obj( PotentialWrapper(self.potential), key=key, df=TestDF, total_mass=10 * u.solMass, ) # test class type assert isinstance(msamp, klass) assert isinstance(msamp, self.obj) # test inputs assert msamp._potential == self.potential # -------------------------- else: # never hit in Test_PotentialSampler, only in subs # --------------- # Can't have the "key" argument with pytest.raises(ValueError, match="Can't specify 'key'"): self.obj(PotentialWrapper(self.potential), key="not None") # --------------- # AOK msamp = self.obj(PotentialWrapper(self.potential, frame="icrs")) assert self.obj is not sample.PotentialSampler assert isinstance(msamp, self.obj) assert isinstance(msamp, sample.PotentialSampler) assert not hasattr(msamp, "_instance") assert msamp._potential == self.potential