Пример #1
0
def test_predict():
    client = CitrinationClient(environ['CITRINATION_API_KEY'],
                               environ['CITRINATION_SITE'])
    inputs = [
        {
            "CHEMICAL_FORMULA": "AlCu"
        },
    ]
    resp = client.predict("betterdensitydemo", inputs)
    prediction = resp['candidates'][0]['Density']
    assert abs(prediction[0] - 5.786) < 0.1
Пример #2
0
def main():
    # Set up client
    client = CitrinationClient(os.environ["CITRINATION_API_KEY"],
                               'https://citrination.com')

    # Read in experimental data
    filename = "C:/Users/mvane/Documents/Skunkworks/BMG/Data/BMG_full_dataset_with_energies.csv"
    exp_data = pd.read_csv(filename)
    formula = exp_data['formula'].as_matrix()
    energy = exp_data['PROPERTY: Nearest DFT Formation Energy (eV)'].as_matrix(
    )
    form = "formula"
    property = "Property Nearest DFT Formation Energy"
    # Convert formulas & energies to dictionary. Make a list of dictionaries.
    input = []
    for i in range(0, len(formula)):
        input.append({form: formula[i], property: energy[i]})

    # Make predictions of Tg, Tx, and Tl. (These will also contain many of the Magpie descriptors used to train the model)
    model_num = "4416"
    predictions = client.predict(model_num, input)

    # Write all these predictions to json files with date and time to differentiate predictions.
    # Folder path:
    folder_out = "C:/Users/mvane/Documents/GitHub/better-glasses/predictions_output.csv"
    first = True
    # Make CSV writer.
    with open(folder_out, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        # Go through every prediction that we made from the chemical formula
        for p in predictions:
            keys = p.all_keys()
            # Make a header row with all of the keys listed
            if first:
                row = []
                row.append('Formula')
                for k in keys:
                    row.append(k)
                    row.append(str(k) + ' Uncertainty')
                writer.writerow(row)
                first = False
            row = []
            # Write the formula in the first column of the CSV. Formula will also show up in a later column.
            row.append(p.get_value('formula').value)
            # Write the value and uncertainty of each key to the CSV
            for k in keys:
                val = p.get_value(k).value
                loss = p.get_value(k).loss
                row.append(str(val))
                row.append(str(loss))
            writer.writerow(row)

    csvfile.close()
Пример #3
0
def predict_from_pifs(view_id,
                      pifs,
                      predict,
                      condition={},
                      exclude=[],
                      client=None):
    '''
	predict properties using inputs from pifs. returns dataframe with actual and predicted values
	------------------
	view_id: dataview id containing model
	pifs: list of pifs to predict
	predict: list of properties to predict
	condition: dict of conditions and values
	exclude: properties in pif to exclude from inputs (besides properties to predict)
	client: CitrinationClient instance
	'''
    if client is None:
        client = CitrinationClient(os.environ['CITRINATION_API_KEY'],
                                   'https://citrination.com')

    ids = []
    inputs = []
    predict = predict
    actuals = []
    for pif in pifs:
        pids = {p.name: p.value for p in pif.ids}
        ids.append(pids)
        props = {p.name: p.scalars for p in pif.properties}
        props['formula'] = pif.chemical_formula
        inp = {
            'Property {}'.format(k): v
            for (k, v) in props.items() if k not in predict + exclude
        }
        inp.update(condition)
        inputs.append(inp)
        actuals.append({k: v for (k, v) in props.items() if k in predict})

    modelout = client.predict(view_id, inputs)
    predictions = []
    for r in modelout:
        pred = {
            'pred_{}'.format(p): r.get_value('Property {}'.format(p)).value
            for p in predict
        }
        predictions.append(pred)

    dicts = []
    for i, a, p in zip(ids, actuals, predictions):
        td = {**i, **a, **p}
        dicts.append(td)
    result = pd.DataFrame(dicts)

    return result
Пример #4
0
class CitrinationSaxsClassifer(object):
    """A set of classifiers to be used on SAXS spectra"""

    def __init__(self, address, api_key_file):
        with open(api_key_file, "r") as g:
            api_key = g.readline()
        a_key = api_key.strip()

        self.client = CitrinationClient(site = address, api_key=a_key)


    def citrination_classify(self,sample_params):
        """
        Parameters
        ----------
        sample_params : ordered dictionary
            ordered dictionary of floats representing features of test sample

        Returns
        -------
        flags : dict
            dictionary of (boolean,float) tuples,
            where the first item is the flag
            and the second is the probability,
            for each of the potential scattering populations
        """

        inputs = self.append_str_property(sample_params)

        flags = OrderedDict()
        resp = self.client.predict("24", inputs) # "24" is ID of dataview on Citrination
        flags['unidentified'] = resp['candidates'][0]['Property unidentified']
        flags['guinier_porod'] = resp['candidates'][0]['Property guinier_porod']
        flags['spherical_normal'] = resp['candidates'][0]['Property spherical_normal']
        flags['diffraction_peaks'] = resp['candidates'][0]['Property diffraction_peaks']

        return flags


    # helper function
    def append_str_property(self, sample_params):
        inputs = {}
        for k,v in sample_params.items():
            k = "Property " + k
            inputs[k] = v
        return inputs


    def citrination_predict(self, populations, sample_params, q_I):
        """Apply self.models and self.scalers to sample_params.

        Parameters
        ----------
        sample_params : ordered dictionary
            ordered dictionary of floats representing features of test sample

        Returns
        -------
        flags : dict
            dictionary of (boolean,float) tuples,
            where the first item is the flag
            and the second is the probability,
            for each of the potential scattering populations
        """

        features = self.append_str_property(sample_params)

        params = OrderedDict.fromkeys(saxs_math.all_parameter_keys)

        if populations['unidentified'][0] == '1':
            # TODO: we could still use a fit to 'predict' I0_floor...
            return params # all params are "None"

        if populations['spherical_normal'][0] == '1' and populations['diffraction_peaks'][0] == '0':
            resp = self.client.predict("27", features) # "27" is ID of dataview on Citrination
            params['r0_sphere'] = resp['candidates'][0]['Property r0_sphere']

            additional_features = saxs_math.spherical_normal_profile(q_I)
            additional_features = self.append_str_property(additional_features)
            ss_features = dict(features)
            ss_features.update(additional_features)
            resp = self.client.predict("28", ss_features)
            params['sigma_sphere'] = resp['candidates'][0]['Property sigma_sphere']

        if populations['guinier_porod'][0] == '1':
            additional_features = saxs_math.guinier_porod_profile(q_I)
            additional_features = self.append_str_property(additional_features)
            rg_features = dict(features)
            rg_features.update(additional_features)
            resp =self.client.predict("29", rg_features)
            params['rg_gp'] = resp['candidates'][0]['Property rg_gp']

        return params
def test_predict():
    client = CitrinationClient(environ['CITRINATION_API_KEY'], environ['CITRINATION_SITE'])
    inputs = [{"CHEMICAL_FORMULA": "AlCu"}, ]
    resp = client.predict("betterdensitydemo", inputs)
    prediction = resp['candidates'][0]['Density']
    assert abs(prediction[0] - 5.786) < 0.1