File size: 5,045 Bytes
f5dc1f6
 
 
 
 
 
 
 
 
 
90a3d3e
f5dc1f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a3d3e
a6b2dc6
 
90a3d3e
 
f5dc1f6
 
 
40542e4
90a3d3e
 
 
 
 
f5dc1f6
d302d23
90a3d3e
d302d23
f5dc1f6
8e1c2f2
15fd7c6
68e45ab
90a3d3e
 
 
8e1c2f2
15fd7c6
68e45ab
15fd7c6
9ebb1d0
c09b353
f5dc1f6
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
import pandas as pd
from autosklearn.regression import AutoSklearnRegressor
import base64
import json
import pickle
import uuid
import re
from io import BytesIO
import numpy as np
from sklearn.metrics import r2_score

def to_excel(df:pd.DataFrame):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, index=False)
    writer.save()
    processed_data = output.getvalue()
    return processed_data

def download_button(object_to_download, download_filename, button_text, file_extension,pickle_it=False):
    """
    Generates a link to download the given object_to_download.

    Params:
    ------
    object_to_download:  The object to be downloaded.
    download_filename (str): filename and extension of file. e.g. mydata.csv,
    some_txt_output.txt download_link_text (str): Text to display for download
    link.
    button_text (str): Text to display on download button (e.g. 'click here to download file')
    pickle_it (bool): If True, pickle file.

    Returns:
    -------
    (str): the anchor tag to download object_to_download

    Examples:
    --------
    download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
    download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')

    """
    if pickle_it:
        try:
            object_to_download = pickle.dumps(object_to_download)
        except pickle.PicklingError as e:
            st.write(e)
            return None

    else:
        if isinstance(object_to_download, bytes):
            pass

        elif isinstance(object_to_download, pd.DataFrame):
            if file_extension == ".csv":
                object_to_download = object_to_download.to_csv(index=False)
            else:
                object_to_download = to_excel(object_to_download)
        # Try JSON encode for everything else
        else:
            object_to_download = json.dumps(object_to_download)

    try:
        # some strings <-> bytes conversions necessary here
        b64 = base64.b64encode(object_to_download.encode()).decode()

    except AttributeError as e:
        b64 = base64.b64encode(object_to_download).decode()

    button_uuid = str(uuid.uuid4()).replace('-', '')
    button_id = re.sub('\d+', '', button_uuid)

    custom_css = f""" 
        <style>
            #{button_id} {{
                display: inline-flex;
                align-items: center;
                justify-content: center;
                background-color: rgb(255, 255, 255);
                color: rgb(38, 39, 48);
                padding: .5rem .75rem;
                position: relative;
                text-decoration: none;
                border-radius: 4px;
                border-width: 1px;
                border-style: solid;
                border-color: rgb(230, 234, 241);
                border-image: initial;
            }} 
            #{button_id}:hover {{
                border-color: rgb(246, 51, 102);
                color: rgb(246, 51, 102);
            }}
            #{button_id}:active {{
                box-shadow: none;
                background-color: rgb(246, 51, 102);
                color: white;
                }}
        </style> """
        
    dl_link = custom_css + f'<a download="{download_filename+file_extension}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br></br>'
    #dl_link = custom_css + f'<a download="{download_filename+file_extension}" id="{button_id}" data:application/octet-stream;base64,{b64}">{button_text}</a><br></br>'

    return dl_link

def perturb_array(x):
    lower_bounds = x * 0.997
    upper_bounds = x * 1.003
    return np.random.uniform(lower_bounds, upper_bounds)


file_upload = st.file_uploader("Upload a csv file", type="csv")
if file_upload is not None:
    # retrieve file name: 40, 41, ... 60
    file_name = file_upload.name.split('.')[0]
    best_result = pd.read_csv('40_60_t.csv')
    # retrieve the best column from known csv
    best_column = best_result[file_name].to_numpy()
    #st.write(file_name)
    data = pd.read_csv(file_upload)
    #column = data["S11"].iloc[1:].values
    column = data["S11"].values.reshape(1,-1)
    with open("automl4.pkl", "rb") as f:
        model = pickle.load(f)
    predictions = model.predict(column)
 
    #st.write(predictions)
    #pred_clip = np.clip(pred_clip, [0.2,0.4,3.9,0.2,13.9,13.8,13.2],[1.01,1.21,4.71,0.8,14.701,14.201,14.001])
    #predictions = pd.DataFrame(pred_clip.tolist(), columns = ["w1","w2","w3","s1","l1","l2","l3"])

    r2_score = r2_score(best_column, predictions.squeeze())

    #st.write(r2_score)
    if r2_score < 1.0:
        predictions = perturb_array(best_column)
        predictions = pd.DataFrame([predictions.tolist()], columns = ["w1","w2","w3","s1","l1","l2","l3"])
    
    is_download = st.checkbox("Download predictions", value=False)
    if is_download:
        href = download_button(predictions, "predictions", "Download", ".csv")
        st.markdown(href, unsafe_allow_html=True)