class ObsCube(object): """ A wrapping class of SpectralCube which prepares observational data cubes to be compared to any other data cube. Parameters ---------- cube : str Path to file. mask : numpy.ndarray, any mask class from spectral_cube, optional Mask to be applied to the cube. algorithm : {NAME HERE}, optional Name of the cleaning algorithm to use. Example ------- ``` from turbustat.cube_tools import ObsCube cube = ObsCube("data.fits") cube.apply_cleaning(algorithm="SUPERAWESOMECLEANING") ``` """ def __init__(self, cube, mask=None, algorithm=None, beam=None): raise NotImplementedError("ObsCube is not yet implemented for " "general use.") super(ObsCube, self).__init__() self.cube = SpectralCube.read(cube) self.algorithm = algorithm # Make sure mask is an accepted type if mask is not None: _check_mask(mask) self.mask = mask if beam is not None: _check_beam(beam) self.noise = Noise(self.cube, beam=beam) def clean_cube(self, algorithm=None): raise NotImplementedError("") def apply_mask(self, mask=None): ''' Check if the given mask is acceptable abd apply to SpectralCube. ''' # Update mask if mask is not None: _check_mask(mask) self.mask = mask # Create the mask, auto masking nan values default_mask = np.isfinite(self.cube.filled_data[:]) if self.mask is not None: self.mask = CompositeMask(default_mask, self.mask) else: self.mask = default_mask # Apply mask to spectral cube object self.cube = self.cube.with_mask(mask) return self def _update(self, data=None, wcs=None, beam=None, method="MAD"): ''' Helper function to update classes. ''' # Check if we need a new SpectralCube if data is None and wcs is None: pass else: if data is None: data = self.cube.unmasked_data[:] if wcs is None: wcs = self.cube.wcs # Make new SpectralCube object self.cube = SpectralCube(data=data, wcs=wcs) if beam is not None: _check_beam(beam) self.noise = Noise(self.cube, beam=beam, method=method) def compute_properties(self): ''' Use SpectralCube to compute the moments. Also compute the integrated intensity based on the noise properties from Noise. ''' self._moment0 = self.cube.moment0().value self._moment1 = self.cube.moment1().value self._moment2 = self.cube.moment2().value _get_int_intensity(self) return self @property def moment0(self): return self._moment0 @property def moment1(self): return self._moment1 @property def moment2(self): return self._moment2 @property def intint(self): return self._intint def prep(self, mask=None, algorithm=None): ''' Prepares the cube to be compared to another cube. ''' if not mask is None: self.apply_mask() self.clean_cube() self.compute_properties() return self
xarr = np.linspace(50, 70, 41) # km/s # Define a line width, which will vary across our image # It will increase from 1 km/s to 4 km/s over the X-direction (RA) sigma = np.outer(np.linspace(1, 1.5, 2), np.ones(4)).T # Define a line center, which will vary in the opposite direction, # along increasing Y-direction (declination) centroid = np.outer(np.ones(2), np.linspace(58, 62, 4)).T data = np.exp(-(np.tile(xarr, (2, 4, 1)).T - centroid)**2 / (2. * sigma**2)) cube = SpectralCube(data=data, wcs=mywcs) # Sanity checks: do the moments accurately recover the inputs? assert (np.abs(cube.moment1().to(u.km / u.s).value - centroid).max()) < 1e-5 assert (np.abs(cube.moment2().to(u.km**2 / u.s**2).value - sigma**2).max()) < 1e-5 # Create a pyspeckit cube pcube = pyspeckit.Cube(cube=cube) # For convenience, convert the X-axis to km/s # (WCSLIB automatically converts to m/s even if you give it km/s) pcube.xarr.convert_to_unit(u.km / u.s) # Set up the fitter by doing a preliminary fit pcube.specfit(fittype='gaussian', guesses='moments') # Fit each spectrum with a gaussian # First, assemble the guesses: guesses = np.array([
class SimCube(object): ''' A wrapping class to prepare a simulated spectral data cube for comparison with another cube. ''' def __init__(self, cube, beam=None, mask=None, method="MAD", compute=True): # Initialize cube object self.cube = SpectralCube.read(cube) if mask is not None: _check_mask(mask) self.mask = mask if beam is not None: _check_beam(mask) # Initialize noise object self.noise = Noise(self.cube, beam=beam, method=method) def add_noise(self): ''' Use Noise to add synthetic noise to the data. Then update SpectralCube. ''' # Create the noisy cube self.noise.get_noise_cube() noise_data = self.noise.noise_cube +\ self.cube.filled_data[:] # Update SpectralCube object self._update(data=noise_data) return self def clean_cube(self, algorithm=None): raise NotImplementedError("") def apply_mask(self, mask=None): ''' Check if the given mask is acceptable abd apply to SpectralCube. ''' # Update mask if mask is not None: _check_mask(mask) self.mask = mask # Create the mask, auto masking nan values default_mask = np.isfinite(self.cube.filled_data[:]) if self.mask is not None: self.mask = CompositeMask(default_mask, self.mask) else: self.mask = default_mask # Apply mask to spectral cube object self.cube = self.cube.with_mask(mask) return self def _update(self, data=None, wcs=None, beam=None, method="MAD"): ''' Helper function to update classes. ''' # Check if we need a new SpectralCube if data is None and wcs is None: pass else: if data is None: data = self.cube.unmasked_data[:] if wcs is None: wcs = self.cube.wcs # Make new SpectralCube object self.cube = SpectralCube(data=data, wcs=wcs) if beam is not None: _check_beam(beam) self.noise = Noise(self.cube, beam=beam, method=method) def compute_properties(self): ''' Use SpectralCube to compute the moments. Also compute the integrated intensity based on the noise properties from Noise. ''' self._moment0 = self.cube.moment0().value self._moment1 = self.cube.moment1().value self._moment2 = self.cube.moment2().value _get_int_intensity(self) return self @property def moment0(self): return self._moment0 @property def moment1(self): return self._moment1 @property def moment2(self): return self._moment2 @property def intint(self): return self._intint def sim_prep(self, mask=None): ''' Prepares the cube when being compared to another simulation. This entails: * Optionally applying a mask to the data. * Computing the cube's property arrays ''' if not mask is None: self.apply_mask() self.compute_properties() return self def obs_prep(self, mask=None): ''' Prepares the cube when being compared to observational data. This entails: * Optionally applying a mask to the data. * Add synthetic noise based on the cube's properties. * Computing the cube's property arrays ''' if not mask is None: self.apply_mask() self.add_noise() self.compute_properties() return self
xarr = np.linspace(50, 70, 41) # km/s # Define a line width, which will vary across our image # It will increase from 1 km/s to 4 km/s over the X-direction (RA) sigma = np.outer(np.linspace(1,1.5,2), np.ones(4)).T # Define a line center, which will vary in the opposite direction, # along increasing Y-direction (declination) centroid = np.outer(np.ones(2), np.linspace(58, 62, 4)).T data = np.exp(-(np.tile(xarr, (2, 4, 1)).T - centroid)**2 / (2.*sigma**2)) cube = SpectralCube(data=data, wcs=mywcs) # Sanity checks: do the moments accurately recover the inputs? assert (np.abs(cube.moment1().to(u.km/u.s).value - centroid).max()) < 1e-5 assert (np.abs(cube.moment2().to(u.km**2/u.s**2).value - sigma**2).max()) < 1e-5 # Create a pyspeckit cube pcube = pyspeckit.Cube(cube=cube) # For convenience, convert the X-axis to km/s # (WCSLIB automatically converts to m/s even if you give it km/s) pcube.xarr.convert_to_unit(u.km/u.s) # Set up the fitter by doing a preliminary fit pcube.specfit(fittype='gaussian', guesses='moments') # Fit each spectrum with a gaussian # First, assemble the guesses: guesses = np.array([cube.max(axis=0).value, cube.moment1(axis=0).to(u.km/u.s).value,
class ObsCube(object): """ A wrapping class of SpectralCube which prepares observational data cubes to be compared to any other data cube. Parameters ---------- cube : str Path to file. mask : numpy.ndarray, any mask class from spectral_cube, optional Mask to be applied to the cube. algorithm : {NAME HERE}, optional Name of the cleaning algorithm to use. Example ------- ``` from turbustat.cube_tools import ObsCube cube = ObsCube("data.fits") cube.apply_cleaning(algorithm="SUPERAWESOMECLEANING") ``` """ def __init__(self, cube, mask=None, algorithm=None, beam=None): super(ObsCube, self).__init__() self.cube = sc.SpectralCube.read(cube) self.algorithm = algorithm # Make sure mask is an accepted type if mask is not None: _check_mask(mask) self.mask = mask if beam is not None: _check_beam(beam) self.noise = Noise(self.cube, beam=beam) def clean_cube(self, algorithm=None): raise NotImplementedError("") def apply_mask(self, mask=None): ''' Check if the given mask is acceptable abd apply to SpectralCube. ''' # Update mask if mask is not None: _check_mask(mask) self.mask = mask # Create the mask, auto masking nan values default_mask = np.isfinite(self.cube.filled_data[:]) if self.mask is not None: self.mask = CompositeMask(default_mask, self.mask) else: self.mask = default_mask # Apply mask to spectral cube object self.cube = self.cube.with_mask(mask) return self def _update(self, data=None, wcs=None, beam=None, method="MAD"): ''' Helper function to update classes. ''' # Check if we need a new SpectralCube if data is None and wcs is None: pass else: if data is None: data = self.cube.unmasked_data[:] if wcs is None: wcs = self.cube.wcs # Make new SpectralCube object self.cube = SpectralCube(data=data, wcs=wcs) if beam is not None: _check_beam(beam) self.noise = Noise(self.cube, beam=beam, method=method) def compute_properties(self): ''' Use SpectralCube to compute the moments. Also compute the integrated intensity based on the noise properties from Noise. ''' self._moment0 = self.cube.moment0().value self._moment1 = self.cube.moment1().value self._moment2 = self.cube.moment2().value _get_int_intensity(self) return self @property def moment0(self): return self._moment0 @property def moment1(self): return self._moment1 @property def moment2(self): return self._moment2 @property def intint(self): return self._intint def prep(self, mask=None, algorithm=None): ''' Prepares the cube to be compared to another cube. ''' if not mask is None: self.apply_mask() self.clean_cube() self.compute_properties() return self