def extract_attribute(self, cls, attr_name): """ Returns an attribute of a class and its children profiles in the the galaxy as a `ValueIrregular` or `Grid2DIrregular` object. For example, if a galaxy has two light profiles and we want the `LightProfile` axis-ratios, the following: `galaxy.extract_attribute(cls=LightProfile, name="axis_ratio"` would return: ValuesIrregular(values=[axis_ratio_0, axis_ratio_1]) If a galaxy has three mass profiles and we want the `MassProfile` centres, the following: `galaxy.extract_attribute(cls=MassProfile, name="centres"` would return: GridIrregular2D(grid=[(centre_y_0, centre_x_0), (centre_y_1, centre_x_1), (centre_y_2, centre_x_2)]) This is used for visualization, for example plotting the centres of all light profiles colored by their profile. """ if isinstance(self, cls): if hasattr(self, attr_name): attribute = getattr(self, attr_name) if isinstance(attribute, float): return values.ValuesIrregular(values=[attribute]) if isinstance(attribute, tuple): return grid_2d_irregular.Grid2DIrregular(grid=[attribute])
def ellipticities(self) -> values.ValuesIrregular: """ If we treat this vector field as a set of weak lensing shear measurements, the galaxy ellipticity each vector corresponds too. """ return values.ValuesIrregular(values=np.sqrt(self[:, 0]**2 + self[:, 1]**2.0))
def phis(self) -> values.ValuesIrregular: """ If we treat this vector field as a set of weak lensing shear measurements, the position angle defined counter clockwise from the positive x-axis of each galaxy ellipticity that each vector corresponds too. """ return values.ValuesIrregular( values=np.arctan2(self[:, 0], self[:, 1]) * 180.0 / np.pi / 2.0)
def from_dict(cls, dict_: dict) -> "PointSourceDataset": """ Create a point source dataset from a dictionary representation. Parameters ---------- dict_ A dictionary. Arrays are represented as lists or lists of lists. Returns ------- An instance """ return cls( name=dict_["name"], positions=grid_2d_irregular.Grid2DIrregular(dict_["positions"]), positions_noise_map=values.ValuesIrregular( dict_["positions_noise_map"]), fluxes=values.ValuesIrregular(dict_["fluxes"]) if dict_["fluxes"] is not None else None, fluxes_noise_map=values.ValuesIrregular(dict_["fluxes_noise_map"]) if dict_["fluxes_noise_map"] is not None else None, )
def extract_attribute(self, cls, attr_name): """ Returns an attribute of a class in the tracer as a `ValueIrregular` or `Grid2DIrregular` object. For example, if a tracer has an image-plane with a galaxy with two light profiles, the following: `tracer.extract_attribute(cls=LightProfile, name="axis_ratio")` would return: ValuesIrregular(values=[axis_ratio_0, axis_ratio_1]) If the image plane has has two galaxies with two mass profiles and the source plane another galaxy with a mass profile, the following: `tracer.extract_attribute(cls=MassProfile, name="centre")` would return: GridIrregular2D(grid=[(centre_y_0, centre_x_0), (centre_y_1, centre_x_1), (centre_y_2, centre_x_2)]) This is used for visualization, for example plotting the centres of all mass profiles colored by their profile. """ def extract(value, name): try: return getattr(value, name) except (AttributeError, IndexError): return None attributes = [ extract(value, attr_name) for galaxy in self.galaxies for value in galaxy.__dict__.values() if isinstance(value, cls) ] if attributes == []: return None elif isinstance(attributes[0], float): return values.ValuesIrregular(values=attributes) elif isinstance(attributes[0], tuple): return grid_2d_irregular.Grid2DIrregular(grid=attributes)
def extract_attribute(self, cls, attr_name): """ Returns an attribute of a class and its children profiles in the the galaxy as a `ValueIrregular` or `Grid2DIrregular` object. For example, if a galaxy has two light profiles and we want the `LightProfile` axis-ratios, the following: `galaxy.extract_attribute(cls=LightProfile, name="axis_ratio"` would return: ValuesIrregular(values=[axis_ratio_0, axis_ratio_1]) If a galaxy has three mass profiles and we want the `MassProfile` centres, the following: `galaxy.extract_attribute(cls=MassProfile, name="centres"` would return: GridIrregular2D(grid=[(centre_y_0, centre_x_0), (centre_y_1, centre_x_1), (centre_y_2, centre_x_2)]) This is used for visualization, for example plotting the centres of all light profiles colored by their profile. """ def extract(value, name): try: return getattr(value, name) except (AttributeError, IndexError): return None attributes = [ extract(value, attr_name) for value in self.__dict__.values() if isinstance(value, cls) ] attributes = list(filter(None, attributes)) if attributes == []: return None elif isinstance(attributes[0], float): return values.ValuesIrregular(values=attributes) elif isinstance(attributes[0], tuple): return grid_2d_irregular.Grid2DIrregular(grid=attributes)
def __init__(self, fluxes, noise_map, positions, tracer): self.positions = positions self.magnifications = abs( tracer.magnification_via_hessian_from_grid(grid=positions)) flux = tracer.extract_attribute(cls=ps.PointSourceFlux, name="flux")[0] model_fluxes = values.ValuesIrregular(values=[ magnification * flux for magnification in self.magnifications ]) super().__init__( data=fluxes, noise_map=noise_map, model_data=model_fluxes, mask=None, inversion=None, )
def semi_minor_axes(self) -> values.ValuesIrregular: """ If we treat this vector field as a set of weak lensing shear measurements, the semi-minor axis of each galaxy ellipticity that each vector corresponds too. """ return values.ValuesIrregular(values=3 * (1 - self.ellipticities))
def values_from_array_slim(self, array_slim): """Create a *ValuesIrregular* object from a 1D NumPy array of values of shape [total_coordinates]. The *ValuesIrregular* are structured following this `Grid2DIrregular` instance.""" return values.ValuesIrregular(values=array_slim)