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)
def _initCubes(self): ''' Initialize a list of Marvin Cube objects ''' _cubes = [None] if self.data_origin == 'file': sdss_path = Path(release=self.release) if self.dir3d == 'stack': cubes = sdss_path.expand('mangacube', drpver=self._drpver, plate=self.plateid, ifu='*', wave='LOG') else: cubes = sdss_path.expand('mangamastar', drpver=self._drpver, plate=self.plateid, ifu='*', wave='LOG') _cubes = [Cube(filename=cube, mode=self.mode, release=self.release) for cube in cubes] elif self.data_origin == 'db': _cubes = [Cube(plateifu=cube.plateifu, mode=self.mode, release=self.release) for cube in self._plate.cubes] elif self.data_origin == 'api': routeparams = {'plateid': self.plateid} url = config.urlmap['api']['getPlateCubes']['url'].format(**routeparams) # Make the API call response = self._toolInteraction(url) data = response.getData() plateifus = data['plateifus'] _cubes = [Cube(plateifu=pifu, mode=self.mode, release=self.release) for pifu in plateifus] FuzzyList.__init__(self, _cubes) self.mapper = (lambda e: e.plateifu)
def getSpaxel(self): args = av.manual_parse(self, request, use_params='galaxy', required=['plateifu', 'type'], makemulti=True) cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release} maptype = args.get('type', None) if maptype == 'optical': # for now, do this, but TODO - general processRequest to handle lists and not lists try: mousecoords = args.getlist('mousecoords[]', type=float) except Exception as e: mousecoords = None if mousecoords: pixshape = (args.get('imwidth', type=int), args.get('imheight', type=int)) if (mousecoords[0] < 0 or mousecoords[0] > pixshape[0]) or (mousecoords[1] < 0 or mousecoords[1] > pixshape[1]): output = {'specmsg': 'Error: requested pixel coords are outside the image range.', 'status': -1} self.galaxy['error'] = output['specmsg'] else: cube = Cube(**cubeinputs) # TODO - generalize image file sas_url to filesystem switch, maybe in sdss_access infile = get_manga_image(cube, local=True) current_session['imagefile'] = infile #infile = os.path.join(os.getenv('MANGA_SPECTRO_REDUX'), args.get('image').split('redux/')[1]) arrcoords = convertImgCoords(mousecoords, infile, to_radec=True) webspec, specmsg, badspots = getWebSpectrum(cube, arrcoords[0], arrcoords[1], byradec=True) if not webspec: self.galaxy['error'] = 'Error: {0}'.format(specmsg) status = -1 else: status = 1 msg = 'gettin some spaxel at RA/Dec {0}'.format(arrcoords) output = {'message': msg, 'specmsg': specmsg, 'spectra': webspec, 'status': status, 'badspots': badspots} else: output = {'specmsg': 'Error getting mouse coords', 'status': -1} self.galaxy['error'] = output['specmsg'] elif maptype == 'heatmap': # grab spectrum based on (x, y) coordinates x = args.get('x', None, type=int) y = args.get('y', None, type=int) if all([x, y]): cube = Cube(**cubeinputs) webspec, specmsg, badspots = getWebSpectrum(cube, x, y, xyorig='lower') msg = 'gettin some spaxel with (x={0}, y={1})'.format(x, y) if not webspec: self.galaxy['error'] = 'Error: {0}'.format(specmsg) status = -1 else: status = 1 output = {'message': msg, 'specmsg': specmsg, 'spectra': webspec, 'status': status, 'badspots': badspots} else: output = {'specmsg': 'Error: X or Y not specified for map', 'status': -1} self.galaxy['error'] = output['specmsg'] else: output = {'specmsg': 'Error: No maptype specified in request', 'status': -1} self.galaxy['error'] = output['specmsg'] return jsonify(result=output)
def test_get_available_bintypes(self, galaxy, mode): if mode == 'db': cube = Cube(plateifu=galaxy.plateifu) else: cube = Cube(filename=galaxy.cubepath) bintypes = cube.get_available_bintypes() expbins = galaxy.dap['stellar_sigma'].keys() assert set(bintypes) == set(expbins)
def test_load_cube_from_file_OSError(self, galaxy): cube = Cube(filename=galaxy.cubepath) cube.filename = 'hola.fits' with pytest.raises((IOError, OSError)) as ee: cube._load_cube_from_file() assert 'filename {0} cannot be found'.format(cube.filename) in str( ee.value)
def test_getSpaxel_inputs(self, galaxy, x, y, ra, dec, excType, message): """Tests exceptions when getSpaxel gets inappropriate inputs.""" kwargs = self._dropNones(x=x, y=y, ra=ra, dec=dec) with pytest.raises(excType) as ee: cube = Cube(plateifu=galaxy.plateifu, release=galaxy.release) cube.getSpaxel(**kwargs) assert message in str(ee.value)
def test_pickling_db_fails(self, temp_scratch, galaxy): cube = Cube(plateifu=galaxy.plateifu) spaxel = cube.getSpaxel(1, 3) file = temp_scratch.join('test_spaxel.mpf') with pytest.raises(MarvinError) as cm: spaxel.save(str(file), overwrite=True) assert 'objects with data_origin=\'db\' cannot be saved.' in str(cm.value)
def test_pickling_db(self, galaxy, temp_scratch): cube = Cube(plateifu=galaxy.plateifu) assert cube.data_origin == 'db' file = temp_scratch.join('test_cube_db.mpf') with pytest.raises(MarvinError) as cm: cube.save(str(file)) assert 'objects with data_origin=\'db\' cannot be saved.' in str( cm.value)
def test_pickling_db(self): cube = Cube(plateifu=self.plateifu) self.assertEqual(cube.data_origin, 'db') with self.assertRaises(MarvinError) as ee: cube.save() self.assertIn('objects with data_origin=\'db\' cannot be saved.', str(ee.exception))
def test_getSpaxel_remote_drpver_differ_from_global(self, galaxy, monkeyconfig): if galaxy.release == 'MPL-5': pytest.skip('Skipping release for forced global MPL-5') assert config.release == 'MPL-5' cube = Cube(plateifu=galaxy.plateifu, mode='remote', release=galaxy.release) expected = galaxy.spaxel['flux'] spectrum = cube.getSpaxel(ra=galaxy.spaxel['ra'], dec=galaxy.spaxel['dec']).flux assert spectrum.value[galaxy.spaxel['specidx']] == pytest.approx(expected)
def cube(galaxy, exporigin, mode): ''' Yield a Marvin Cube based on the expected origin combo of (mode+db). Fixture tests 6 cube origins from (mode+db) combos [file, db and api] ''' if exporigin == 'file': c = Cube(filename=galaxy.cubepath, release=galaxy.release, mode=mode) else: c = Cube(plateifu=galaxy.plateifu, release=galaxy.release, mode=mode) c.exporigin = exporigin yield c c = None
def test_getspaxel_matches_file_db_remote(self, galaxy): cube_file = Cube(filename=galaxy.cubepath) cube_db = Cube(plateifu=galaxy.plateifu) cube_api = Cube(plateifu=galaxy.plateifu, mode='remote') assert cube_file.data_origin == 'file' assert cube_db.data_origin == 'db' assert cube_api.data_origin == 'api' xx = galaxy.spaxel['x'] yy = galaxy.spaxel['y'] spec_idx = galaxy.spaxel['specidx'] flux = galaxy.spaxel['flux'] ivar = galaxy.spaxel['ivar'] mask = galaxy.spaxel['mask'] spaxel_slice_file = cube_file[yy, xx] spaxel_slice_db = cube_db[yy, xx] spaxel_slice_api = cube_api[yy, xx] abs = 1.e-7 assert spaxel_slice_file.flux.value[spec_idx] == pytest.approx(flux, abs=abs) assert spaxel_slice_db.flux.value[spec_idx] == pytest.approx(flux, abs=abs) assert spaxel_slice_api.flux.value[spec_idx] == pytest.approx(flux, abs=abs) assert spaxel_slice_file.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_slice_db.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_slice_api.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_slice_file.flux.mask[spec_idx] == pytest.approx(mask) assert spaxel_slice_db.flux.mask[spec_idx] == pytest.approx(mask) assert spaxel_slice_api.flux.mask[spec_idx] == pytest.approx(mask) xx_cen = galaxy.spaxel['x_cen'] yy_cen = galaxy.spaxel['y_cen'] try: spaxel_getspaxel_file = cube_file.getSpaxel(x=xx_cen, y=yy_cen) spaxel_getspaxel_db = cube_db.getSpaxel(x=xx_cen, y=yy_cen) spaxel_getspaxel_api = cube_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.flux.value[spec_idx] == pytest.approx(flux, abs=1e-6) assert spaxel_getspaxel_db.flux.value[spec_idx] == pytest.approx(flux, abs=1e-6) assert spaxel_getspaxel_api.flux.value[spec_idx] == pytest.approx(flux, abs=1e-6) assert spaxel_getspaxel_file.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_getspaxel_db.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_getspaxel_api.flux.ivar[spec_idx] == pytest.approx(ivar) assert spaxel_getspaxel_file.flux.mask[spec_idx] == pytest.approx(mask) assert spaxel_getspaxel_db.flux.mask[spec_idx] == pytest.approx(mask) assert spaxel_getspaxel_api.flux.mask[spec_idx] == pytest.approx(mask)
def _getSpaxel_remote_fail(self, ra, dec, errMsg1, errMsg2=None, excType=MarvinError): cube = Cube(mangaid=self.mangaid, mode='remote') with self.assertRaises(excType) as cm: cube.getSpaxel(ra=ra, dec=dec) self.assertIn(errMsg1, str(cm.exception)) if errMsg2: self.assertIn(errMsg2, str(cm.exception))
def setUp(self): super(TestGalaxyPage, self).setUp() self.blue = 'galaxy_page' config.setRelease('MPL-5') self.mode = config.mode self.release = config.release self.params = {'release': self.release} # set up cube and expected values self.cube = Cube(plateifu=self.plateifu, mode=self.mode) # NSA params for 8485-1901 self.cols = [ 'z', 'elpetro_logmass', 'sersic_n', 'elpetro_absmag_i', 'elpetro_absmag_g_r', 'elpetro_th50_r', 'elpetro_absmag_u_r', 'elpetro_absmag_i_z', 'elpetro_ba', 'elpetro_phi', 'elpetro_mtol_i', 'elpetro_th90_r' ] self.exp_nsa_plotcols = { 'z': 0.0407447, 'elpetro_logmass': 9.565475912843823, 'sersic_n': 3.29617, 'elpetro_absmag_i': -19.1125469207764, 'elpetro_absmag_g_r': 0.5961246490479013, 'elpetro_th50_r': 1.33067, 'elpetro_absmag_u_r': 1.7617149353028019, 'elpetro_absmag_i_z': 0.20068740844720168, 'elpetro_ba': 0.87454, 'elpetro_phi': 154.873, 'elpetro_mtol_i': 1.30610692501068, 'elpetro_th90_r': 3.6882 }
def updateMaps(self): args = av.manual_parse(self, request, use_params='galaxy', required=['plateifu', 'bintemp', 'params[]'], makemulti=True) #self._drpver, self._dapver, self._release = parseSession() cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release} params = args.getlist('params[]', type=str) bintemp = args.get('bintemp', None, type=str) current_session['bintemp'] = bintemp # get cube (self.galaxy['cube'] does not work) try: cube = Cube(**cubeinputs) except Exception as e: cube = None # Try to make the web maps if not cube: output = {'mapmsg': 'No cube found', 'maps': None, 'status': -1} elif not params: output = {'mapmsg': 'No parameters selected', 'maps': None, 'status': -1} else: try: mapdict = buildMapDict(cube, params, self._dapver, bintemp=bintemp) except Exception as e: output = {'mapmsg': e.message, 'status': -1, 'maps': None} else: output = {'mapmsg': None, 'status': 1, 'maps': mapdict} return jsonify(result=output)
def test_load_filename_does_not_exist(self): """Tries to load a file that does not exist, in auto mode.""" with pytest.raises(AssertionError) as ee: Cube(filename='hola.fits', mode='auto') assert re.match(r'filename .*hola.fits does not exist', str(ee.value)) is not None
def test_getSpaxel_remote_fail_badresponse(self, monkeyconfig): assert config.urlmap is not None with pytest.raises(MarvinError) as cm: Cube(mangaid='1-209232', mode='remote') assert 'Failed to establish a new connection' in str(cm.value)
def test_pickling_file_custom_path(self): cube = Cube(filename=self.filename) test_path = '~/test.mpf' path = cube.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))) cube_restored = Cube.restore(path, delete=True) self.assertEqual(cube_restored.data_origin, 'file') self.assertIsInstance(cube_restored, Cube) self.assertIsNotNone(cube_restored.data) self.assertFalse(os.path.exists(path))
def test_sort_dir_spectrum(self, galaxy, class_, expected): cube = Cube(plateifu=galaxy.plateifu) spax = cube[0, 0] spec = spax.flux dir_ = _sort_dir(spec, class_) dir_public = [it for it in dir_ if it[0] != '_'] assert set(dir_public) == set(expected)
def test_cube_load_from_local_database_success(self, galaxy): """Tests for Cube Load by Database.""" cube = Cube(mangaid=galaxy.mangaid) assert cube is not None assert galaxy.mangaid == cube.mangaid assert galaxy.plate == cube.plate assert galaxy.dec == cube.dec assert galaxy.ra == cube.ra
def test_getSpaxel_remote_drpver_differ_from_global(self): self._update_release('MPL-5') self.assertEqual(config.release, 'MPL-5') cube = Cube(plateifu=self.plateifu, mode='remote', release='MPL-4') expect = 0.62007582 self._test_getSpaxel(cube, 3000, expect, ra=232.544279, dec=48.6899232)
def initDynamic(self): ''' Route to run when the dynamic toggle is initialized This creates the web spectrum and dap heatmaps ''' # get the form parameters args = av.manual_parse(self, request, use_params='galaxy', required='plateifu') # datamodel dm = datamodel[self._dapver] # turning toggle on current_session['toggleon'] = args.get('toggleon') # get the cube cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release} cube = Cube(**cubeinputs) output = {'specstatus': -1, 'mapstatus': -1} # get web spectrum webspec, specmsg = getWebSpectrum(cube, cube.ra, cube.dec, byradec=True) daplist = [p.full(web=True) for p in dm.properties] dapdefaults = dm.get_default_mapset() # build the uber map dictionary try: mapdict = buildMapDict(cube, dapdefaults, self._dapver) mapmsg = None except Exception as e: mapdict = [{'data': None, 'msg': 'Error', 'plotparams': None} for m in dapdefaults] mapmsg = 'Error getting maps: {0}'.format(e) else: output['mapstatus'] = 1 if not webspec: output['error'] = 'Error: {0}'.format(specmsg) else: output['specstatus'] = 1 output['image'] = get_manga_image(cube) output['spectra'] = webspec output['specmsg'] = specmsg output['maps'] = mapdict output['mapmsg'] = mapmsg output['dapmaps'] = daplist output['dapmapselect'] = dapdefaults output['dapbintemps'] = dm.get_bintemps(db_only=True) current_session['bintemp'] = '{0}-{1}'.format(dm.get_bintype(), dm.get_template()) # try to jsonify the result try: jsonout = jsonify(result=output) except Exception as e: jsonout = jsonify(result={'specstatus': -1, 'mapstatus': -1, 'error': '{0}'.format(e)}) return jsonout
def test_pickling_api(self, temp_scratch, galaxy): cube = Cube(plateifu=galaxy.plateifu, mode='remote') assert cube.data_origin == 'api' assert isinstance(cube, Cube) assert cube.data is None test_path = temp_scratch.join('test_cube_api.mpf') cube.save(str(test_path)) assert test_path.check() is True cube = None assert cube is None cube_restored = Cube.restore(str(test_path)) assert cube_restored.data_origin == 'api' assert isinstance(cube_restored, Cube) assert cube_restored.data is None
def test_custom_drpall(self, galaxy, mpl, drpver): assert galaxy.drpall in config.drpall cube = Cube(plateifu=galaxy.plateifu, release=mpl) drpall = 'drpall-{0}.fits'.format(drpver) assert cube._release == mpl assert cube._drpver == drpver assert os.path.exists(cube._drpall) is True assert drpall in cube._drpall assert galaxy.drpall in config.drpall
def test_cube_flux_from_local_database(self): cube = Cube(plateifu=self.plateifu, mode='local') flux = cube.flux self.assertEqual(cube.data_origin, 'db') cubeFlux = fits.getdata(self.filename) self.assertTrue(np.allclose(flux, cubeFlux))
def test_cube_flux_from_api(self): cube = Cube(plateifu=self.plateifu, mode='remote') flux = cube.flux self.assertEqual(cube.data_origin, 'api') cubeFlux = fits.getdata(self.filename) self.assertTrue(np.allclose(flux, cubeFlux, rtol=1e-5))
def setUpClass(cls): super(TestCubeBase, cls).setUpClass() cls.outrelease = 'MPL-4' cls._update_release(cls.outrelease) cls.set_filepaths() cls.filename = os.path.realpath(cls.cubepath) cls.cubeFromFile = Cube(filename=cls.filename)
def test_load_filename_remote(self): """Tries to load a filename in remote mode and fails.""" config.mode = 'remote' with self.assertRaises(MarvinError) as ee: Cube(filename='hola.fits') self.assertIn('filename not allowed in remote mode', str(ee.exception))
def test_cube_load_from_local_database_success(self): """Tests for Cube Load by Database.""" cube = Cube(mangaid=self.mangaid) self.assertIsNotNone(cube) self.assertEqual(self.mangaid, cube.mangaid) self.assertEqual(self.plate, cube.plate) self.assertEqual(self.dec, cube.dec) self.assertEqual(self.ra, cube.ra)
def test_pickling_file_custom_path(self, temp_scratch, galaxy): cube = Cube(filename=galaxy.cubepath) assert cube.data_origin == 'file' assert isinstance(cube, Cube) assert cube.data is not None test_path = temp_scratch.join('cubepickle').join('test_cube.mpf') assert test_path.check(file=1) is False path = cube.save(path=str(test_path)) assert test_path.check(file=1) is True assert path == os.path.realpath(os.path.expanduser(str(test_path))) cube_restored = Cube.restore(str(test_path), delete=True) assert cube_restored.data_origin == 'file' assert isinstance(cube_restored, Cube) assert cube_restored.data is not None assert not os.path.exists(path)