Exemple #1
0
def get_nsa_dict(name, drpver, makenew=None):
    ''' Gets a NSA dictionary from a pickle or a query '''
    nsapath = os.environ.get('MANGA_SCRATCH_DIR', None)
    if nsapath and os.path.isdir(nsapath):
        nsapath = nsapath
    else:
        nsapath = os.path.expanduser('~')

    nsaroot = os.path.join(nsapath, 'nsa_pickles')
    if not os.path.isdir(nsaroot):
        os.makedirs(nsaroot)

    picklename = '{0}.pickle'.format(name)
    nsapickle_file = os.path.join(nsaroot, picklename)
    if os.path.isfile(nsapickle_file):
        nsadict = marvin_pickle.restore(nsapickle_file)
    else:
        # make from scratch from db
        session = marvindb.session
        sampledb = marvindb.sampledb
        allnsa = session.query(sampledb.NSA, marvindb.datadb.Cube.plateifu).\
            join(sampledb.MangaTargetToNSA, sampledb.MangaTarget,
                 marvindb.datadb.Cube, marvindb.datadb.PipelineInfo,
                 marvindb.datadb.PipelineVersion, marvindb.datadb.IFUDesign).\
            filter(marvindb.datadb.PipelineVersion.version == drpver).options(FromCache(name)).all()
        nsadict = [(_db_row_to_dict(n[0], remove_columns=['pk', 'catalogue_pk']), n[1]) for n in allnsa]

        # write a new NSA pickle object
        if makenew:
            marvin_pickle.save(nsadict, path=nsapickle_file, overwrite=True)

    return nsadict
Exemple #2
0
 def test_delete_on_restore(self, temp_scratch):
     file = temp_scratch.join('tmp_testMarvinPickleDeleteOnRestore.pck')
     path_out = marvin_pickle.save(obj=data, path=str(file), overwrite=False)
     assert file.check() is True
     revived_data = marvin_pickle.restore(path_out, delete=True)
     assert data == revived_data
     assert os.path.isfile(str(file)) is False
Exemple #3
0
 def test_overwrite_true(self, temp_scratch):
     file = temp_scratch.join('tmp_testMarvinPickleOverwriteTrue.pck')
     open(str(file), 'a').close()
     path_out = marvin_pickle.save(obj=data, path=str(file), overwrite=True)
     assert file.check() is True
     revived_data = marvin_pickle.restore(path_out)
     assert data == revived_data
Exemple #4
0
    def save(self, path=None, overwrite=False):
        """Pickles the object.

        If ``path=None``, uses the default location of the file in the tree
        but changes the extension of the file to ``.mpf``. Returns the path
        of the saved pickle file.

        """

        return marvin_pickle.save(self, path=path, overwrite=overwrite)
Exemple #5
0
    def save(self, path=None, overwrite=False):
        """Pickles the object.

        If ``path=None``, uses the default location of the file in the tree
        but changes the extension of the file to ``.mpf``. Returns the path
        of the saved pickle file.

        Parameters:
            obj:
                Marvin object to pickle.
            path (str):
                Path of saved file. Default is ``None``.
            overwrite (bool):
                If ``True``, overwrite existing file. Default is ``False``.

        Returns:
            str:
                Path of saved file.

        """

        return marvin_pickle.save(self, path=path, overwrite=overwrite)
Exemple #6
0
 def test_specify_path(self, temp_scratch):
     file = temp_scratch.join('tmp_testMarvinPickleSpecifyPath.pck')
     path_out = marvin_pickle.save(obj=data, path=str(file), overwrite=False)
     assert file.check() is True
     revived_data = marvin_pickle.restore(path_out)
     assert data == revived_data