Saving and Restoring Marvin objects

With all Marvin Tools, you can save the object you are working with locally to your filesystem, and restore it later on. This works using the Python pickle package. The objects are pickled (i.e. formatted and compressed) into a pickle file object. All Marvin Tools, Queries, and Results can be saved and restored.

We can save a map...


In [14]:
# let's grab the H-alpha emission line flux map
from marvin.tools.maps import Maps
mapfile = '/Users/Brian/Work/Manga/analysis/v2_0_1/2.0.2/SPX-GAU-MILESHC/8485/1901/manga-8485-1901-MAPS-SPX-GAU-MILESHC.fits.gz'
maps = Maps(filename=mapfile)
haflux = maps.getMap('emline_gflux', channel='ha_6564')
print(haflux)


<Marvin Map (plateifu='8485-1901', property='emline_gflux', channel='ha_6564')>

We can save any Marvin object with the save method. This methods accepts a string filename+path as the name of the pickled file. If a full file path is not specified, it defaults to the current directory. save also accepts an overwrite boolean keyword in case you want to overwrite an existing file.


In [15]:
haflux.save('my_haflux_map')


Out[15]:
'/Users/Brian/Work/github_projects/Marvin/docs/sphinx/jupyter/my_haflux_map'

Now we have a saved map. We can restore it anytime we want using the restore class method. A class method means you call it from the imported class itself, and not on the instance. restore accepts a string filename as input and returns the instantianted object.


In [24]:
# import the individual Map class
from marvin.tools.quantities import Map

# restore the Halpha flux map into a new variable
filename = '/Users/Brian/Work/github_projects/Marvin/docs/sphinx/jupyter/my_haflux_map'
newflux = Map.restore(filename)
print(newflux)


<Marvin Map (plateifu='8485-1901', property='emline_gflux', channel='ha_6564')>

We can also save and restore Marvin Queries and Results. First let's create and run a simple query...


In [32]:
from marvin.tools.query import Query, Results

# let's make a query
f = 'nsa.z < 0.1'
q = Query(searchfilter=f)
print(q)

# and run it
r = q.run()
print(r)


Your parsed filter is: 
nsa.z<0.1
Marvin Query(mode='remote', limit=100, sort=None, order='asc')
Results contain of a total of 4, only returning the first 4 results
[u'mangaid', u'plate', u'plateifu', u'name', u'z']
Marvin Results(results=NamedTuple(mangaid=u'1-209232', plate=8485, plateifu=u'8485-1901', name=u'1901', z=0.0407446920871735), 
query=u'SELECT mangadatadb.cube.mangaid AS "cube.mangaid", mangadatadb.cube.plate AS "cube.plate", concat(mangadatadb.cube.plate, \'-\', mangadatadb.ifudesign.name) AS "cube.plateifu", mangadatadb.ifudesign.name AS "ifu.name", mangasampledb.nsa.z AS "nsa.z" \nFROM mangadatadb.cube JOIN mangadatadb.ifudesign ON mangadatadb.ifudesign.pk = mangadatadb.cube.ifudesign_pk JOIN mangasampledb.manga_target ON mangasampledb.manga_target.pk = mangadatadb.cube.manga_target_pk JOIN mangasampledb.manga_target_to_nsa ON mangasampledb.manga_target.pk = mangasampledb.manga_target_to_nsa.manga_target_pk JOIN mangasampledb.nsa ON mangasampledb.nsa.pk = mangasampledb.manga_target_to_nsa.nsa_pk JOIN mangadatadb.pipeline_info AS drpalias ON drpalias.pk = mangadatadb.cube.pipeline_info_pk \nWHERE mangasampledb.nsa.z < 0.1 AND drpalias.pk = 25', 
count=4, 
mode=remote

Let's save both the query and results for later use. Without specifiying a filename, by default Marvin will name the query or results using your provided search filter.


In [34]:
q.save()
r.save()


WARNING: file already exists. Not overwriting.
Out[34]:
'/Users/Brian/marvin_results_nsa.z<0.1.mpf'

By default, if you don't specify a filename for the pickled file, Marvin will auto assign one for you with extension .mpf (MaNGA Pickle File).

Now let's restore...


In [37]:
newquery = Query.restore('/Users/Brian/marvin_query_nsa.z<0.1.mpf')
print('query', newquery)
print('filter', newquery.searchfilter)


('query', Marvin Query(mode='remote', limit=100, sort=None, order='asc'))
('filter', 'nsa.z < 0.1')

In [39]:
myresults = Results.restore('/Users/Brian/marvin_results_nsa.z<0.1.mpf')
print(myresults.results)


[NamedTuple(mangaid=u'1-209232', plate=8485, plateifu=u'8485-1901', name=u'1901', z=0.0407446920871735), NamedTuple(mangaid=u'1-209113', plate=8485, plateifu=u'8485-1902', name=u'1902', z=0.0378877110779285), NamedTuple(mangaid=u'1-209191', plate=8485, plateifu=u'8485-12701', name=u'12701', z=0.0234252735972404), NamedTuple(mangaid=u'1-209151', plate=8485, plateifu=u'8485-12702', name=u'12702', z=0.0185245610773563)]

In [ ]: