Exemple #1
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='    ')
Exemple #2
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)
Exemple #3
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)
Exemple #4
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)
Exemple #5
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
Exemple #6
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
Exemple #7
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)
Exemple #8
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='    ')
Exemple #9
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))