def test_pickling_db(self, galaxy, temp_scratch): modelcube = ModelCube(plateifu=galaxy.plateifu, bintype=galaxy.bintype) file = temp_scratch.join('test_modelcube_db.mpf') with pytest.raises(MarvinError) as cm: modelcube.save(str(file)) assert 'objects with data_origin=\'db\' cannot be saved.' in str( cm.value)
def test_pickling_db(self): modelcube = ModelCube(plateifu=self.plateifu) with self.assertRaises(MarvinError) as ee: modelcube.save() self.assertIn('objects with data_origin=\'db\' cannot be saved.', str(ee.exception))
def test_getspaxel_db_only_model(self): model_cube = ModelCube(plateifu=self.plateifu) spaxel = model_cube.getSpaxel(x=1, y=2, properties=False, spectrum=False) self._test_getspaxel(spaxel) self.assertIsNone(spaxel.cube) self.assertIsNone(spaxel.spectrum) self.assertIsNone(spaxel.maps) self.assertEqual(len(spaxel.properties), 0)
def test_getspaxel_matches_file_db_remote(self, galaxy): if galaxy.bintype != 'SPX': pytest.skip() modelcube_file = ModelCube(filename=galaxy.modelpath, bintype=galaxy.bintype, template=galaxy.template, release=galaxy.release) modelcube_db = ModelCube(mangaid=galaxy.mangaid, bintype=galaxy.bintype, template=galaxy.template, release=galaxy.release) modelcube_api = ModelCube(mangaid=galaxy.mangaid, mode='remote', bintype=galaxy.bintype, template=galaxy.template, release=galaxy.release) assert modelcube_file.data_origin == 'file' assert modelcube_db.data_origin == 'db' assert modelcube_api.data_origin == 'api' idx = galaxy.spaxel['specidx'] flux = galaxy.spaxel['model_flux'] ivar = galaxy.spaxel['model_ivar'] mask = galaxy.spaxel['model_mask'] xx_cen = galaxy.spaxel['x_cen'] yy_cen = galaxy.spaxel['y_cen'] try: spaxel_getspaxel_file = modelcube_file.getSpaxel(x=xx_cen, y=yy_cen) spaxel_getspaxel_db = modelcube_db.getSpaxel(x=xx_cen, y=yy_cen) spaxel_getspaxel_api = modelcube_api.getSpaxel(x=xx_cen, y=yy_cen) except MarvinError as ee: assert 'do not correspond to a valid binid' in str(ee) pytest.skip() assert spaxel_getspaxel_file.binned_flux.value[idx] == pytest.approx( flux, abs=1e-6) assert spaxel_getspaxel_db.binned_flux.value[idx] == pytest.approx( flux, abs=1e-6) assert spaxel_getspaxel_api.binned_flux.value[idx] == pytest.approx( flux, abs=1e-6) assert spaxel_getspaxel_file.binned_flux.ivar[idx] == pytest.approx( ivar) assert spaxel_getspaxel_db.binned_flux.ivar[idx] == pytest.approx(ivar) assert spaxel_getspaxel_api.binned_flux.ivar[idx] == pytest.approx( ivar) assert spaxel_getspaxel_file.binned_flux.mask[idx] == pytest.approx( mask) assert spaxel_getspaxel_db.binned_flux.mask[idx] == pytest.approx(mask) assert spaxel_getspaxel_api.binned_flux.mask[idx] == pytest.approx( mask)
def modelcube(galaxy, exporigin, mode): ''' Yield a Marvin ModelCube based on the expected origin combo of (mode+db). Fixture tests 6 modelcube origins from (mode+db) combos [file, db and api] ''' if exporigin == 'file': mc = ModelCube(filename=galaxy.modelpath, release=galaxy.release, mode=mode) else: mc = ModelCube(plateifu=galaxy.plateifu, release=galaxy.release, mode=mode) mc.exporigin = exporigin yield mc mc = None
def test_init_from_api_not_default(self): model_cube = ModelCube(plateifu=self.plateifu, bintype='NRE', mode='remote') self.assertEqual(model_cube.data_origin, 'api') self._test_init(model_cube, bintype='NRE')
def test_get_flux_api_raises_exception(self): model_cube = ModelCube(plateifu=self.plateifu, mode='remote') with self.assertRaises(MarvinError) as err: model_cube.flux self.assertIn('cannot return a full cube in remote mode.', str(err.exception))
def test_raises_exception_mpl4(self): marvin.config.setMPL('MPL-4') with self.assertRaises(MarvinError) as err: ModelCube(plateifu=self.plateifu) self.assertIn('ModelCube requires at least dapver=\'2.0.2\'', str(err.exception))
def test_pickling_file_custom_path(self): modelcube = ModelCube(filename=self.filename) test_path = '~/test.mpf' path = modelcube.save(path=test_path) self._files_created.append(path) self.assertTrue(os.path.exists(path)) self.assertEqual(path, os.path.realpath(os.path.expanduser(test_path))) modelcube_restored = ModelCube.restore(path, delete=True) self.assertEqual(modelcube_restored.data_origin, 'file') self.assertIsInstance(modelcube_restored, ModelCube) self.assertIsNotNone(modelcube_restored.data) self.assertFalse(os.path.exists(path))
def test_getspaxel_db_api_model(self, galaxy): model_cube = ModelCube(plateifu=galaxy.plateifu, bintype=galaxy.bintype, template=galaxy.template, release=galaxy.release, ) spaxel = _get_spaxel_helper(model_cube, 1, 2, properties=False, drp=False) self._test_getspaxel(spaxel, galaxy) assert isinstance(spaxel.getCube(), Cube) assert 'flux' not in spaxel.cube_quantities assert isinstance(spaxel.getMaps(), Maps) assert len(spaxel.maps_quantities) == 0
def test_pickling_file_custom_path(self, temp_scratch, galaxy): modelcube = ModelCube(filename=galaxy.modelpath, bintype=galaxy.bintype) assert modelcube.data_origin == 'file' assert isinstance(modelcube, ModelCube) assert modelcube.data is not None file = temp_scratch.join('mcpickle').join('test_modelcube.mpf') assert file.check(file=1) is False path = modelcube.save(path=str(file)) assert file.check() is True assert os.path.exists(path) modelcube_restored = ModelCube.restore(str(file), delete=True) assert modelcube_restored.data_origin == 'file' assert isinstance(modelcube_restored, ModelCube) assert modelcube_restored.data is not None assert not os.path.exists(path)
def test_pickling_api(self, temp_scratch, galaxy): modelcube = ModelCube(plateifu=galaxy.plateifu, bintype=galaxy.bintype, mode='remote') assert modelcube.data_origin == 'api' assert isinstance(modelcube, ModelCube) assert modelcube.data is None file = temp_scratch.join('test_modelcube_api.mpf') modelcube.save(str(file)) assert file.check() is True modelcube = None assert modelcube is None modelcube_restored = ModelCube.restore(str(file)) assert modelcube_restored.data_origin == 'api' assert isinstance(modelcube_restored, ModelCube) assert modelcube_restored.data is None
def test_pickling_file(self, temp_scratch, galaxy): modelcube = ModelCube(filename=galaxy.modelpath, bintype=galaxy.bintype) assert modelcube.data_origin == 'file' assert isinstance(modelcube, ModelCube) assert modelcube.data is not None file = temp_scratch.join('test_modelcube.mpf') modelcube.save(str(file)) assert file.check() is True assert modelcube.data is not None modelcube = None assert modelcube is None modelcube_restored = ModelCube.restore(str(file)) assert modelcube_restored.data_origin == 'file' assert isinstance(modelcube_restored, ModelCube) assert modelcube_restored.data is not None
def test_init_modelcube(self, galaxy, data_origin): if data_origin == 'file': kwargs = {'filename': galaxy.modelpath} elif data_origin == 'db': kwargs = {'plateifu': galaxy.plateifu} elif data_origin == 'api': kwargs = {'plateifu': galaxy.plateifu, 'mode': 'remote'} model_cube = ModelCube(**kwargs) assert model_cube.data_origin == data_origin assert model_cube.nsa.z == pytest.approx(galaxy.redshift) self._test_init(model_cube, galaxy)
def test_nobintype_in_db(self, galaxy): if galaxy.release != 'MPL-6': pytest.skip('only running this test for MPL6') with pytest.raises(MarvinError) as cm: ModelCube(plateifu=galaxy.plateifu, bintype='ALL', release=galaxy.release) assert 'Specified bintype ALL is not available in the DB' in str( cm.value)
def test_getspaxel(self, galaxy, data_origin): if data_origin == 'file': kwargs = {'filename': galaxy.modelpath} elif data_origin == 'db': kwargs = {'plateifu': galaxy.plateifu} elif data_origin == 'api': kwargs = {'plateifu': galaxy.plateifu, 'mode': 'remote'} model_cube = ModelCube(bintype=galaxy.bintype, template=galaxy.template, release=galaxy.release, **kwargs) spaxel = _get_spaxel_helper(model_cube, 1, 2) self._test_getspaxel(spaxel, galaxy)
def test_pickling_api(self): modelcube = ModelCube(plateifu=self.plateifu, mode='remote') self.assertEqual(modelcube.data_origin, 'api') self.assertIsInstance(modelcube, ModelCube) self.assertIsNone(modelcube.data) path = modelcube.save() self._files_created.append(path) self.assertTrue(os.path.exists(path)) self.assertEqual(os.path.realpath(path), os.path.realpath(self.filename[0:-7] + 'mpf')) modelcube = None self.assertIsNone(modelcube) modelcube_restored = ModelCube.restore(path) self.assertEqual(modelcube_restored.data_origin, 'api') self.assertIsInstance(modelcube_restored, ModelCube) self.assertIsNone(modelcube_restored.data) self.assertEqual(modelcube_restored.header['VERSDRP3'], 'v2_0_1')
def test_pickling_file(self): modelcube = ModelCube(filename=self.filename) self.assertEqual(modelcube.data_origin, 'file') self.assertIsInstance(modelcube, ModelCube) self.assertIsNotNone(modelcube.data) path = modelcube.save() self._files_created.append(path) self.assertTrue(os.path.exists(path)) self.assertEqual(os.path.realpath(path), os.path.realpath(self.filename[0:-7] + 'mpf')) self.assertIsNotNone(modelcube.data) modelcube = None self.assertIsNone(modelcube) modelcube_restored = ModelCube.restore(path) self.assertEqual(modelcube_restored.data_origin, 'file') self.assertIsInstance(modelcube_restored, ModelCube) self.assertIsNotNone(modelcube_restored.data)
def test_init_modelcube_bintype(self, galaxy, data_origin): kwargs = {'bintype': galaxy.bintype.name} if data_origin == 'file': kwargs['filename'] = galaxy.modelpath elif data_origin == 'db': kwargs['plateifu'] = galaxy.plateifu elif data_origin == 'api': kwargs['plateifu'] = galaxy.plateifu kwargs['mode'] = 'remote' model_cube = ModelCube(**kwargs) assert model_cube.data_origin == data_origin self._test_init(model_cube, galaxy, bintype=galaxy.bintype.name)
def test_get_cube_units(self, galaxy): model_cube = ModelCube(filename=galaxy.modelpath) unit = '1E-17 erg/s/cm^2/ang/spaxel' fileunit = model_cube.data['EMLINE'].header['BUNIT'] assert unit == fileunit unit = fileunit.replace('ang', 'angstrom').split('/spaxel')[0] spaxel = u.Unit('spaxel', represents=u.pixel, doc='A spectral pixel', parse_strict='silent') newunit = (u.Unit(unit) / spaxel) #unit = '1e-17 erg / (Angstrom cm2 s spaxel)' dmunit = model_cube.emline_fit.unit assert newunit == dmunit
def _get_dap_objects(self, **kwargs): """Gets the Maps and ModelCube object.""" try: kwargs_maps = kwargs.copy() kwargs_maps.pop('modelcube_filename', None) kwargs_maps['filename'] = kwargs_maps.pop('maps_filename', None) maps = Maps(**kwargs_maps) except MarvinError as ee: raise MarvinError('failed to open a Maps: {0}'.format(str(ee))) self.plateifu = maps.plateifu self.mangaid = maps.mangaid if _is_MPL4(maps._dapver): return maps, None # try: kwargs_modelcube = kwargs.copy() kwargs_modelcube.pop('maps_filename', None) kwargs_modelcube['filename'] = kwargs_modelcube.pop( 'modelcube_filename', None) kwargs_modelcube['plateifu'] = None # TODO: we need a check here to make sure that if we open both Maps and ModelCube # from files, their bintypes, templates, plate-ifu, etc are consistent. # TODO: this is not a very good implementation and probably has corner cases in which # it fails. It should be refactored. if kwargs_modelcube['filename'] is None: kwargs_modelcube['plateifu'] = self.plateifu kwargs_modelcube['bintype'] = maps.bintype kwargs_modelcube['template_kin'] = maps.template_kin kwargs_modelcube['template_pop'] = maps.template_pop kwargs_modelcube['release'] = maps._release modelcube = ModelCube(**kwargs_modelcube) # except Exception: # # warnings.warn('cannot open a ModelCube for this combination of ' # 'parameters. Some fetures will not be available.', MarvinUserWarning) # modelcube = False return maps, modelcube
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
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
def _get_model_cube(name, **kwargs): """Retrieves a Marvin ModelCube object.""" model_cube = 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 err: results['error'] = 'Failed to parse input name {0}: {1}'.format( name, str(err)) return model_cube, 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)) model_cube = ModelCube(mangaid=mangaid, plateifu=plateifu, release=release, **kwargs) results['status'] = 1 except Exception as err: results['error'] = 'Failed to retrieve ModelCube {0}: {1}'.format( name, str(err)) return model_cube, results
def _get_model_cube(name, use_file=False, release=None, **kwargs): """Retrieves a Marvin ModelCube object.""" model_cube = None results = {} drpver, dapver = config.lookUpVersions(release) # parse name into either mangaid or plateifu try: idtype = parseIdentifier(name) except Exception as err: results['error'] = 'Failed to parse input name {0}: {1}'.format( name, str(err)) return model_cube, results filename = None plateifu = None mangaid = None bintype = kwargs.pop('bintype') template = kwargs.pop('template') try: if use_file: if idtype == 'mangaid': plate, ifu = mangaid2plateifu(name, drpver=drpver) elif idtype == 'plateifu': plate, ifu = name.split('-') if Path is not None: daptype = '{0}-{1}'.format(bintype, template) filename = Path().full('mangadap5', ifu=ifu, drpver=drpver, dapver=dapver, plate=plate, mode='LOGCUBE', daptype=daptype) assert os.path.exists(filename), 'file not found.' else: raise MarvinError('cannot create path for MaNGA cube.') else: if idtype == 'plateifu': plateifu = name elif idtype == 'mangaid': mangaid = name else: raise MarvinError( 'invalid plateifu or mangaid: {0}'.format(idtype)) model_cube = ModelCube(filename=filename, mangaid=mangaid, plateifu=plateifu, release=release, template=template, bintype=bintype, **kwargs) results['status'] = 1 except Exception as err: results['error'] = 'Failed to retrieve ModelCube {0}: {1}'.format( name, str(err)) return model_cube, results
def test_get_flux_remote(self, galaxy): model_cube = ModelCube(plateifu=galaxy.plateifu, mode='remote') shape = tuple([4563] + galaxy.shape) assert model_cube.binned_flux.shape == shape
def test_raises_exception_mpl4(self, galaxy): with pytest.raises(MarvinError) as cm: ModelCube(plateifu=galaxy.plateifu, release='MPL-4') assert 'ModelCube requires at least dapver=\'2.0.2\'' in str(cm.value)
def test_init_from_file_global_mpl4(self, galaxy): model_cube = ModelCube(filename=galaxy.modelpath, release='MPL-4') assert model_cube.data_origin == 'file' self._test_init(model_cube, galaxy)
def test_flag(self, flag, galaxy): modelcube = ModelCube(plateifu=galaxy.plateifu, bintype=galaxy.bintype) assert getattr(modelcube, flag, None) is not None
def test_quality_flag(self, galaxy): modelcube = ModelCube(plateifu=galaxy.plateifu, bintype=galaxy.bintype) assert modelcube.quality_flag is not None