Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
import plotly.express as px | |
import numpy as np | |
from numpy.core.multiarray import zeros | |
def upload_file(files): | |
file_paths = [file.name for file in files] | |
df = pd.read_excel(file_paths[0]) | |
return file_paths,og_Data.update(value=df, visible=True) | |
def plot(df, one, two, element): | |
print("plotting") | |
df.set_index(df.columns[0]) | |
df2=df.transpose() | |
header=df2.iloc[0] | |
df2=df2[1:] | |
df2.columns=header | |
df2.reset_index() | |
plot=px.line(df2, x=df2.index,y=df2.columns, markers=True, title=f"<sup>{one}/{two}</sup>{element} Normalization", | |
labels={ | |
"index":f'<sup>i</sup>{element}', | |
"value":f"ε<sup>i</sup>{element}" | |
}) | |
return gr.update(value=plot, visible=True) | |
def norm(df, i, j, u, v): | |
df1= np.zeros_like(df) | |
df2=pd.DataFrame(data=df) | |
normalized= pd.DataFrame(df1) | |
normalized.columns = df2.columns | |
normalized[normalized.columns[0]]=df[df.columns[0]] | |
for ii in range(1,df2.shape[1]): | |
k= df2.columns[ii]; | |
md=(k-u)/(u-v) | |
for jj in range(0,df2.shape[0]): | |
normalized[k][jj]= df[k][jj]-df[u][jj]+((df[v][jj]-df[u][jj])*md) | |
return gr.update(value=normalized, visible=True), export.update(visible=True) | |
def write_xl(norm,u,v, element): | |
norm.to_excel(f'{u}.{v} {element} Normalization.xlsx') | |
return | |
with gr.Blocks() as demo: | |
#gr.Markdown() | |
with gr.Row(): | |
Element= gr.Textbox(label="Element", placeholder="Enter element symbol (e.g. Mo)") | |
i= gr.Number(label="Current Normalizing Isotope #1", info= "(e.g. 98 for 98/96)", precision=0) | |
j= gr.Number(label="Current Normalizing Isotope #2", info= "(e.g. 96 for 98/96)", precision=0) | |
u=gr.Number(label="Desired Normalizing Isotope #1", info= "(e.g. 92 for 92/98)", precision=0) | |
v=gr.Number(label="Desired Normalizing Isotope #2", info= "(e.g. 98 for 92/98)", precision=0) | |
#gr.Markdown() | |
with gr.Row(): | |
upload_button = gr.UploadButton("Click to Upload and Transform Data", file_types=[".xlsx"], file_count=1) | |
Plot_button=gr.Button("Click to Plot") | |
#Plot1_button=gr.Button("Click to Plot Original Data") | |
file_output = gr.File(visible=False) | |
with gr.Row(): | |
og_Data = gr.DataFrame(type="pandas", visible=False) | |
upload_button.upload(upload_file, upload_button, outputs=[ file_output,og_Data]) | |
with gr.Row(): | |
with gr.Column(): | |
og_Plot= gr.Plot(label="Original Data", visible=False) | |
Plot_button.click(fn=plot, inputs=[og_Data, i, j, Element], outputs=og_Plot, scroll_to_output=True) | |
with gr.Column(): | |
norm_Plot= gr.Plot(label="Re-normalized Data", visible=False) | |
with gr.Row(): | |
norm_Data=gr.Dataframe(type="pandas", visible=False) | |
Plot_button.click(fn=plot, inputs=[norm_Data, u, v, Element], outputs=norm_Plot) | |
with gr.Row(): | |
with gr.Column(scale=3): | |
gr.Button(visible=False,interactive=False) | |
with gr.Column(scale=3): | |
gr.Button(visible=False,interactive=False) | |
with gr.Column(scale=1.1): | |
export=gr.Button("Click to export normalized data as .xslx file",visible=False).style(full_width=False, size="sm") ##define function to write excel | |
export.click(fn=write_xl, inputs=[norm_Data, u, v, Element]) | |
upload_button.upload(upload_file, upload_button, outputs=[ file_output,og_Data]).success( | |
fn=norm, inputs=[og_Data, i, j, u, v],outputs=[norm_Data,export]) | |
demo.launch(debug=True) |