示例#1
0
def baseline_correction(signal_seq, name="tissue"):
    '''
    input :
        signal sequence
    output :
        res : peak of signal
        base_signal: signal sequence without baseline shift

    '''
    base_signal = BaselineRemoval(signal_seq).IModPoly(2)  # getting off baseline shift

    if name == "tissue":
        left_side, right_side = truncate_tissue(base_signal)  # get the left and right boundary of peak indexes
    else:
        left_side, right_side = truncate_aif(base_signal)

    res = copy.deepcopy(base_signal)

    # --- pick peak ---

    #     res -= res[left_side - 1]

    res[:left_side] = 0
    res[right_side + 1:] = 0
    res[res < 0] = 0
    return res, base_signal
示例#2
0
def plot_feature(x, y, feature):
    """
    input: frequencies and intensities as lists (x, y). title is user defined title for features, FFT, PSD, etc.
    returns baseline smoothed spectrum with peak detection
    """
    baseObj=BaselineRemoval(y)
    y=baseObj.ModPoly(2)
    y = savgol_filter(y, 5, 2)
    peaks, _ = find_peaks(y)
    first_n_freq = x[peaks][np.argsort(-y[peaks])][0:5]
    first_n_int =  y[peaks][np.argsort(-y[peaks])][0:5]
    plt.plot(x, y)
    plt.plot(x[peaks], y[peaks], "x")
    plt.xlabel('Freq/Hz')
    plt.ylabel('amplitude')
    plt.title(feature)
    plt.show()    
示例#3
0
 def get_first_n_features(self,
                          x,
                          y,
                          n_peaks,
                          filter_order=5,
                          filter_poly=2):
     baseObj = BaselineRemoval(y)
     y = baseObj.ModPoly(filter_poly)
     y = savgol_filter(y, filter_order, filter_poly)
     peaks, _ = find_peaks(y)
     first_n_freq = x[peaks][np.argsort(-y[peaks])][0:n_peaks]
     first_n_int = y[peaks][np.argsort(-y[peaks])][0:n_peaks]
     zero_pad_freq = [0] * n_peaks
     zero_pad_int = [0] * n_peaks
     zero_pad_freq[:len(first_n_freq)] = first_n_freq
     zero_pad_int[:len(first_n_int)] = first_n_int
     freq_int = list(zero_pad_freq) + list(zero_pad_int)
     return freq_int
示例#4
0
 def baselineCorretion(self, data):
     data_len = len(data)
     x = np.arange(data_len) * self.partial_frec
     if self.use_baseline_correction:
         y = BaselineRemoval(data).ZhangFit(data_len)
         y = y - np.mean(y)
     else:
         y = data
     return x,  y
示例#5
0
def clean_and_convert(
    arr,
    zhang=True,
    reshape_length=False
):  # zhang is br, reshape_length is the desired data length
    ars_cleaned = []

    for i in range(len(arr)):
        if reshape_length is False:
            temp = arr[i]
        else:
            temp = arr[i][:-(len(arr[i]) - reshape_length)]

        if zhang is True:
            baseObj = BaselineRemoval(temp)
            temp = baseObj.ZhangFit()

        temp = (temp - np.min(temp)) / (np.max(temp) - np.min(temp))
        ars_cleaned.append(temp)

    return np.array(ars_cleaned)
示例#6
0
def clean_and_convert(arr, zhang=True, reshape_length=False):
    """
    clean_and_convert(arr, zhang=True, reshape_length=False)
    Description: for preparing data for model,
                 it can reshape data, apply baseline-removal and it normalizes to (0-1)
    Params: arr = Input array.
            zhang = Boolean value. If True baseline will be removed.
                    If false baseline will not be removed
            reshape_length = An int.
                             Determines the length of each trace, after cleaning.
                             If False, original length will be kept.
    Latest update: 03-06-2021. Added more comments.
    """

    ars_cleaned = []

    for i in range(len(arr)):

        # RESHAPE
        if reshape_length is False:
            temp = arr[i]
        else:
            temp = arr[i][:-(len(arr[i]) - int(reshape_length))]
        # -

        # BASELINE-REMOVAL
        if zhang is True:
            baseObj = BaselineRemoval(temp)
            temp = baseObj.ZhangFit()
        # -

        temp = (temp - np.min(temp)) / (np.max(temp) - np.min(temp)
                                        )  # Normalizes traces between 0-1
        ars_cleaned.append(temp)

    return np.array(ars_cleaned)
示例#7
0
    def BGR(self, data, method, polynomial_degree, lambda_, porder, itermax):
        '''Background correction of the data

        Args:
            data: data to background correct
            method:
                ZhangFit [https://doi.org/10.1039/B922045C]:
                    porder: signal-to-noise ratio (typically 3)
                    lambda_ : smoothing-factor for the background (typically in the order of 500)
                    itermax: number of iterations
                ModPoly [https://doi.org/10.1366/000370203322554518]
                ModPoly [https://doi.org/10.1366/000370207782597003]
                    polynomial_degree: degree of the polynomial for background correction (typically 3)

        Returns:
            Background corrected data

        Raises:
            Method not found. Possible options are: ZhangFit, ModPoly and IModPoly. Check spelling.
        '''

        baseObj = BaselineRemoval(data)
        if method == 'ZhangFit':
            BGR_output = baseObj.ZhangFit(lambda_=lambda_,
                                          porder=porder,
                                          itermax=itermax)
        elif method == 'ModPoly':
            BGR_output = baseObj.ModPoly(polynomial_degree)
        elif method == 'IModPoly':
            BGR_output = baseObj.IModPoly(polynomial_degree)
        else:
            raise Exception(
                'Method not found. Possible options are: ZhangFit, ModPoly and IModPoly. Check spelling.'
            )

        return BGR_output
示例#8
0
def flatten_spectra(da, method):
    """
    flattening of a spectrum
    
    Arguments :
        da (xarray): spectrum xarray
        method (tuple): tuple (method,*param)
        
    Returns:
        z (np array): flattened spectrum
        z_base (np array): baseline
    """

    # 3rd party dependecies
    from BaselineRemoval import BaselineRemoval

    method_type, *param = method

    if method_type == "linear":
        z, z_base = supress_baseline(da.lbd.values, da.values, *param)

    elif method_type == "top hat":
        top_hat_bandwith = param[0]
        factor = top_hat_bandwith / da.lbd.attrs["spectral_resolution"] / len(
            da.lbd)
        z = topHat(da, factor=factor)
        z_base = da.values - z

    elif method_type == "ials":
        z_base = baseline_ials(da.lbd.values, da.values, *param)
        z = da.values - z_base

    elif method_type == "als":
        z_base = baseline_als(da.values, *param)
        z = da.values - z_base

    elif method_type == "arPLS":
        z_base = baseline_arPLS(da.values, *param)
        z = da.values - z_base

    elif method_type == "drPLS":
        z_base = baseline_drPLS(da.values, *param)
        z = da.values - z_base

    elif method_type == "rubberband":
        z_base = rubberband(da.lbd.values, da.values)
        z = da.values - z_base

    elif method_type == "Modpoly":
        baseObj = BaselineRemoval(da.values, *param)
        z = baseObj.ModPoly()
        z_base = da.values - z

    elif method_type == "Imodpoly":
        baseObj = BaselineRemoval(da.values, *param)
        z = baseObj.IModPoly()
        z_base = da.values - z

    else:
        raise Exception(f"unknown method:{method_type}")

    return z, z_base
示例#9
0
def baselineCorrection(id):
    df = getSeriesDataframe(id, 1)
    df.iloc[:,3:] = df.iloc[:,3:].apply(lambda x: BaselineRemoval(x).ZhangFit(), axis=1, result_type='expand')
    df.round(3).to_csv(f'./csv/corrected/{id}.csv', index=False)
示例#10
0

window.mainloop()

a = int(y1)
b = int(y2)
df = pd.read_excel(path)
#importing the x and y values from the dataframe
lambda_values = df.iloc[:,0:1].values
input_array = df.iloc[:,1:2].values
#the input array has to be in the form of a list in order for the methods to work
input_array = input_array.transpose()
input_array = input_array.tolist()
input_array = input_array[0]

#to check and apply smoothing if required
if y3 == '' or z == 0:
    to_baseline_correct = input_array
else:
    s = int(y3)
    to_baseline_correct = savgol_filter(input_array, s, 2)

#baseline removal
baseObj=BaselineRemoval(to_baseline_correct)
Output=baseObj.ZhangFit()

#plotting the spectra
plt.title('Output spectra')
plt.plot(lambda_values, Output)
plt.xlim([a,b])
示例#11
0
def data_treatments(contents, filename, wl_chosen, snv_value, toggle_smooth, S_M, P, pca_v, toggle_derivative, d_selector,toggle_baseline, bl_selector, d_p, d_n, pcn, cvn):
    if contents is not None:
        df = parse_data(contents, filename)
        spectra1 = df.groupby(['Concentration(g/100g/solvents)', 'Temperature'], as_index=False).mean()
        spectra = spectra1.drop(["Concentration(g/100g/solvents)", "Temperature"], axis=1)
        names = []
        for i in range(len(spectra1)):
            names.append("Concentration : %s Temp: %s" % ((spectra1.iloc[i, 0]), (spectra1.iloc[i, 1])))
        wavelengths = pd.DataFrame(spectra.columns.values.astype(int))
        wavelengths.columns = ['wavelength']
        spectra = spectra.T.reset_index(drop=True)
        df1 = pd.concat([wavelengths, spectra], axis=1)

        high = wavelengths[wavelengths['wavelength'] == wl_chosen[0]].index.values
        low = wavelengths[wavelengths['wavelength'] == wl_chosen[1]].index.values
        dff = df1.iloc[low[0]:high[0], :]
        dff2 = spectra.iloc[low[0]:high[0], :]

        dataPanda1 = []
        for i in range(len(dff2.columns)):
            text = names[i]
            trace = (go.Scattergl(x=dff["wavelength"], y=dff2.iloc[:, i], name=text, text=text,
                                  hoverinfo='text'))
            dataPanda1.append(trace)

        layout1 = go.Layout(
            hovermode='closest',
            title='Raman Shift: Untreated Data',
        )

        fig_raw_data = dict(data=dataPanda1, layout=layout1)

        polynomial_degree = 3  # only needed for Modpoly and IModPoly algorithm


        if toggle_baseline == 'Apply Baseline':
            baseline_data = [] 
            for i in range(len(dff2.columns)):
                Modpoly_output=[]
                baseObj = BaselineRemoval(dff2.iloc[:, i])

                if bl_selector == 'MP':
                    Modpoly_output = pd.DataFrame(baseObj.ModPoly(polynomial_degree) )
                elif bl_selector == 'IMP':
                    Modpoly_output = pd.DataFrame(baseObj.IModPoly(polynomial_degree) )
                elif bl_selector == 'ZF':
                    Modpoly_output = pd.DataFrame(baseObj.ZhangFit() )
                baseline_data.append(Modpoly_output)

            baseline_data=pd.concat(baseline_data,axis=1)
            baseline_data.reset_index()
            base = dff2.iloc[:, 1]- baseline_data.iloc[:, 1]

            fig_baseline = px.line(x=dff["wavelength"], y=dff2.iloc[:, 1])
            fig_baseline.add_trace(go.Scatter(x=dff["wavelength"], y=base, mode='lines'))
            fig_baseline.update_layout(
                title={
                    'text': "BaseLine Correction Fit",
                    'y': 0.9,
                    'x': 0.5,
                    'xanchor': 'center',
                    'yanchor': 'top'}),

            fig_baseline.update_layout(showlegend=False)
            fig_baseline_removed = px.line(x=dff["wavelength"], y=baseline_data.iloc[:,1])
            fig_baseline_removed.update_layout(
                title={
                    'text': "Baseline Corrected",
                    'y': 0.9,
                    'x': 0.5,
                    'xanchor': 'center',
                    'yanchor': 'top'}),

            fig_baseline_removed.update_layout(showlegend=False)
            dff2=baseline_data

        else:
            fig_baseline = go.Figure(data=[])
            fig_baseline.update_layout(
                title={
                    'text': "Baseline Correction Not Selected",
                    'y': 0.9,
                    'x': 0.5,
                    'xanchor': 'center',
                    'yanchor': 'top'}),
            fig_baseline_removed = go.Figure(data=[])
            fig_baseline_removed.update_layout(
                title={
                    'text': "Baseline Correction Not Selected",
                    'y': 0.9,
                    'x': 0.5,
                    'xanchor': 'center',
                    'yanchor': 'top'}),

        if snv_value == 'SNV':
            dff2 = pd.DataFrame(snv(dff2.T.values)).T

        if toggle_smooth == 'Smoothing':
            dff2 = pd.DataFrame(savgol_filter(dff2.T.values, S_M, polyorder=P, deriv=0, axis=1)).T

        if toggle_derivative == 'Apply Derivative':
            if d_selector == "FD":
                dff2 = pd.DataFrame(savgol_filter(dff2.T.values, d_n, polyorder=d_p, deriv=1, axis=1)).T
            elif d_selector == 'SD':
                dff2 = pd.DataFrame(savgol_filter(dff2.T.values, d_n, polyorder=d_p, deriv=2, axis=1)).T

        dataPanda = []

        for i in range(len(dff2.columns)):
            text = names[i]
            trace = (go.Scattergl(x=dff["wavelength"], y=dff2.iloc[:, i], name=text, text=text,
                                  hoverinfo='text'))
            dataPanda.append(trace)

        layout2 = go.Layout(
            hovermode='closest',
            title='Raman Shift: Treated Data',
        )

        fig_treated_data = dict(data=dataPanda, layout=layout2)

        xdata = (dff2.T.values)
        X = StandardScaler().fit_transform(xdata)
        pca = PCA(n_components=4)
        pca.fit_transform(X)
        pca_data = pd.DataFrame(pca.transform(X))  # get PCA coordinates for scaled_data
        per_var = pd.DataFrame(np.round(pca.explained_variance_ratio_ * 100, decimals=1), columns=['Per_Var'])
        labels_df = pd.DataFrame(['PC' + str(x) for x in range(1, len(per_var) + 1)], columns=['PC'])
        per_var_df = pd.concat([per_var, labels_df], axis=1)
        labels = ['PC' + str(x) for x in range(1, len(per_var) + 1)]
        pca_data.columns = labels
        scaler = MinMaxScaler()
        loading_scores = pd.DataFrame(scaler.fit_transform((pd.Series(pca.components_[0])).values.reshape(-1, 1)))
        cum_var = pd.DataFrame(np.cumsum(per_var.values))

        if pca_v == "Temp":
            spectra1['Temperature'] = spectra1['Temperature'].astype(str)
            fig_biplot = px.scatter(pca_data, x='PC1', y='PC2', color=spectra1['Temperature'],
                               custom_data=[spectra1['Concentration(g/100g/solvents)']],
                               )
            fig_biplot.update_traces(
                hovertemplate="<br>".join([
                    "PC1: %{x}",
                    "PC2: %{y}",
                    "Concentration (g/100g solvent): %{customdata[0]}",
                    "Temperature: %{text}",

                ]),
                text=spectra1['Temperature']
            ),
            fig_biplot.update_layout(legend_title_text='Temperature')
            fig_biplot.update_traces(marker=dict(size=12))

        elif pca_v == "Conc":
            Conc = spectra1['Concentration(g/100g/solvents)']
            fig_biplot = px.scatter(pca_data, x='PC1', y='PC2', color=Conc)
            fig_biplot.update_layout(coloraxis_colorbar=dict(
                title="Concentration"))
            fig_biplot.update_traces(marker=dict(size=12))

        fig_biplot.update_layout(
            title={
                'text': "Bi-plot",
                'y': 0.9,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'})

        fig_cumvar = px.bar(per_var_df, x='PC', y='Per_Var',
                       labels={'PC': 'Principal Component #', 'Per_Var': 'Percentage Total Variance (%)'}, color='PC')
        fig_cumvar.add_trace(go.Scatter(x=per_var_df['PC'], y=cum_var.iloc[:, 0]))
        fig_cumvar.update_layout(
            title={
                'text': "Scree Plot",
                'y': 0.9,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'}),

        fig_cumvar.update_layout(showlegend=False)

        fig_loadings = px.line(x=dff["wavelength"], y=loading_scores.iloc[:, 0], labels=dict(x="Wavelength", y="Loadings"))

        fig_loadings.update_layout(
            title={
                'text': "Loadings Plot",
                'y': 0.9,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'})

        dataPanda3 = []

        for i in range(len(dff2.columns)):
            text = names[i]
            trace = (go.Scattergl(x=dff["wavelength"], y=dff2.iloc[:, i], name=text, text=text,
                                  hoverinfo='text'))
            dataPanda3.append(trace)

        layout2 = go.Layout(
            hovermode='closest',
            title='Raman Shift: Treated Data',
            showlegend=False,
        )

        fig_data_treatment_PCA = dict(data=dataPanda3, layout=layout2)

        xdata1 = (dff2.T.values)
        X1 = StandardScaler().fit_transform(xdata1)
        y_1 = spectra1['Concentration(g/100g/solvents)']
        pls = PLSRegression(n_components=pcn)
        pls.fit(X1, y_1)
        y_cv = cross_val_predict(pls, X1, y_1, cv=cvn)
        y_c = pd.DataFrame(pls.predict(X1))
        cv = pd.concat([y_1, y_c], axis=1)
        cv.columns = ["Actual", "Predicted"]
        score_cv = round(r2_score(y_1, y_cv), 3)
        score = round(r2_score(y_1, y_c), 3)
        rmse = round((mean_squared_error(y_1, y_cv)) ** 0.5, 3)
        rmsep = round((mean_squared_error(y_1, y_c)) ** 0.5, 3)
        spectra1['Temperature'] = spectra1['Temperature'].astype(str)
        plscof = pd.DataFrame(pls.coef_[:, 0])
        dff = dff.reset_index()
        coeff = pd.concat([dff["wavelength"], plscof], axis=1)
        coeff.columns = ["wavelength", "coeffs"]
        fig_PLS_pred = px.scatter(cv, x="Predicted", y="Actual", color=spectra1['Temperature'])
        regline = sm.OLS(cv["Actual"], sm.add_constant(cv["Predicted"])).fit().fittedvalues
        fig_PLS_pred.add_traces(go.Scatter(x=cv["Predicted"], y=regline,
                                    mode='lines',
                                    marker_color='black',
                                    name='Best Fit')
                         ),
        fig_PLS_pred.update_layout(
            title={
                'text': "Predicted vs Actual Conc",
                'y': 0.9,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'})

        fig_PLS_coeff = px.line(coeff, x="wavelength", y="coeffs")
        fig_PLS_coeff.update_layout(
            title={
                'text': "PLS Coefficients",
                'y': 0.9,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'})
    else:
        fig_raw_data = go.Figure(go.Scatter(x=1, y=1))
        fig_treated_data = go.Figure(data=[])
        fig_biplot = go.Figure(data=[])
        fig_cumvar = go.Figure(data=[])
        fig_loadings = go.Figure(data=[])
        fig_data_treatment_PCA = go.Figure(data=[])
        fig_PLS_pred = go.Figure(data=[])
        fig_PLS_coeff = go.Figure(data=[])
        fig_baseline = go.Figure(data=[])
        fig_baseline_removed = go.Figure(data=[])
        score_cv = []
        score = []
        rmse = []
        rmsep = []

    return fig_raw_data, fig_treated_data, fig_biplot, fig_cumvar, fig_loadings, fig_data_treatment_PCA, fig_PLS_pred, \
           html.H3('{0:.2f}'.format(score_cv), style={'textAlign': 'center'}), \
           html.H3('{0:.2f}'.format(rmse), style={'textAlign': 'center'}), \
           html.H3('{0:.2f}'.format(score), style={'textAlign': 'center'}), \
           html.H3('{0:.2f}'.format(rmsep), style={'textAlign': 'center'}), \
           fig_PLS_coeff, fig_baseline, fig_baseline_removed
示例#12
0
 def remove_baseline(self, poly_order=24):
     x = np.real(self.spectrum)
     baseline_object = BaselineRemoval(x)
     Modpoly_output = baseline_object.ModPoly(poly_order)
     Imodpoly_output = baseline_object.IModPoly(poly_order)
     self.spectrum = baseline_object.ZhangFit()
for substance, times in substances.items():
    axs[1].axvspan(*times,
                   facecolor=next(color),
                   edgecolor='None',
                   alpha=0.5,
                   label=substance)

axs[1].legend(title='Application')
axs[1].set_title('After the alignment procedure')

axs[1].set(xlabel='time [min]')

fig.savefig(experiment_name + '/General_traces.pdf')

# Finding variables required for calculating amplitude, areas under peaks, half-widths
aligned = BaselineRemoval(contrac_aligned)
zhang = aligned.ZhangFit()

# Determine threshold height of the peak
max_val = np.where(zhang == zhang.max())[0][0]
peaks_height = (zhang.max() - zhang[max_val - peaks_distance:max_val +
                                    peaks_distance].min()) * 0.2

peaks, peak_values = find_peaks(zhang,
                                height=peaks_height,
                                distance=peaks_distance)
widths, widths_heights, left, right = peak_widths(zhang,
                                                  peaks,
                                                  rel_height=width_height)
left = left.astype(int)
right = right.astype(int)
示例#14
0
def flatten_hyperspectra(da, method):
    """
    Baseline supression of all the spectra of an hyperspectra.
    
    Arguments :
        da (xarray): hyperspectrum to flatten
        method (tuple): (method,*param)
        
    Returns:
        da (xarray):  flattened hyperspectrum
    """

    # 3rd party imports
    import numpy as np
    from tqdm import trange
    from BaselineRemoval import BaselineRemoval

    # Local application import
    from raman_hyperspectra.raman_hyperspectra_read_files import construct_xarray

    method_type, *param = method

    X = da.data.reshape((da.shape[0] * da.shape[1], da.shape[2]))

    zth = []
    if method_type == "linear":
        for i in trange(da.shape[0] * da.shape[1], desc="linear flattening"):
            z, _ = supress_baseline(da.lbd.values, X[i, :], *param)
            zth.append(z)

    elif method_type == "top hat":
        top_hat_bandwith = param[0]
        factor = top_hat_bandwith / da.lbd.attrs["spectral_resolution"] / len(
            da.lbd)
        for i in trange(da.shape[0] * da.shape[1], desc="tophat flattening"):
            zth.append(topHat(X[i, :], factor=factor))

    elif method_type == "ials":
        for i in trange(da.shape[0] * da.shape[1], desc="ials flattening"):
            z_base = baseline_ials(da.lbd.values, X[i, :], *param)
            zth.append(X[i, :] - z_base)

    elif method_type == "als":
        for i in trange(da.shape[0] * da.shape[1], desc="als flattening"):
            z_base = baseline_als(X[i, :], *param)
            zth.append(X[i, :] - z_base)

    elif method_type == "arPLS":
        for i in trange(da.shape[0] * da.shape[1], desc="arPLS flattening"):
            z_base = baseline_arPLS(X[i, :], *param)
            zth.append(X[i, :] - z_base)

    elif method_type == "drPLS":
        for i in trange(da.shape[0] * da.shape[1], desc="drPLS flattening"):
            z_base = baseline_drPLS(X[i, :], *param)
            zth.append(X[i, :] - z_base)

    elif method_type == "rubberband":
        for i in trange(da.shape[0] * da.shape[1],
                        desc="rubberband flattening"):
            z_base = rubberband(da.lbd.values, X[i, :])
            zth.append(X[i, :] - z_base)

    elif method_type == "Modpoly":
        for i in trange(da.shape[0] * da.shape[1], desc="Modpoly flattening"):
            baseObj = BaselineRemoval(X[i, :], *param)
            Modpoly_output = baseObj.ModPoly()
            zth.append(X[i, :] - z_base)

    elif method_type == "Imodpoly":
        for i in trange(da.shape[0] * da.shape[1], desc="Imodpoly flattening"):
            baseObj = BaselineRemoval(X[i, :], *param)
            Imodpoly_output = baseObj.IModPoly()
            zth.append(X[i, :] - z_base)

    else:
        raise Exception(f"unknown method:{method_type}")

    try:
        tool = da.attrs["tool"]

    except:
        tool = "unknown"

    try:
        hyperspectra_name = da.attrs["hyperspectra_name"]

    except:
        tool = "unknown"

    da = construct_xarray(
        np.array(zth).reshape((da.shape)),
        range(da.shape[0]),
        range(da.shape[1]),
        da["lbd"].data,
        tool=tool,
        hyperspectra_name=hyperspectra_name,
    )
    da.attrs["flattening method"] = method_type

    return da

st.subheader('The files loaded need to be in a specific shape:')
st.subheader(' - 3 columns, the first one with the X coordinates')
st.subheader(
    ' - the second one as dummy column (could be filled with random numbers does not matter)'
)
st.subheader(' - the third one with the Y values')
uploadfile = st.file_uploader('load zip file here', 'zip')

if uploadfile:
    files, files_names = load_func(uploadfile)

    for file in files:
        #baseline removal:
        baseObj = BaselineRemoval(files[file][2])
        Zhangfit_output = baseObj.ZhangFit()
        files[file][3] = Zhangfit_output

    name = st.selectbox('Select spectrum:', files_names)

    p1 = figure(title='', x_axis_label='', y_axis_label=''
                )  #, x_axis_type = scale_logx, y_axis_type = scale_logy)
    p1.line(files[name][0], files[name][2], line_width=2, color=colori[1])
    p1.line(files[name][0],
            files[name][2] - files[name][3],
            line_width=2,
            color=colori[0])
    st.bokeh_chart(p1, use_container_width=True)

    p1 = figure(title='', x_axis_label='', y_axis_label=''
    plt.plot(potential, current, label="True Data: " + base, color='C0')

    # If We use the CHI Peaks, Skip Peak Detection
    if useCHIPeaks and Ip != None and Vp != None:
        # Set Axes Limits
        axisLimits = [
            min(current) - min(current) / 10,
            max(current) + max(current) / 10
        ]

    # Else, Perform baseline Subtraction to Find the peak
    else:
        # Get Baseline
        if backwardScan:
            if useAPI:
                baseObj = BaselineRemoval(-current)
                baseline = current + baseObj.ModPoly(order)
            else:
                baseline = -getBase(potential, -current, Iterations, order)
        else:
            if useAPI:
                baseObj = BaselineRemoval(current)
                baseline = current - baseObj.ModPoly(order)
            else:
                baseline = getBase(potential, current, Iterations, order)
        baselineCurrent = current - baseline

        # Plot Subtracted baseline
        plt.plot(potential,
                 baselineCurrent,
                 label="Current After Baseline Subtraction",

'get the Measured ir spectrum into good shape'
data_path_ir_g = "11 dichloro ethene IR.txt"
data_ir_g = np.genfromtxt(data_path_ir_g, dtype=float , delimiter=None, encoding='utf-8' ,autostrip=True , skip_header=21)
const_ir_trans = 0.01  #constant for scaling transmittance
const_ir_x = 0.95
x_ir_g=data_ir_g[0:,0] * const_ir_x
y_ir_g=np.exp(-data_ir_g[0:,1]*const_ir_trans)

'fit the gauss view ir spectrum'
data_path_ir = "gem_dce_ir.csv"
data_ir = np.genfromtxt(data_path_ir, dtype=float , delimiter=',', encoding='utf-8' ,autostrip=True , skip_header=2)
x_ir=data_ir[0:,0]
input_array_ir = 1-data_ir[0:,1]
baseObj_ir=BaselineRemoval(input_array_ir)
y_ir=((1-baseObj_ir.ZhangFit())/100) + 1

'plot the gauss raman spectrum'
data_path_ra_g = "11 dichloro raman.txt"
data_ra_g = np.genfromtxt(data_path_ra_g, dtype=float , delimiter=None, encoding='utf-8' ,autostrip=True , skip_header=21)
const_ra_trans = 700  #constant for scaling transmittance
const_ra_x = 0.95
x_ra_g=data_ir_g[0:,0] * const_ir_x
y_ra_g=(data_ra_g[0:,1]*const_ra_trans)


'fit the measured view ra spectrum'
data_path_ra = "dce_isomer_C_raman.csv"
data_ra = np.genfromtxt(data_path_ra, dtype=float , delimiter=',', encoding='utf-8' ,autostrip=True , skip_header=2)
x_ra=(data_ra[0:,0] - 0.28)