Esempio n. 1
0
def update_slider_marks(filename):
    if filename is None:
        filename = 'ExampleData'
        database_sel = init_db
    else:
        filename = filename
        database_sel = database
    data, raw_data = pop_with_db(filename, database_sel)
    return {str(each): str(each) for each in data['Cycle_Index'].unique()}
Esempio n. 2
0
def update_slider_max(filename):
    if filename is None:
        filename = 'ExampleData'
        database_sel = init_db
    else:
        filename = filename
        database_sel = database
    data, raw_data = pop_with_db(filename, database_sel)
    datatype = data['datatype'].iloc[0]
    (cycle_ind_col, data_point_col, volt_col, curr_col, dis_cap_col,
     char_cap_col, charge_or_discharge) = col_variables(datatype)
    return data['Cycle_Index'].max()
Esempio n. 3
0
def test_pop_with_db():
    """test that the clean and raw dataframes are 
    returned from this function from a file'"""

    process_data(test_filename, test_db, decoded_dataframe, test_datatype)

    df_clean, df_raw = pop_with_db(test_filename, test_db)

    assert df_clean is not None
    assert type(df_clean) == pd.DataFrame

    assert df_raw is not None
    assert type(df_raw) == pd.DataFrame

    assert 'Smoothed_dQ/dV' in df_clean.columns
    os.remove(test_db)
    return
Esempio n. 4
0
def test_get_model_dfs():
    """This tests that the model results returned make sense 
    with respect to each other. First tests types of returned 
    variables, and also checks that the number of peaks found 
    in either charge or discharge sections correspond to the
    number of unique prefixes in the model vals dictionary."""

    process_data(test_filename, test_db, decoded_dataframe,
                 test_datatype)

    df_clean, df_raw = pop_with_db(test_filename, test_db)
    cyc = 1
    lenmax = len(df_clean)
    peak_thresh = 0.7
    new_df_mody, model_c_vals, model_d_vals, \
        peak_heights_c, peak_heights_d = get_model_dfs(df_clean,
                                                       test_datatype,
                                                       cyc, lenmax,
                                                       peak_thresh)

    assert type(new_df_mody) == pd.DataFrame
    assert type(model_c_vals) == dict and type(model_d_vals) == dict
    assert type(peak_heights_c) == list
    assert type(peak_heights_d) == list

    # There should be at least a base_amplitude, base_center, base_fwhm, and base sigma for all cycles
    for key in ['base_amplitude', 'base_center', 'base_fwhm', 'base_height', 'base_sigma']:
        assert key in model_c_vals.keys()
        assert key in model_d_vals.keys()
        assert type(model_c_vals[key]) in (np.float64, np.float32, float, int)
        assert type(model_d_vals[key]) in (np.float64, np.float32, float, int)

    # There should be one peak height in peak_heights_cd for each unique key
    for item in [[model_d_vals, peak_heights_d], [model_c_vals, peak_heights_c]]:
        pref_list = []
        for key in item[0].keys():
            pref = key.split('_')[0]
            pref_list.append(pref)
            pref_set = set(pref_list)
        assert len(pref_set) - 1 == len(item[1])
        # minus one because of 'base' prefix
    os.remove(test_db)
    return
Esempio n. 5
0
def test_generate_model_for_maccor():
    """Tests that three new tables are generated in the database 
    in the process of generating the model. Acutal model generation
    functions are tested further outside of this wrapper."""
    peak_thresh = 0.7

    process_data(test_filename_mac, test_db, decoded_dataframe_mac,
                 test_datatype_mac)
    filename_pref = get_filename_pref(test_filename_mac)
    df_clean, df_raw = pop_with_db(test_filename_mac, test_db)

    feedback = generate_model(df_clean, filename_pref,
                              peak_thresh, test_db)
    assert type(feedback) == str
    names_list = get_table_names(test_db)

    list_new_tables = ['-ModPoints', 'ModParams', '-descriptors']
    for i in list_new_tables:
        assert filename_pref + i in names_list
    os.remove(test_db)
    return
Esempio n. 6
0
def update_figure2(filename, peak_thresh, n_clicks, show_gauss, desc_to_plot,
                   cd_to_plot, peaknum_to_plot):
    """ This is  a function to evaluate the model on a sample plot before updating the database"""
    if filename is None:
        filename = 'ExampleData'
        database_sel = init_db
    else:
        database_sel = database
    data, raw_data = pop_with_db(filename, database_sel)
    datatype = data['datatype'].iloc[0]
    (cycle_ind_col, data_point_col, volt_col, curr_col, dis_cap_col,
     char_cap_col, charge_or_discharge) = col_variables(datatype)
    selected_step = round(data[cycle_ind_col].max() / 2) + 1
    # select a cycle in the middle of the set
    dff_data = data[data[cycle_ind_col] == selected_step]
    if len(data[cycle_ind_col].unique()) > 1:
        lenmax = max([
            len(data[data[cycle_ind_col] == cyc])
            for cyc in data[cycle_ind_col].unique() if cyc != 1
        ])
    else:
        lenmax = len(data)
    dff_raw = raw_data[raw_data[cycle_ind_col] == selected_step]
    peak_vals_df = get_file_from_database(
        filename.split('.')[0] + '-descriptors', database_sel)

    fig = plotly.subplots.make_subplots(
        rows=1,
        cols=2,
        subplot_titles=('Descriptors',
                        'Example Data for Model Tuning (Cycle ' +
                        str(int(selected_step)) + ')'),
        shared_xaxes=True)
    marker = {'color': ['#0074D9']}
    if peak_vals_df is not None:
        if n_clicks is not None:
            # if the user has hit the update-model-button - remake model
            new_df_mody, model_c_vals, model_d_vals, peak_heights_c, peak_heights_d = get_model_dfs(
                dff_data, datatype, selected_step, lenmax, peak_thresh)
            dff_mod = new_df_mody
            c_sigma = model_c_vals['base_sigma']
            c_center = model_c_vals['base_center']
            c_amplitude = model_c_vals['base_amplitude']
            c_fwhm = model_c_vals['base_fwhm']
            c_height = model_c_vals['base_height']

            d_sigma = model_d_vals['base_sigma']
            d_center = model_d_vals['base_center']
            d_amplitude = model_d_vals['base_amplitude']
            d_fwhm = model_d_vals['base_fwhm']
            d_height = model_d_vals['base_height']
        else:
            # if user hasn't pushed the button, populate with original model
            # from database
            modset_name = filename.split('.')[0] + '-ModPoints'
            df_model = get_file_from_database(modset_name, database_sel)
            dff_mod = df_model[df_model[cycle_ind_col] == selected_step]

            filtpeakvals = peak_vals_df[peak_vals_df['c_cycle_number'] ==
                                        selected_step]
            filtpeakvals = filtpeakvals.reset_index(drop=True)
            # grab values for the underlying gaussian in the charge:
            try:
                c_sigma = filtpeakvals['c_gauss_sigma'].iloc[0]
                c_center = filtpeakvals['c_gauss_center'].iloc[0]
                c_amplitude = filtpeakvals['c_gauss_amplitude'].iloc[0]
                c_fwhm = filtpeakvals['c_gauss_fwhm'].iloc[0]
                c_height = filtpeakvals['c_gauss_height'].iloc[0]
            except BaseException:
                # there may not be a model
                pass
            # grab values for the underlying discharge gaussian:
            try:
                d_sigma = filtpeakvals['d_gauss_sigma'].iloc[0]
                d_center = filtpeakvals['d_gauss_center'].iloc[0]
                d_amplitude = filtpeakvals['d_gauss_amplitude'].iloc[0]
                d_fwhm = filtpeakvals['d_gauss_fwhm'].iloc[0]
                d_height = filtpeakvals['d_gauss_height'].iloc[0]
            except BaseException:
                pass

        fig.append_trace(
            {
                'x': dff_data[volt_col],
                'y': dff_data['Smoothed_dQ/dV'],
                'type': 'scatter',
                'marker': marker,
                'name': 'Smoothed Data'
            }, 1, 2)
        if len(peaknum_to_plot) > 0:
            for value in peaknum_to_plot:
                try:
                    fig.append_trace(
                        {
                            'x':
                            peak_vals_df['c_cycle_number'],
                            'y':
                            peak_vals_df[str(''.join(desc_to_plot)) +
                                         str(''.join(cd_to_plot)) + value],
                            'type':
                            'scatter',
                            'marker':
                            marker,
                            'name':
                            value
                        }, 1, 1)
                except KeyError as e:
                    None
        fig.append_trace(
            {
                'x': dff_mod[volt_col],
                'y': dff_mod['Model'],
                'type': 'scatter',
                'name': 'Model of One Cycle'
            }, 1, 2)
        # add if checkbox is selected to show polynomial baseline
        if 'show' in show_gauss:
            try:
                fig.append_trace(
                    {
                        'x':
                        dff_mod[volt_col],
                        'y': ((c_amplitude /
                               (c_sigma * ((2 * 3.14159)**0.5))) * np.exp(
                                   (-(dff_mod[volt_col] - c_center)**2) /
                                   (2 * c_sigma**2))),
                        'type':
                        'scatter',
                        'name':
                        'Charge Gaussian Baseline'  # plot the poly
                    },
                    1,
                    2)
            except BaseException:
                pass
            # add the plot of the discharge guassian:
            try:
                fig.append_trace(
                    {
                        'x':
                        dff_mod[volt_col],
                        'y':
                        -((d_amplitude /
                           (d_sigma * ((2 * 3.14159)**0.5))) * np.exp(
                               (-(dff_mod[volt_col] - d_center)**2) /
                               (2 * d_sigma**2))),
                        'type':
                        'scatter',
                        'name':
                        'Discharge Gaussian Baseline'  # plot the poly
                    },
                    1,
                    2)
            except BaseException:
                pass

    fig['layout']['showlegend'] = True
    fig['layout']['xaxis1'].update(title='Cycle Number')
    fig['layout']['xaxis2'].update(title='Voltage (V)')
    fig['layout']['yaxis1'].update(title='Descriptor Value')
    fig['layout']['yaxis2'].update(title='dQ/dV',
                                   range=[
                                       dff_data['Smoothed_dQ/dV'].min(),
                                       dff_data['Smoothed_dQ/dV'].max()
                                   ])
    fig['layout']['height'] = 600
    fig['layout']['margin'] = {'l': 40, 'r': 10, 't': 60, 'b': 200}
    return fig
Esempio n. 7
0
def update_figure1(selected_step, filename, showmodel):
    fig = plotly.subplots.make_subplots(rows=1,
                                        cols=2,
                                        subplot_titles=('Raw Cycle',
                                                        'Smoothed Cycle'),
                                        shared_xaxes=True)
    marker = {'color': ['#0074D9']}
    if filename is None or filename == 'options':
        filename = 'ExampleData'
        database_sel = init_db
    else:
        database_sel = database
    data, raw_data = pop_with_db(filename, database_sel)
    datatype = data['datatype'].iloc[0]
    (cycle_ind_col, data_point_col, volt_col, curr_col, dis_cap_col,
     char_cap_col, charge_or_discharge) = col_variables(datatype)
    modset_name = filename.split('.')[0] + '-ModPoints'
    df_model = get_file_from_database(modset_name, database_sel)
    if df_model is not None:
        filt_mod = df_model[df_model[cycle_ind_col] == selected_step]

    if data is not None:
        filtered_data = data[data[cycle_ind_col] == selected_step]
    if raw_data is not None:
        raw_filtered_data = raw_data[raw_data[cycle_ind_col] == selected_step]

    for i in filtered_data[cycle_ind_col].unique():
        if data is not None:
            dff = filtered_data[filtered_data[cycle_ind_col] == i]
        if raw_data is not None:
            dff_raw = raw_filtered_data[raw_filtered_data[cycle_ind_col] == i]
        if df_model is not None:
            dff_mod = filt_mod[filt_mod[cycle_ind_col] == i]

        if data is not None:
            fig.append_trace(
                {
                    'x': dff[volt_col],
                    'y': dff['Smoothed_dQ/dV'],
                    'type': 'scatter',
                    'marker': marker,
                    'name': 'Smoothed Data'
                }, 1, 2)
        if raw_data is not None:
            fig.append_trace(
                {
                    'x': dff_raw[volt_col],
                    'y': dff_raw['dQ/dV'],
                    'type': 'scatter',
                    'marker': marker,
                    'name': 'Raw Data'
                }, 1, 1)
        if df_model is not None and showmodel == 'showmodel':
            fig.append_trace(
                {
                    'x': dff_mod[volt_col],
                    'y': dff_mod['Model'],
                    'type': 'scatter',
                    'name': 'Model'
                }, 1, 2)

        fig['layout']['showlegend'] = False
        fig['layout']['xaxis1'].update(title='Voltage (V)')
        fig['layout']['xaxis2'].update(title='Voltage (V)')
        fig['layout']['yaxis1'].update(title='dQ/dV')
        fig['layout']['yaxis2'].update(title='dQ/dV')
        fig['layout']['height'] = 600
        fig['layout']['margin'] = {'l': 40, 'r': 10, 't': 60, 'b': 200}
    return fig