def run(self):
        blendranges = []
        submodel_blend_names = []
        submodel_predictions = []
        datakey = self.ChooseDataComboBox.currentText()
        refcol = ('comp', self.optimizeSubRangesComboBox.currentText())

        # start with the low model
        blendranges.append(
            [-9999, float(self.lowPredictionMaxSpinBox.value())])
        submodel_blend_names.append(self.lowPredictionComboBox.currentText())
        submodel_predictions.append(
            self.data[datakey].df[('predict',
                                   self.lowPredictionComboBox.currentText())])

        # append the intermediate submodels
        for i in range(0, self.index_spin.value()):
            temp_vals = self.subwidgets[i].getValues()
            blendranges.append(temp_vals[1])
            submodel_blend_names.append(temp_vals[0])
            submodel_predictions.append(self.data[datakey].df[('predict',
                                                               temp_vals[0])])

        # append the high model
        blendranges.append(
            [float(self.highPredictionMinSpinBox.value()), 9999])
        submodel_blend_names.append(self.highPredictionComboBox.currentText())
        submodel_predictions.append(
            self.data[datakey].df[('predict',
                                   self.highPredictionComboBox.currentText())])

        # append the reference model as a catch-all
        submodel_blend_names.append(
            self.referencePredictionComboBox.currentText())
        submodel_predictions.append(self.data[datakey].df[(
            'predict', self.referencePredictionComboBox.currentText())])

        if self.optimizeSubRangesCheckBox.isChecked():
            truevals = self.data[datakey].df[refcol]
        else:
            truevals = None

        sm_obj = sm.sm(blendranges)
        # optimize blending if reference data is provided
        if truevals is not None:
            predictions_blended = sm_obj.do_blend(
                np.array(submodel_predictions), truevals=truevals)
        else:
            # otherwise just blend the predictions together
            predictions_blended = sm_obj.do_blend(
                np.array(submodel_predictions))

        # save the blended predictions
        self.data[datakey].df[('predict', 'Blended-Predict ' +
                               str(sm_obj.blendranges))] = predictions_blended
Esempio n. 2
0
def test_sm_blend():
    df = pd.read_csv(get_path('test_data.csv'), header=[0, 1])
    x = df['wvl']
    y = df[('comp', 'SiO2')]

    model1 = reg.regression(method=['PLS'],
                            params=[{
                                'n_components': 3,
                                'scale': False
                            }])
    model1.fit(x, y)
    df[('predict', 'model1')] = model1.predict(x)

    model2 = reg.regression(method=['PLS'],
                            params=[{
                                'n_components': 5,
                                'scale': False
                            }])
    model2.fit(x, y)
    df[('predict', 'model2')] = model2.predict(x)

    model3 = reg.regression(method=['PLS'],
                            params=[{
                                'n_components': 4,
                                'scale': False
                            }])
    model3.fit(x, y)
    df[('predict', 'model3')] = model3.predict(x)

    predictions = [
        df[('predict', 'model2')], df[('predict', 'model1')],
        df[('predict', 'model3')], df[('predict', 'model1')]
    ]

    blendranges = [[-9999, 30], [20, 60], [50, 9999]]
    sm_obj = sm.sm(blendranges)
    blended_predictions = sm_obj.do_blend(
        np.array(predictions))  #without optimization
    rmse = np.sqrt(np.average((blended_predictions - df[('comp', 'SiO2')])**2))
    np.testing.assert_almost_equal(rmse, 12.703434300128926, decimal=5)

    blended_predictions = sm_obj.do_blend(
        np.array(predictions),
        truevals=np.array(df[('comp', 'SiO2')]))  #with optimization
    rmse = np.sqrt(np.average((blended_predictions - df[('comp', 'SiO2')])**2))
    expected_blendranges = [
        -9999., 36.5198746, 47.98157746, 56.2537253, 118.94036468, 9999.
    ]
    np.testing.assert_almost_equal(rmse, 9.954065920454982, decimal=5)
    np.testing.assert_allclose(expected_blendranges,
                               sm_obj.blendranges,
                               rtol=1e-5)
    def setup(self):
        blendranges = []
        datakey = self.ChooseDataComboBox.currentText()

        # start with the low model
        blendranges.append(
            [-9999, float(self.lowPredictionMaxSpinBox.value())])

        # append the intermediate submodels
        for i in range(0, self.index_spin.value()):
            temp_vals = self.subwidgets[i].getValues()
            blendranges.append(temp_vals[1])

        # append the high model
        blendranges.append(
            [float(self.highPredictionMinSpinBox.value()), 9999])

        sm_obj = sm.sm(blendranges)
        # save the blended predictions
        try:
            self.data[datakey].df[('predict', 'Blended-Predict ' +
                                   str(sm_obj.blendranges))] = 99999
        except:
            pass