示例#1
0
    def test_release(self):

        cube = marvin.tools.cube.Cube(plateifu=self.plateifu)
        spaxel = Spaxel(x=15, y=16, cube=cube)

        self.assertEqual(spaxel.release, 'MPL-4')

        with self.assertRaises(MarvinError) as ee:
            spaxel.release = 'a'
            self.assertIn('the release cannot be changed', str(ee.exception))
示例#2
0
    def test_wrong_force_load(self, galaxy):

        x = galaxy.dap['x']
        y = galaxy.dap['y']
        spaxel = Spaxel(x, y, plateifu=galaxy.plateifu, cube=True,
                        maps=False, modelcube=False)

        with pytest.raises(AssertionError) as ee:
            spaxel.load(force='crap')

        assert 'force can only be cube, maps, or modelcube' in str(ee)
示例#3
0
    def _load_data(self, **kwargs):
        """Loads one of the spaxels to get the DAP properties for the binid."""

        assert len(self.spaxel_coords) > 0

        sample_coords = self.spaxel_coords[0]

        if 'plateifu' not in kwargs:
            kwargs['plateifu'] = self._maps.plateifu

        sample_spaxel = Spaxel(x=sample_coords[0],
                               y=sample_coords[1],
                               cube=True,
                               maps=self._maps,
                               modelcube=self._modelcube,
                               load=True,
                               allow_binned=True,
                               **kwargs)

        self.specres = sample_spaxel.specres
        self.specresd = sample_spaxel.specresd
        self.spectrum = sample_spaxel.spectrum
        self.properties = sample_spaxel.properties

        self.model_flux = sample_spaxel.model_flux
        self.redcorr = sample_spaxel.redcorr
        self.model = sample_spaxel.model
        self.emline = sample_spaxel.emline
        self.emline_base = sample_spaxel.emline_base
        self.stellar_continuum = sample_spaxel.stellar_continuum
示例#4
0
    def test_pickling_only_cube_file(self, temp_scratch, galaxy):
        if galaxy.bintype.name != 'SPX':
            pytest.skip("Can't instantiate a Spaxel from a binned Maps.")

        cube = Cube(filename=galaxy.cubepath)
        maps = Maps(filename=galaxy.mapspath)

        spaxel = cube.getSpaxel(1, 3, maps=maps, modelcube=False)

        file = temp_scratch.join('test_spaxel.mpf')

        path_saved = spaxel.save(str(file), overwrite=True)
        assert file.check() is True
        assert os.path.exists(path_saved)

        del spaxel

        spaxel_restored = Spaxel.restore(str(file))
        assert spaxel_restored is not None
        assert isinstance(spaxel_restored, Spaxel)

        assert spaxel_restored._cube is not None
        assert spaxel_restored._cube.data_origin == 'file'
        assert isinstance(spaxel_restored._cube.data, astropy.io.fits.HDUList)

        assert spaxel_restored._maps is not None
        assert spaxel_restored._maps.data_origin == 'file'
        assert isinstance(spaxel_restored._maps.data, astropy.io.fits.HDUList)
示例#5
0
    def test_spaxel_ra_dec(self):

        cube = marvin.tools.cube.Cube(plateifu=self.plateifu)
        spaxel = Spaxel(x=15, y=16, cube=cube)

        self.assertAlmostEqual(spaxel.ra, 232.54512, places=5)
        self.assertAlmostEqual(spaxel.dec, 48.690062, places=5)
示例#6
0
    def _load_spaxels(self, **kwargs):
        """Creates a list of unloaded spaxels for this binid."""

        load_spaxels = kwargs.pop('load_spaxels', False)

        self.spaxel_coords = self._maps.get_bin_spaxels(self.binid,
                                                        only_list=True)

        if len(self.spaxel_coords) == 0:
            raise MarvinError(
                'there are no spaxels associated with binid={0}.'.format(
                    self.binid))
        else:

            if 'plateifu' not in kwargs:
                kwargs['plateifu'] = self._maps.plateifu

            modelcube_for_spaxel = False if not self._modelcube else self._modelcube.get_unbinned(
            )
            self.spaxels = [
                Spaxel(x=cc[0],
                       y=cc[1],
                       cube=True,
                       maps=self._maps.get_unbinned(),
                       modelcube=modelcube_for_spaxel,
                       load=load_spaxels,
                       **kwargs) for cc in self.spaxel_coords
            ]
示例#7
0
    def test_no_cube_maps_false_db(self):

        spaxel = Spaxel(x=15, y=16, plateifu=self.plateifu, maps=False)

        self.assertIsNone(spaxel.maps)
        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) == 0)
示例#8
0
    def test_bad_binid(self):

        spaxel = Spaxel(0, 0, plateifu='8485-1901', cube=True,
                        maps=True, modelcube=True, bintype='HYB10')

        with pytest.raises(MarvinError) as ee:
            spaxel.stellar_vel.bin.get_bin_spaxels()

        assert 'do not correspond to a valid binid' in str(ee)
示例#9
0
    def test_cube_false_no_maps_db(self):

        spaxel = Spaxel(x=15, y=16, plateifu=self.plateifu, cube=False)

        self.assertIsNone(spaxel.cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsNone(spaxel.spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
示例#10
0
    def test_load_false(self):

        spaxel = Spaxel(plateifu=self.plateifu, x=15, y=16, load=False)

        self.assertFalse(spaxel.loaded)
        self.assertTrue(spaxel.cube)
        self.assertTrue(spaxel.maps)
        self.assertTrue(spaxel.modelcube)
        self.assertEqual(len(spaxel.properties), 0)
        self.assertIsNone(spaxel.spectrum)

        spaxel.load()

        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
        self.assertIsInstance(spaxel.properties, DictOfProperties)
示例#11
0
    def test_db_maps_miles(self):

        spaxel = Spaxel(x=15,
                        y=16,
                        cube=False,
                        modelcube=False,
                        maps=True,
                        plateifu=self.plateifu,
                        template_kin='MILES-THIN')
        self.assertEqual(spaxel.maps.template_kin, 'MILES-THIN')
示例#12
0
    def test_cube_object_db(self):

        cube = marvin.tools.cube.Cube(plateifu=self.plateifu)
        spaxel = Spaxel(x=15, y=16, cube=cube)

        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
示例#13
0
    def test_force_load(self, galaxy, force):

        x = galaxy.dap['x']
        y = galaxy.dap['y']
        spaxel = Spaxel(x, y, plateifu=galaxy.plateifu, cube=True,
                        maps=False, modelcube=False)

        assert spaxel.cube_quantities is not None
        assert spaxel.maps_quantities == {}
        assert spaxel.modelcube_quantities == {}

        spaxel.load(force=force)

        if force == 'cube':
            assert spaxel.cube_quantities is not None
        elif force == 'maps':
            assert spaxel.maps_quantities is not None
        elif force == 'modelcube':
            assert spaxel.modelcube_quantities is not None
示例#14
0
    def test_getattr(self, galaxy):

        x = galaxy.dap['x']
        y = galaxy.dap['y']

        spaxel = Spaxel(x, y, plateifu='8485-1901', cube=True,
                        maps=True, modelcube=True)

        assert spaxel.flux is not None
        assert spaxel.emline_gflux_ha_6564 is not None
        assert spaxel.binned_flux is not None
示例#15
0
    def test_api_maps_invalid_template(self):

        with self.assertRaises(AssertionError) as cm:
            Spaxel(x=15,
                   y=16,
                   cube=False,
                   modelcube=False,
                   maps=True,
                   plateifu=self.plateifu,
                   template_kin='MILES-TH')
        self.assertIn('invalid template_kin', str(cm.exception))
示例#16
0
    def test_cube_maps_object_filename(self):

        cube = marvin.tools.cube.Cube(filename=self.filename_cube)
        maps = marvin.tools.maps.Maps(filename=self.filename_maps_default)
        spaxel = Spaxel(x=15, y=16, cube=cube, maps=maps)

        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
示例#17
0
    def test_fails_unbinned_maps(self):

        maps = marvin.tools.maps.Maps(plateifu=self.plateifu,
                                      bintype='VOR10',
                                      release='MPL-5')

        with self.assertRaises(MarvinError) as cm:
            Spaxel(x=15, y=16, plateifu=self.plateifu, maps=maps)

        self.assertIn('cannot instantiate a Spaxel from a binned Maps.',
                      str(cm.exception))
示例#18
0
    def test_load_all(self):

        set_the_config('MPL-6')
        spaxel = Spaxel(26, 13, plateifu='8485-1901', cube=True,
                        maps=True, modelcube=True, bintype='HYB10', release='MPL-6')

        assert isinstance(spaxel, Spaxel)

        bin_spaxels = spaxel.stellar_vel.bin.get_bin_spaxels(lazy=False)

        assert len(bin_spaxels) > 0
        assert bin_spaxels[0].loaded is True
示例#19
0
    def test_cube_maps_object_api(self):

        cube = marvin.tools.cube.Cube(plateifu=self.plateifu, mode='remote')
        maps = marvin.tools.maps.Maps(plateifu=self.plateifu, mode='remote')
        spaxel = Spaxel(x=15, y=16, cube=cube, maps=maps)

        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
        self.assertIsInstance(spaxel.properties, DictOfProperties)
示例#20
0
    def test_dir(self, galaxy):

        x = galaxy.dap['x']
        y = galaxy.dap['y']

        spaxel = Spaxel(x, y, plateifu='8485-1901', cube=True,
                        maps=True, modelcube=True)

        dir_list = dir(spaxel)

        assert 'flux' in dir_list
        assert 'emline_gflux_ha_6564' in dir_list
        assert 'binned_flux' in dir_list
示例#21
0
    def test_files_maps(self, galaxy):

        x = galaxy.dap['x']
        y = galaxy.dap['y']

        spaxel = Spaxel(x, y,
                        cube=False,
                        maps=galaxy.mapspath,
                        modelcube=False)

        assert isinstance(spaxel, Spaxel)

        assert not isinstance(spaxel._cube, Cube)
        assert isinstance(spaxel._maps, Maps)
        assert not isinstance(spaxel._modelcube, ModelCube)
示例#22
0
    def test_cube_maps_object_filename_mpl5(self):

        marvin.config.setMPL('MPL-5')

        cube = marvin.tools.cube.Cube(filename=self.filename_cube)
        maps = marvin.tools.maps.Maps(filename=self.filename_maps_default)
        spaxel = Spaxel(x=15, y=16, cube=cube, maps=maps)

        self.assertEqual(cube._drpver, 'v1_5_1')
        self.assertEqual(spaxel._drpver, 'v1_5_1')
        self.assertEqual(maps._drpver, 'v1_5_1')
        self.assertEqual(maps._dapver, '1.1.1')
        self.assertEqual(spaxel._dapver, '1.1.1')

        self.assertIsInstance(spaxel.cube, marvin.tools.cube.Cube)
        self.assertIsInstance(spaxel.maps, marvin.tools.maps.Maps)

        self.assertIsInstance(spaxel.spectrum, Spectrum)
        self.assertTrue(len(spaxel.properties) > 0)
示例#23
0
    def test_pickling_data(self, temp_scratch, galaxy):

        drpver, __ = config.lookUpVersions()

        maps = Maps(filename=galaxy.mapspath)
        modelcube = ModelCube(filename=galaxy.modelpath)
        spaxel = maps.getSpaxel(25, 15, xyorig='lower', drp=False, models=modelcube)

        file = temp_scratch.join('test_spaxel.mpf')

        path_saved = spaxel.save(str(file), overwrite=True)
        assert file.check() is True
        assert os.path.exists(path_saved)

        del spaxel

        spaxel_restored = Spaxel.restore(str(file))

        assert spaxel_restored.stellar_vel.value is not None
        assert spaxel_restored.stellar_vel.bin.binid is not None
示例#24
0
def _getSpaxel(name, x, y, **kwargs):
    """Retrieves a Marvin Spaxel object."""

    spaxel = None
    results = {}

    # Pop the release to remove a duplicate input to Maps
    release = kwargs.pop('release', None)

    # parse name into either mangaid or plateifu
    try:
        idtype = parseIdentifier(name)
    except Exception as e:
        results['error'] = 'Failed to parse input name {0}: {1}'.format(
            name, str(e))
        return spaxel, results

    try:
        if idtype == 'plateifu':
            plateifu = name
            mangaid = None
        elif idtype == 'mangaid':
            mangaid = name
            plateifu = None
        else:
            raise MarvinError(
                'invalid plateifu or mangaid: {0}'.format(idtype))

        spaxel = Spaxel(x=x,
                        y=y,
                        mangaid=mangaid,
                        plateifu=plateifu,
                        release=release,
                        **kwargs)
        results['status'] = 1
    except Exception as e:
        results['error'] = 'Failed to retrieve Spaxels {0}: {1}'.format(
            name, str(e))

    return spaxel, results
示例#25
0
    def get_bin_spaxels(self, lazy=True):
        """Returns a list of the spaxels associated with this bin.

        Parameters
        ----------
        lazy : bool
            If ``True``, the spaxels returned will be lazy loaded. Spaxels
            can be fully loaded by calling their `~.Spaxel.load` method.

        Returns
        -------
        spaxels : list
            A list of all the `.Spaxel` instances associated with this
            quantity binid.

        """

        if self.binid < 0:
            raise marvin.core.exceptions.MarvinError(
                'coordinates ({}, {}) do not correspond to a valid binid.'.
                format(self._spaxel.x, self._spaxel.y))

        spaxel_coords = zip(*numpy.where(self.binid_map.value == self.binid))
        spaxels = []

        for ii, jj in spaxel_coords:
            spaxels.append(
                Spaxel(x=jj,
                       y=ii,
                       plateifu=self._spaxel.plateifu,
                       release=self._spaxel.release,
                       cube=self._spaxel._cube,
                       maps=self._spaxel._maps,
                       modelcube=self._spaxel._modelcube,
                       bintype=self._spaxel.bintype,
                       template=self._spaxel.template,
                       lazy=lazy))

        return spaxels
示例#26
0
    def test_pickling_all_api(self, temp_scratch, galaxy):
        drpver, __ = config.lookUpVersions()

        cube = Cube(plateifu=galaxy.plateifu, mode='remote')
        maps = Maps(plateifu=galaxy.plateifu, mode='remote')
        modelcube = ModelCube(plateifu=galaxy.plateifu, mode='remote')
        spaxel = cube.getSpaxel(1, 3, maps=maps, modelcube=modelcube)

        assert spaxel._cube.data_origin == 'api'
        assert spaxel._maps.data_origin == 'api'
        assert spaxel._modelcube.data_origin == 'api'

        file = temp_scratch.join('test_spaxel_api.mpf')

        path_saved = spaxel.save(str(file), overwrite=True)
        assert file.check() is True
        assert os.path.exists(path_saved)

        del spaxel

        spaxel_restored = Spaxel.restore(str(file))
        assert spaxel_restored is not None
        assert isinstance(spaxel_restored, Spaxel)

        assert spaxel_restored._cube is not None
        assert isinstance(spaxel_restored._cube, Cube)
        assert spaxel_restored._cube.data_origin == 'api'
        assert spaxel_restored._cube.data is None
        assert spaxel_restored._cube.header['VERSDRP3'] == drpver

        assert spaxel_restored._maps is not None
        assert isinstance(spaxel_restored._maps, Maps)
        assert spaxel_restored._maps.data_origin == 'api'
        assert spaxel_restored._maps.data is None

        assert spaxel_restored._modelcube is not None
        assert isinstance(spaxel_restored._modelcube, ModelCube)
        assert spaxel_restored._modelcube.data_origin == 'api'
        assert spaxel_restored._modelcube.data is None
示例#27
0
    def test_files_modelcube(self, galaxy):

        x = galaxy.dap['x']
        y = galaxy.dap['y']

        if galaxy.release == 'MPL-4':
            pytest.skip()
        else:
            modelcube_filename = galaxy.modelpath

        spaxel = Spaxel(x, y,
                        cube=False,
                        maps=False,
                        modelcube=modelcube_filename)

        assert isinstance(spaxel, Spaxel)

        assert not isinstance(spaxel._cube, Cube)
        assert not isinstance(spaxel._maps, Maps)

        if galaxy.release != 'MPL-4':
            assert isinstance(spaxel._modelcube, ModelCube)
示例#28
0
    def test_no_inputs(self):

        with pytest.raises(MarvinError) as ee:
            Spaxel(0, 0, cube=None, maps=None, modelcube=None)

        assert 'no inputs defined' in str(ee)
示例#29
0
    def test_SpaxelBase(self, galaxy, cube_maps_modelcube_modes):

        plateifu = galaxy.plateifu
        bintype = galaxy.bintype.name
        template = galaxy.template.name
        release = galaxy.release
        x = galaxy.dap['x']
        y = galaxy.dap['y']

        cube, maps, modelcube = cube_maps_modelcube_modes

        if cube == 'object':
            cube = Cube(plateifu=plateifu, release=release)

        if maps == 'object':
            maps = Maps(plateifu=plateifu,
                        bintype=bintype,
                        template=template,
                        release=release)

        if release == 'MPL-4':
            modelcube = False
        elif modelcube == 'object':
            modelcube = ModelCube(plateifu=plateifu,
                                  bintype=bintype,
                                  template=template,
                                  release=release)

        if cube is False and maps is False and modelcube is False:
            pytest.skip()

        spaxel = Spaxel(x,
                        y,
                        plateifu=plateifu,
                        cube=cube,
                        maps=maps,
                        modelcube=modelcube,
                        template=template,
                        bintype=bintype)

        assert isinstance(spaxel, Spaxel)

        if (spaxel.bintype is not None and spaxel.bintype.binned is True
                and (spaxel._maps or spaxel._modelcube)):
            assert isinstance(spaxel, Spaxel)
        else:
            assert isinstance(spaxel, Spaxel)

        if spaxel._cube:
            assert len(spaxel.cube_quantities) > 0
        else:
            assert len(spaxel.cube_quantities) == 0

        if spaxel._maps:
            assert len(spaxel.maps_quantities) > 0
        else:
            assert len(spaxel.maps_quantities) == 0

        if spaxel._modelcube:
            assert len(spaxel.modelcube_quantities) > 0
        else:
            assert len(spaxel.modelcube_quantities) == 0

        assert spaxel.plateifu == galaxy.plateifu
        assert spaxel.mangaid == galaxy.mangaid

        assert isinstance(spaxel.getCube(), Cube)
        assert isinstance(spaxel.getMaps(), Maps)

        if release != 'MPL-4':
            assert isinstance(spaxel.getModelCube(), ModelCube)
示例#30
0
    def convertToTool(self, tooltype, **kwargs):
        ''' Converts the list of results into Marvin Tool objects

            Creates a list of Marvin Tool objects from a set of query results.
            The new list is stored in the Results.objects property.
            If the Query.returntype parameter is specified, then the Results object
            will automatically convert the results to the desired Tool on initialization.

            Parameters:
                tooltype (str):
                    The requested Marvin Tool object that the results are converted into.
                    Overrides the returntype parameter.  If not set, defaults
                    to the returntype parameter.
                limit (int):
                    Limit the number of results you convert to Marvin tools.  Useful
                    for extremely large result sets.  Default is None.
                mode (str):
                    The mode to use when attempting to convert to Tool. Default mode
                    is to use the mode internal to Results. (most often remote mode)

            Example:
                >>> # Get the results from some query
                >>> r = q.run()
                >>> r.results
                >>> [NamedTuple(mangaid=u'14-12', name=u'1901', nsa.z=-9999.0),
                >>>  NamedTuple(mangaid=u'14-13', name=u'1902', nsa.z=-9999.0),
                >>>  NamedTuple(mangaid=u'27-134', name=u'1901', nsa.z=-9999.0),
                >>>  NamedTuple(mangaid=u'27-100', name=u'1902', nsa.z=-9999.0),
                >>>  NamedTuple(mangaid=u'27-762', name=u'1901', nsa.z=-9999.0)]

                >>> # convert results to Marvin Cube tools
                >>> r.convertToTool('cube')
                >>> r.objects
                >>> [<Marvin Cube (plateifu='7444-1901', mode='remote', data_origin='api')>,
                >>>  <Marvin Cube (plateifu='7444-1902', mode='remote', data_origin='api')>,
                >>>  <Marvin Cube (plateifu='7995-1901', mode='remote', data_origin='api')>,
                >>>  <Marvin Cube (plateifu='7995-1902', mode='remote', data_origin='api')>,
                >>>  <Marvin Cube (plateifu='8000-1901', mode='remote', data_origin='api')>]

        '''

        # set the desired tool type
        mode = kwargs.get('mode', self.mode)
        limit = kwargs.get('limit', None)
        toollist = ['cube', 'spaxel', 'maps', 'rss', 'modelcube']
        tooltype = tooltype if tooltype else self.returntype
        assert tooltype in toollist, 'Returned tool type must be one of {0}'.format(toollist)

        # get the parameter list to check against
        paramlist = self.paramtocol.keys()

        print('Converting results to Marvin {0} objects'.format(tooltype.title()))
        if tooltype == 'cube':
            self.objects = [Cube(plateifu=res.__getattribute__(
                            self._getRefName('cube.plateifu')),
                            mode=mode) for res in self.results[0:limit]]
        elif tooltype == 'maps':

            isbin = 'bintype.name' in paramlist
            istemp = 'template.name' in paramlist
            self.objects = []

            for res in self.results[0:limit]:
                mapkwargs = {'mode': mode, 'plateifu': res.__getattribute__(self._getRefName('cube.plateifu'))}

                if isbin:
                    binval = res.__getattribute__(self._getRefName('bintype.name'))
                    mapkwargs['bintype'] = binval

                if istemp:
                    tempval = res.__getattribute__(self._getRefName('template.name'))
                    mapkwargs['template_kin'] = tempval

                self.objects.append(Maps(**mapkwargs))
        elif tooltype == 'spaxel':

            assert 'spaxelprop.x' in paramlist and 'spaxelprop.y' in paramlist, \
                    'Parameters must include spaxelprop.x and y in order to convert to Marvin Spaxel.'

            self.objects = []

            load = kwargs.get('load', True)
            maps = kwargs.get('maps', True)
            modelcube = kwargs.get('modelcube', True)

            for res in self.results[0:limit]:
                spaxkwargs = {'plateifu': res.__getattribute__(self._getRefName('cube.plateifu')),
                              'x': res.__getattribute__(self._getRefName('spaxelprop.x')),
                              'y': res.__getattribute__(self._getRefName('spaxelprop.y')),
                              'load': load, 'maps': maps, 'modelcube': modelcube}
                self.objects.append(Spaxel(**spaxkwargs))
        elif tooltype == 'rss':
            self.objects = [RSS(plateifu=res.__getattribute__(
                            self._getRefName('cube.plateifu')),
                            mode=mode) for res in self.results[0:limit]]
        elif tooltype == 'modelcube':

            isbin = 'bintype.name' in paramlist
            istemp = 'template.name' in paramlist
            self.objects = []

            for res in self.results[0:limit]:
                mapkwargs = {'mode': mode, 'plateifu': res.__getattribute__(self._getRefName('cube.plateifu'))}

                if isbin:
                    binval = res.__getattribute__(self._getRefName('bintype.name'))
                    mapkwargs['bintype'] = binval

                if istemp:
                    tempval = res.__getattribute__(self._getRefName('template.name'))
                    mapkwargs['template_kin'] = tempval

                self.objects.append(ModelCube(**mapkwargs))