예제 #1
0
파일: insert.py 프로젝트: ZhouHUB/simdb
def insert_pdf_data_document(name, input_filename=None,
                             atomic_config=None, exp_dict=None, time=None):
    if time is None:
        time = ttime.time()
    # at some level, you dont actually care where this thing is on disk
    file_uid = str(uuid4())
    # create the filename
    file_name = os.path.join(simdb.PDF_PATH, file_uid + '.gr')

    generated = False
    if atomic_config is not None:
        # Then we should generate the PDF
        s = Scatter(exp_dict)

        # Just in case we use the exp_dict = None default params
        params = s.exp

        # get the atomic configuration from the DB
        atomic_doc, = find_atomic_config_document(_id=atomic_config.id)
        atoms = atomic_doc.file_payload[-1]

        # Generate the PDF from the atomic configuration
        gobs = s.get_pdf(atoms)
        generated = True

        # Save the gobs
        # TODO: replace with context
        f = open(file_name, 'w')
        np.save(f, gobs)
        f.close()
        res = fsc.insert_resource('genpdf', file_name)
        fsc.insert_datum(res, file_uid)
    else:
        # Then the pdf is experimental, thus we should let filestore know it
        # exists, and load the PDF generating parameters into the Metadata
        res = fsc.insert_resource('pdfgetx3', input_filename)
        fsc.insert_datum(res, file_uid)
        params = load_gr_file(input_filename)[-1]

    # create an instance of a mongo document (metadata)
    if generated is True:
        a = PDFData(name=name, file_uid=file_uid, ase_config_id=atomic_config,
                    pdf_params=params, time=time)
    else:
        a = PDFData(name=name, file_uid=file_uid,
                    # will support when we merge this with metadata store
                    # experiment_uid=exp_uid,
                    pdf_params=params, time=time)
    # save the document
    a.save()
    return a
예제 #2
0
파일: insert.py 프로젝트: CJ-Wright/simdb
def insert_atom_document(name, ase_object, time=None):
    if time is None:
        time = ttime.time()
    # at some level, you dont actually care where this thing is on disk
    file_uid = str(uuid4())

    is_trajectory = False
    if isinstance(ase_object, list):
        is_trajectory = True

    # create the filename
    file_name = os.path.join(simdb.ATOM_PATH, file_uid + '.traj')
    # save the object
    aseio.write(file_name, ase_object)

    # do the filestore magic
    resource = fsc.insert_resource('ase', file_name)
    fsc.insert_datum(resource, file_uid,
                     datum_kwargs={'is_trajectory': is_trajectory})

    # create an instance of a mongo document (metadata)
    a = AtomicConfig(name=name, file_uid=file_uid, time=time)
    # save the document
    a.save()
    return a
예제 #3
0
def test_overwrite_global():
    mock_base = dict(spec='syn-mod',
                     resource_path='',
                     resource_kwargs={'shape': (5, 7)})

    res = fsc.insert_resource(**mock_base)

    cache_key = (str(res.id), SynHandlerMod.__name__)
    with fsr.handler_context({'syn-mod': SynHandlerMod}):
        fsr.get_spec_handler(res.id)
        assert_in(cache_key, fsr._HANDLER_CACHE)
        fsr.register_handler('syn-mod', SynHandlerEcho, overwrite=True)
        assert_not_in(cache_key, fsr._HANDLER_CACHE)
예제 #4
0
def test_get_handler_global():

    mock_base = dict(spec='syn-mod',
                     resource_path='',
                     resource_kwargs={'shape': (5, 7)})

    res = fsc.insert_resource(**mock_base)
    cache_key = (str(res.id), SynHandlerMod.__name__)
    with fsr.handler_context({'syn-mod': SynHandlerMod}):

        handle = fsr.get_spec_handler(res.id)

        assert_true(isinstance(handle, SynHandlerMod))
        assert_in(cache_key, fsr._HANDLER_CACHE)

    assert_not_in(cache_key, fsr._HANDLER_CACHE)
예제 #5
0
    def add_data(self, data, uid=None, resource_kwargs=None):
        """
        Parameters
        ----------
        data : ndarray
            The data to save

        uid : str, optional
            The uid to be used for this entry,
            if not given use uuid1 to generate one

        resource_kwargs : None, optional
            Currently raises if not 'falsy' and is ignored.

        Returns
        -------
        uid : str
            The uid used to register this data with filestore, can
            be used to retrieve it
        """
        if not self._writable:
            raise RuntimeError("This writer can only write one data entry "
                               "and has already been used")

        if resource_kwargs:
            raise ValueError("This writer does not support resource_kwargs")

        if op.exists(self._fpath):
            raise IOError("the requested file {fpath} "
                          "already exist".format(fpath=self._fpath))

        if uid is None:
            uid = str(uuid.uuid1())

        np.save(self._fpath, np.asanyarray(data))
        self._writable = False
        fb = fsc.insert_resource(self.SPEC_NAME, self._fpath, self._f_custom)
        evl = fsc.insert_datum(fb, uid)

        return evl.datum_id