Exemplo n.º 1
0
def insert(db, collection, tmc, debug=True):
    repeated, _tmcdoc = check_repeated(db, collection, tmc)
    inserted = False
    if not repeated:
        db[collection].insert_one(tmc.document)
        inserted = True
    else:
        if debug:
            print(("existed: ", _tmcdoc["unique_name"]))
            print("merging....")
        try:
            _tmc = tmcMongo(document=_tmcdoc, tag=_tmcdoc["tag"], subtag=_tmcdoc["subtag"],
                            publication=_tmcdoc["publication"])
            converted = True
        except ValueError:
            converted = False
            merge_document_with_new(db, doc1=_tmcdoc, doc_new=tmc.document, update_fields=tmc.update_fields)
        if converted:
            merge_documents(db, collection,
                            doc1=_tmcdoc, doc2=tmc.document,
                            update_fields=tmc.update_fields)
            merge_dftruns(dftrun1=_tmc.this_run,
                          dftrun2=tmc.this_run,
                          update_fields=tmc.update_fields)
            _tmc.write_dftrun(force=True)
    return inserted
Exemplo n.º 2
0
def push2db(database, collection, tag, subtag, publication=False,
            user=False, pwd=False, host="localhost", port=27017, auth=False,
            all_runs_pickle=False, update_fields=False, all_runs_list=False,
            outpath='/userarchive/db_backup'):
    '''
    Push data to MongoDB.

    :param db: mongo database instance.
    :param collection: name of a collection.
    :param user: username.
    :param pwd: password.
    :param host: IP address of the MongoDB.
    :param port: port to connect.
    :param auth: whether authentication is required to connect to the MongoDB.
    :param all_runs_pickle: whether to push from a pickle file of a list of DFTrun objects. If False, will run
    check_all_current_convergence() in your mAD folder.
    :param tag: tag of your data. Recommend to use the project name (may related to the name of your paper).
    :param subtag: Recommend as the name of your mAD folder (considering we may have many mAD folders for each project).

    :return: None
    '''
    from molSimplifyAD.ga_check_jobs import check_all_current_convergence
    db = connect2db(user, pwd, host, port, database, auth)
    ensure_collection(db, collection)
    print(('db push is enabled, attempting commit with tag: %s, subtag: %s to %s' % (tag, subtag, collection)))
    if not all_runs_pickle:
        if not all_runs_list == False:
            all_runs = all_runs_list
        else:
            _, all_runs, _ = check_all_current_convergence(post_all=True)
    else:
        all_runs = pickle.load(open(all_runs_pickle, "rb"))
        print(("DFTruns loaded from %s." % all_runs_pickle))
    print(("number of DFTruns exists: ", len(all_runs)))
    count = 0
    merged = 0
    for this_run in list(all_runs.values()):
        print(("adding complex: ", this_run.name))
        print(("converged:", this_run.converged, "geo_flag:", this_run.geo_flag,
               "ss_flag: ", this_run.ss_flag, "metal_spin_flag: ", this_run.metal_spin_flag))
        this_tmc = tmcMongo(this_run=this_run, tag=tag, subtag=subtag,
                            publication=publication, update_fields=update_fields)
        _s = time.time()
        insetred = insert(db, collection, this_tmc)
        print(("elapse: ", time.time() - _s))
        if insetred:
            count += 1
        else:
            merged += 1
    print(("add %d entries in the %s['%s']." % (count, database, collection)))
    print(("merge %d entries in the %s['%s']." % (merged, database, collection)))
    print("creating index...")
    db[collection].create_index([("metal", pymongo.ASCENDING),
                                 ("ox", pymongo.ASCENDING),
                                 ("spin", pymongo.ASCENDING),
                                 ("converged", pymongo.ASCENDING),
                                 ("alpha", pymongo.ASCENDING),
                                 ("lig1", pymongo.ASCENDING),
                                 ("lig5", pymongo.ASCENDING),
                                 ("lig6", pymongo.ASCENDING),
                                 ("status", pymongo.ASCENDING),
                                 ("geo_flag", pymongo.ASCENDING),
                                 ("ss_flag", pymongo.ASCENDING)
                                 ])
    dump_databse(database_name=database,
                 outpath=outpath,
                 user=user, pwd=pwd)
Exemplo n.º 3
0
        basepath = os.path.split(basepath)[0]
    dbinfo = glob.glob(basepath + '/*dbinfo')
    if len(dbinfo) > 0:
        with open(dbinfo[0]) as f:
            dbval = f.readlines()[0]
        reference_struct = query_db(db, 'oct', {'_id': ObjectId(str(dbval))})
        print((reference_struct.count()))
        print((reference_struct[0]['subtag']))
        subtag = reference_struct[0]['subtag'] + '_additional'
    else:
        subtag = 'no_reference_additional'

    print((this_run.geo_flag, this_run.metal_spin_flag, this_run.ss_flag,
           this_run.status, subtag))
    if write:
        this_tmc = tmcMongo(this_run=this_run, tag=tag, subtag=subtag)
        _s = time.time()
        inserted = insert(db, collection, this_tmc)
        print(("elapse: ", time.time() - _s))
        if inserted:
            count += 1
        else:
            merged += 1

#### Do this after looping over all outfiles.
print(("add %d entries in the %s['%s']." % (count, database, collection)))
print(("merge %d entries in the %s['%s']." % (merged, database, collection)))
print("creating index...")
if write:
    db[collection].create_index([("metal", pymongo.ASCENDING),
                                 ("ox", pymongo.ASCENDING),
Exemplo n.º 4
0
            this_run.wavefunction = get_wavefunction(scrpath)
        else:
            this_run.wavefunction = False
        if this_run.converged:
            this_run.flag_oct = True if int(row["flag_oct"]) else False
        else:
            this_run.flag_oct = -1
        filename = "./oldgen_" + this_run.name + "_optim.xyz"
        this_run.init_mol = mol3D()
        this_run.init_mol.readfromtxt(get_initgeo(filename))
        this_run.mol = mol3D()
        this_run.mol.readfromtxt(get_lastgeo(filename))
        this_run.progmol = mol3D()
        this_run.progmol.readfromtxt(get_lastgeo(filename))

        this_tmc = tmcMongo(this_run=this_run,
                            tag="spectro",
                            subtag="oldgen",
                            publication="Duan_JCTC_2019",
                            update_fields=update_fields)
        print((this_tmc.document['status'], this_tmc.document['alphaHOMO']))
        _s = time.time()
        insetred = insert(db, "oct", this_tmc)
        print(("elapse: ", time.time() - _s))
        if insetred:
            count += 1
        else:
            merged += 1
print(("add %d entries in the %s['%s']." % (count, "tmc", "oct")))
print(("merge %d entries in the %s['%s']." % (merged, "tmc", "oct")))
Exemplo n.º 5
0
def main():
    args = arg_parser()
    if args.user == None or args.pwd == None:
        raise KeyError(
            "Please use the format python update_db_documents.py -user <username> -pwd <password>."
        )
    constraints = {
        "lig6": {
            "$ne": "x"
        },
        "status": 0,
        "gap": {
            "$exists": False
        }
    }
    update_fields = ['gap']
    database = "tmc"
    collection = "oct"
    user = args.user
    pwd = args.pwd
    db = connect2db(user,
                    pwd,
                    host="localhost",
                    port=27017,
                    database=database,
                    auth=True)
    print(("Number of complexes in the collection:", db.oct.count()))
    cursor = db[collection].find(constraints)
    tot = count_find(cursor)
    print(("Number of complexes to be updated: ", tot))
    cursor = db[collection].find(constraints,
                                 no_cursor_timeout=True).batch_size(10)
    print(("Are you sure to update %s with constraints %s in %s[%s]? (y/n)" %
           (str(update_fields), str(constraints), database, collection)))
    _in = eval(input())
    if not _in == "y":
        print("Quit. Have a nice day.")
        quit()
    count = 0
    confirmed = False
    for _tmcdoc in cursor:
        print(("complex: ", _tmcdoc["unique_name"]))
        recovered = True
        try:
            _this_tmc = tmcMongo(document=_tmcdoc,
                                 tag=_tmcdoc["tag"],
                                 subtag=_tmcdoc["subtag"],
                                 publication=_tmcdoc["publication"],
                                 update_fields=update_fields)
            print(("complex id_doc:", _this_tmc.id_doc))
        except ValueError:
            print("The input document cannot recover a DFTrun object.")
            recovered = False

        if recovered:
            ####
            ## Case 1.
            ## Simple modification. You have already know what to update and you **don't** want to update dftrun.

            new_tmc = copy.deepcopy(_this_tmc)
            try:
                new_tmc.document["gap"] = new_tmc.document[
                    "alphaLUMO"] - new_tmc.document["alphaHOMO"]
            except:
                pass
            # Change here. e.g. new_tmc.document["publication"] = xxx
            ####

            ####
            ## Case 2.
            ## Modify both the documents and dftrun.

            # _this_run = copy.deepcopy(_this_tmc.this_run)
            # current_folder = _this_run.scrpath.strip("optim.xyz")
            # multiwfnpath = glob.glob(current_folder + "*.molden")
            # if len(multiwfnpath) > 0:
            #     multiwfnpath = multiwfnpath[0]
            #     mulliken_spin_list = get_mulliken(multiwfnpath, _this_run.spin, _this_run.liglist[-1])
            #     print(mulliken_spin_list)
            #     _this_run.net_metal_spin = mulliken_spin_list[0]
            #     if len(mulliken_spin_list) > 1:
            #         _this_run.net_oxygen_spin = mulliken_spin_list[1]
            # else:
            #     print("No molden path found.")
            # _this_run.get_check_flags()
            # new_tmc = tmcMongo(this_run=_this_run, tag=_tmcdoc["tag"], subtag=_tmcdoc["subtag"],
            #                    publication=_tmcdoc["publication"], update_fields=update_fields)
            ###

            if not confirmed:
                for key in update_fields:
                    print(("=======Key======: ", key))
                    if key in _tmcdoc:
                        print(("Current: ", _tmcdoc[key]))
                    else:
                        print("Currently does not exist.")
                    print(("Change to: ", new_tmc.document[key]))
                print("Is this expected? (y/n)")
                _in = eval(input())
                if _in == "y":
                    confirmed = True
                else:
                    print("Quit. Have a nice day.")
                    quit()
            __ = insert(db, collection, new_tmc)
        count += 1
        print((" In progress: %d / %d" % (count, tot)))
    print(("You have changed %d documents in %s[%s]" %
           (tot, database, collection)))