Exemplo n.º 1
0
    def test_add_field(self):
        global dbconfig
        # call without changes
        self.db.update_fields()

        # call with appending column
        dbconfig.DB_MATERIALS_FIELDS.append('testadd')
        dbconfig.DB_MATERIALS_CONVERTERS.append(
            dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.append(
            dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])

        self.db.update_fields()

        # call with inserted column
        dbconfig.DB_MATERIALS_FIELDS.insert(5, 'testadd2')
        dbconfig.DB_MATERIALS_CONVERTERS.insert(
            5, dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.insert(
            5, dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db.update_fields()

        #reset database
        dbconfig.DB_MATERIALS_FIELDS.pop(-1)
        dbconfig.DB_MATERIALS_FIELDS.pop(5)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(-1)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(5)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(-1)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(5)
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db = SLDDB(':memory:')
        self.db.create_database()
Exemplo n.º 2
0
def enter_water():
    db = SLDDB(DB_FILE)
    for T, dens in WATER_DENSITY:
        print(f'Water at {T}°C with density {dens}')
        db.add_material(f'Water',
                        'H2O',
                        description=f"Water at {T}°C",
                        reference=WATER_DENSITY_REF,
                        ref_website=WATER_DENSITY_LINK,
                        density=dens,
                        temperature=T0 + T,
                        physical_state='liquid',
                        data_origin='textbook')
Exemplo n.º 3
0
def select_api(args):
    db = SLDDB(DB_FILE)
    res = db.search_material(filter_invalid=False, ID=int(args['ID']))
    try:
        material = db.select_material(res[0])
    except IndexError:
        return '## ID not found in database'
    except Exception as e:
        return repr(
            e) + '<br >' + "Raised when tried to parse material = %s" % res[0]

    out = material.export(xray_units=args.get('xray_unit', 'edens'))
    return json.dumps(out, indent='    ')
Exemplo n.º 4
0
def enter_amino():
    db = SLDDB(DB_FILE)
    for name, formula, fu_volume in AMINO_DATA:
        print(f'{name}: {formula}    {fu_volume}')
        db.add_material(name,
                        formula,
                        description=f"Amino acid in protein",
                        reference=f'{AMINO_DENS_REF}|{AMINO_HEX_REF}',
                        ref_website=AMINO_REF_LINK,
                        doi=AMINO_REF_DOI,
                        FU_volume=fu_volume,
                        tags=['biology', 'small organic'],
                        physical_state='solution',
                        data_origin='textbook')
Exemplo n.º 5
0
def get_periodic_table(requested_element=None, plot_scale=None):
    if requested_element:
        db = SLDDB(DB_FILE)
        try:
            res = db.search_material(formula=requested_element, name=ELEMENT_FULLNAMES[requested_element])
        except ValueError:
            res=[]
        if len(res)>0:
            return redirect(url_for('calculate_sld', _anchor='results_header', ID=res[0]['ID']))
        else:
            flash(f'No SLD for {ELEMENT_FULLNAMES[requested_element]}')
    scale_colors=PLOT_SCALES[plot_scale]
    return render_template('periodic_table.html', elements=ELEMENTS, group_colors=GROUP_COLORS,
                           element_names=ELEMENT_NAMES, element_colors=ELEMENT_COLORS,
                           element_fullnames=FULLNAMES_OF_Z, element_weight=ELEMENT_WEIGHT,
                           element_b=ELEMENT_B, element_f=ELEMENT_F, element_fMo=ELEMENT_F_MO,
                           scale_colors=scale_colors)
Exemplo n.º 6
0
def search_db(query, invalids=False, offset=0):
    db=SLDDB(DB_FILE)
    if current_user.is_authenticated:
        user="******"%(current_user.name, current_user.email)
    else:
        user=None
    res=db.search_material(filter_invalid=not invalids, offset=offset, **query)
    if len(res)==0:
        return show_search(unsuccessful_query=query)
    hidden_columns=[True for field in DB_MATERIALS_FIELDS]
    advanced_search=any([key in advanced_fields for key in query.keys()])
    for row in res:
        row['color_class']='entry_used'
        if row['selected']==0:
            row['color_class']='entry_unused'
        if row['validated'] is not None:
            row['color_class']='orso_validated'
        if row['invalid'] is not None:
            row['color_class']='orso_invalid'
        if row['created_by'] is not None:
            row['created_by']=encryptor.decrypt(row['created_by'])
        for i, field in enumerate(DB_MATERIALS_FIELDS):
            if row[field] is not None and field not in DB_MATERIALS_HIDDEN_DATA:
                hidden_columns[i]=False
    if offset>0 or len(res)==100:
        count=db.count_material(filter_invalid=not invalids, **query)
        multipage='result range:<br />'
        if offset<99:
            multipage+=f' | {offset+1}-{offset+100} of {count} | <input type="submit" name="next" value="next">'
        elif offset>(count-100):
            multipage+=f'<input type="submit" name="prev" value="prev"> | {offset+1}-{count} of {count} |'
        else:
            multipage+=f'<input type="submit" name="prev" value="prev"> | {offset+1}-{offset+100} of {count} | ' \
                       f'<input type="submit" name="next" value="next">'
    else:
        multipage=None
    flt_fields=[item[1] for item in enumerate(DB_MATERIALS_FIELDS) if not hidden_columns[item[0]]]
    flt_fields_names=[item[1].capitalize() for item in enumerate(DB_MATERIALS_FIELDS) if not hidden_columns[item[0]]]
    hidden_fields_names=[item.capitalize() for item in DB_MATERIALS_HIDDEN_DATA]
    return render_template('search.html', flt_fields=flt_fields, flt_fields_names=flt_fields_names,
                           hidden_fields=DB_MATERIALS_HIDDEN_DATA, hidden_fields_names=hidden_fields_names,
                           query_result=res, orso_user=user, main_fields=main_fields,
                           advanced_fields=advanced_fields, get_input=get_input,
                           advanced_search=advanced_search, multipage=multipage, offset=offset)
Exemplo n.º 7
0
def custom_query(query):
    db=SLDDB(DB_FILE)
    c=db.db.cursor()
    for qitem in query.splitlines():
        c.execute(qitem)
    res=c.fetchall()
    columns=c.description
    c.close()
    db.db.commit()
    return columns, res
Exemplo n.º 8
0
def edit_selection(ID, user):
    db = SLDDB(DB_FILE)
    res = db.search_material(ID=ID, filter_invalid=False)[0]

    def get_input_args(field):
        conv = db_lookup[field][1]
        if field in res:
            value = res[field]
            if value is None:
                value = ""
            elif field == 'created_by':
                value = encryptor.decrypt(value)
        else:
            value = ""
        return conv.html_input(field, value)

    return render_template('input.html',
                           fields=input_fields,
                           get_input=get_input_args,
                           get_unit=get_unit,
                           editing_entry=ID)
Exemplo n.º 9
0
def collect_blendIDs(formula):
    db = SLDDB(DB_FILE)
    elements=[]
    loaded_ids={}
    items=[]
    while '(' in clean_str(formula):
        pre, formula=formula.split(')',1)
        number=float(pre.split('*',1)[0].strip('(').strip())
        ID=int(pre.split('*',1)[1].strip())
        items.append((number, ID))
    for number, ID in items:
        if not ID in loaded_ids:
            entry=db.search_material(ID=ID)[0]
            m=db.select_material(entry)
            loaded_ids[ID]=m
        elements.append(number*loaded_ids[ID])
    result=elements[0]
    for element in elements[1:]:
        # combine molecule matierials
        result+=element
    return result
Exemplo n.º 10
0
def collect_combination(ids, name_dict):
    db = SLDDB(DB_FILE)
    elements=[]
    loaded_ids={}
    for id in ids:
        if not id in loaded_ids:
            try:
                entry=db.search_material(name=ExactString(name_dict[id]))[0]
            except KeyError:
                possible_ids=name_dict.keys()
                raise SequenceParseError(f"Not a valid identifier {id}, options are {''.join(possible_ids)}")
            except IndexError:
                raise SequenceParseError(f"Molecule {name_dict[id]} not found in database")
            m=db.select_material(entry)
            loaded_ids[id]=m
        elements.append(loaded_ids[id])
    result=elements[0]
    for element in elements[1:]:
        # combine molecule matierials
        result+=element
    return result
Exemplo n.º 11
0
def input_material(args):
    db = SLDDB(DB_FILE)

    def get_input_args(field):
        return fill_input(field, args)

    if args['name'] == '' or args['formula'] == '':
        flash("You have to supply a name and Formula!")
        return render_template('input.html',
                               fields=input_fields,
                               get_input=get_input_args,
                               get_unit=get_unit)

    useargs = dict(args.items())
    name = args['name']
    formula = args['formula']
    del (useargs['material'])
    del (useargs['name'])
    del (useargs['formula'])

    for key, value in list(useargs.items()):
        if key == 'created_by' and value != '':
            # encrypt the users email information
            value = encryptor.enrypt(value)
            useargs[key] = value
        if db_lookup[key][1].html_list:
            useargs[key] = request.form.getlist(key)
        if value == '':
            del (useargs[key])
    try:
        db.add_material(name, formula, **useargs)
    except Exception as e:
        flash("Error when trying to insert data:\n" + repr(e))
        return render_template('input.html',
                               fields=input_fields,
                               get_input=get_input_args,
                               get_unit=get_unit)
    return search_db(useargs)
Exemplo n.º 12
0
def calculate_selection(ID):
    db=SLDDB(DB_FILE)
    res=db.search_material(ID=ID, filter_invalid=False)
    try:
        material=db.select_material(res[0])
    except Exception as e:
        return render_template('base.html', error=repr(e)+'<br >'+"Raised when tried to parse material = %s"%res[0])
    match_point=0.
    E, rho_x=material.rho_vs_E()
    _, delta=material.delta_vs_E()
    _, beta=material.beta_vs_E()
    script='' # get_graph(E, rho_x.real, rho_x.imag, res[0]['name'])
    if 'H' in material.formula or 'Hx' in material.formula:
        deuterated=material.deuterated
        if 'Hx' in material.formula:
            exchanged=material.exchanged
            match_point = 100.*material.match_exchange()
        else:
            exchanged=None
    else:
        deuterated=None
        exchanged=None
    if 'H' in material.formula or 'Hx' in material.formula or 'D' in material.formula \
            or any([tag in res[0].get('tags', []) for tag in
                    ['polymer', 'biology', 'membrane', 'lipid', 'small organic', 'surfactant', 'protein']]):
        contrast_matching = True
    else:
        contrast_matching = False
    return render_template('sldcalc.html', material=material, material_name=res[0]['name'],
                           material_description=res[0]['description'], deuterated=deuterated,
                           exchanged=exchanged, match_point=match_point,
                           contrast_matching=contrast_matching, xray_E=E.tolist(),
                           xray_rho_real=nan_to_num(rho_x.real).tolist(),
                           xray_rho_imag=nan_to_num(rho_x.imag).tolist(),
                           xray_delta=nan_to_num(delta).tolist(), xray_beta=nan_to_num(beta).tolist(),
                           validated=res[0]['validated'], validated_by=res[0]['validated_by'],
                           invalid=res[0]['invalid'], invalid_by=res[0]['invalid_by'],
                           formula=res[0]['formula'], density=material.dens, mu=material.mu)
Exemplo n.º 13
0
def search_api(args):
    query = {}
    for key, value in args.items():
        if value.strip() == '':
            continue
        if key in DB_MATERIALS_FIELDS:
            try:
                query[key] = db_lookup[key][1].convert(value)
            except Exception as e:
                return repr(
                    e) + '<br >' + "Raised when tried to parse %s = %s" % (
                        key, value)
    db = SLDDB(DB_FILE)
    res = db.search_material(serializable=True, limit=10000, **query)

    # remove hidden database fields besides ORSO validation
    for ri in res:
        for field in DB_MATERIALS_HIDDEN_DATA:
            if field.startswith('validated'):
                continue
            del ri[field]

    return json.dumps(res, indent='    ')
Exemplo n.º 14
0
def update_material(args):
    db = SLDDB(DB_FILE)

    def get_input_args(field):
        return fill_input(field, args)

    if args['name'] == '' or args['formula'] == '':
        flash("You have to supply a name and Formula!")
        return render_template('input.html',
                               fields=input_fields,
                               get_input=get_input_args,
                               get_unit=get_unit,
                               editing_entry=args['ID'])

    useargs = dict(args.items())
    del (useargs['material'])
    del (useargs['ID'])

    for key, value in list(useargs.items()):
        if db_lookup[key][1].html_list:
            useargs[key] = request.form.getlist(key)
        if value == '':
            useargs[key] = None

    try:
        db.update_material(args['ID'], **useargs)
    except Exception as e:
        flash("Error when trying to insert data:\n" + repr(e))
        return render_template('input.html',
                               fields=input_fields,
                               get_input=get_input_args,
                               get_unit=get_unit,
                               editing_entry=args['ID'])
    for key, value in list(useargs.items()):
        if value is None:
            del (useargs[key])
    return search_db(useargs)
Exemplo n.º 15
0
def main():
    db = SLDDB(DB_FILE)
    for item in data:
        print(item)
        if len(item) > 3:
            db.add_material(item[0],
                            item[0],
                            description=item[3],
                            reference="Christy Kinane",
                            density=item[1],
                            mu=item[2])
        else:
            db.add_material(item[0],
                            item[0],
                            reference="Christy Kinane",
                            density=item[1],
                            mu=item[2])
Exemplo n.º 16
0
def enter_rnadna():
    db = SLDDB(DB_FILE)
    for name, formula, fu_volume in RNA_ABRV:
        print(f'{name}: {formula}    {fu_volume}')
        db.add_material(name,
                        formula,
                        description=f"Nucleotides in RNA",
                        reference=AMINO_DENS_REF,
                        ref_website=AMINO_REF_LINK,
                        FU_volume=fu_volume,
                        tags=['biology', 'small organic'],
                        physical_state='solution',
                        data_origin='textbook')
    for name, formula, fu_volume in DNA_ABRV:
        print(f'{name}: {formula}    {fu_volume}')
        db.add_material(name,
                        formula,
                        description=f"Nucleotides in DNA",
                        reference=AMINO_DENS_REF,
                        ref_website=AMINO_REF_LINK,
                        FU_volume=fu_volume,
                        tags=['biology', 'small organic'],
                        physical_state='solution',
                        data_origin='textbook')
Exemplo n.º 17
0
class SLDDBWindow(QtWidgets.QMainWindow):
    selected_material=None

    def __init__(self):
        super(SLDDBWindow, self).__init__() # Call the inherited classes __init__ method
        self.ui=uic.loadUi('qt_interface/slddb_window.ui', self) # Load the .ui file
        self.setColumns()
        self.show() # Show the GUI
        self.db=SLDDB(DB_FILE)

    def setColumns(self):
        tbl=self.ui.resultTable
        tbl.setColumnCount(len(DB_MATERIALS_FIELDS)-len(DB_MATERIALS_HIDDEN_DATA))
        self.headers=[]
        for field in DB_MATERIALS_FIELDS:
            if field in DB_MATERIALS_HIDDEN_DATA:
                continue
            self.headers.append(field)
        tbl.setHorizontalHeaderLabels(self.headers)
        for i in range(len(self.headers)):
            tbl.setColumnHidden(i, True)

        tbl=self.ui.entryTable
        tbl.setRowCount(len(self.headers))
        tbl.setVerticalHeaderLabels(self.headers)

    def searchDatabase(self):
        name=self.ui.nameEdit.text()
        description=self.ui.descriptionEdit.text()
        formula=self.ui.formulaEdit.text()
        search={'name': name, 'description': description, 'formula': formula}
        for key, value in list(search.items()):
            if value.strip()=='':
                del(search[key])

        results=self.db.search_material(**search)
        hidden_columns=[True for field in self.headers]
        for row in results:
            for i, field in enumerate(self.headers):
                if row[field] is not None:
                    hidden_columns[i]=False
        tbl=self.ui.resultTable
        tbl.sortByColumn(-1, QtCore.Qt.AscendingOrder)
        tbl.setRowCount(len(results))
        for i, hidden in enumerate(hidden_columns):
            tbl.setColumnHidden(i, hidden)
        for i,result in enumerate(results):
            for j, field in enumerate(self.headers):
                item=result[field]
                if type(item) is datetime:
                    item=str(item)
                titem=QtWidgets.QTableWidgetItem()
                titem.setData(QtCore.Qt.DisplayRole, item)
                tbl.setItem(i, j, titem)
        tbl.resizeColumnsToContents()
        self.results=results

        if len(results)==1:
            tbl.setRangeSelected(QtWidgets.QTableWidgetSelectionRange(0, -1, 1, -1), True)
            self.selectItem(0, 0)

    def selectItem(self, row, col):
        tbl=self.ui.resultTable
        ID=tbl.indexFromItem(tbl.item(row, 0)).data()
        orig_row=[res['ID'] for res in self.results].index(ID)
        result=self.results[orig_row]
        try:
            material=self.db.select_material(result)
        except Exception as e:
            return
        self.selected_material=material
        self.ui.resultName.setText(result['name'])
        self.updateResult()

    def updateResult(self):
        material=self.selected_material
        if material is None:
            return

        if self.ui.densityVolumeSelect.currentIndex()==0:
            self.ui.densityVolume.setText('%.4f'%material.dens)
        else:
            self.ui.densityVolume.setText('%.4g'%(1000./material.fu_dens))
        self.ui.neutronSLD.setText("%.6f"%(1e6*material.rho_n.real))
        self.ui.neutronSLDimag.setText("%.6f"%(1e6*material.rho_n.imag))

        try:
            rhox=material.rho_of_E(float(self.ui.xrayEnergyEdit.currentText()))
        except ValueError:
            self.ui.xraySLD.setText("")
            self.ui.xraySLDimag.setText("")
        else:
            self.ui.xraySLD.setText("%.6f"%(1e6*rhox.real))
            self.ui.xraySLDimag.setText("%.6f"%(1e6*rhox.imag))
Exemplo n.º 18
0
def invalidate_selection(ID, user):
    db=SLDDB(DB_FILE)
    db.invalidate_material(ID, user)
    return calculate_selection(ID)
Exemplo n.º 19
0
 def setUpClass(cls):
     cls.db = SLDDB(':memory:')
     cls.db.create_database()
Exemplo n.º 20
0
class TestCreateDB(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.db = SLDDB(':memory:')
        cls.db.create_database()

    @classmethod
    def tearDownClass(cls):
        del (cls.db)

    def test_tables(self):
        c = self.db.db.cursor()
        c.execute("SELECT name FROM sqlite_master WHERE type='table'")
        items = c.fetchall()
        for i, tbl in enumerate([dbconfig.DB_MATERIALS_NAME]):
            with self.subTest(msg=tbl, i=i):
                self.assertTrue((tbl, ) in items)

    def test_element_search(self):
        with self.subTest('database search', i=0):
            s1 = element_table.get_element('Si')
            s2 = element_table.get_element(14)
        with self.subTest('equality', i=0):
            self.assertEqual(s1.Z, s2.Z)
            self.assertEqual(s1.symbol, s2.symbol)
            self.assertEqual(s1.mass, s2.mass)
            self.assertEqual(s1.b, s2.b)
            testing.assert_array_equal(s1._xdata, s2._xdata)

    def test_add_field(self):
        global dbconfig
        # call without changes
        self.db.update_fields()

        # call with appending column
        dbconfig.DB_MATERIALS_FIELDS.append('testadd')
        dbconfig.DB_MATERIALS_CONVERTERS.append(
            dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.append(
            dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])

        self.db.update_fields()

        # call with inserted column
        dbconfig.DB_MATERIALS_FIELDS.insert(5, 'testadd2')
        dbconfig.DB_MATERIALS_CONVERTERS.insert(
            5, dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.insert(
            5, dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db.update_fields()

        #reset database
        dbconfig.DB_MATERIALS_FIELDS.pop(-1)
        dbconfig.DB_MATERIALS_FIELDS.pop(5)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(-1)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(5)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(-1)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(5)
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db = SLDDB(':memory:')
        self.db.create_database()

    def test_backup(self):
        self.db.backup(':memory:')
Exemplo n.º 21
0
import slddb
from slddb import __version__, dbconfig
# for flask use database file in startup folder
DB_FILE = 'slddb.db'
dbconfig.DB_FILE = DB_FILE
slddb.DB_FILE = DB_FILE
from slddb import SLDDB
from slddb.dbconfig import DB_FILE
import sqlite3

if __name__ == '__main__':
    db = SLDDB(DB_FILE)
    try:
        db.create_database()
        db.add_elements()
    except sqlite3.OperationalError:
        db.update_fields()
Exemplo n.º 22
0
 def __init__(self):
     super(SLDDBWindow, self).__init__() # Call the inherited classes __init__ method
     self.ui=uic.loadUi('qt_interface/slddb_window.ui', self) # Load the .ui file
     self.setColumns()
     self.show() # Show the GUI
     self.db=SLDDB(DB_FILE)