File size: 7,413 Bytes
bacf16b
 
 
 
 
 
 
 
 
 
 
 
 
64e42c0
 
de5608c
82579ab
73cfaa5
 
 
 
bacf16b
 
 
 
82579ab
73cfaa5
64e42c0
bacf16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64e42c0
 
 
 
bacf16b
 
 
 
 
 
 
64e42c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bacf16b
 
 
 
 
64e42c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bacf16b
 
64e42c0
 
 
bacf16b
64e42c0
bacf16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82579ab
 
 
 
 
73cfaa5
bacf16b
82579ab
bacf16b
82579ab
de5608c
82579ab
73cfaa5
64e42c0
82579ab
 
 
 
 
 
 
bacf16b
 
 
 
64e42c0
bacf16b
 
 
de5608c
bacf16b
 
 
 
 
de5608c
bacf16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82579ab
bacf16b
de5608c
bacf16b
 
 
 
 
 
 
 
 
de5608c
 
 
 
73cfaa5
 
de5608c
 
 
 
73cfaa5
 
de5608c
 
73cfaa5
de5608c
73cfaa5
de5608c
 
 
73cfaa5
82579ab
de5608c
 
 
bacf16b
de5608c
 
 
 
bacf16b
de5608c
bacf16b
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import sys
import os
import pickle
import gzip
from pathlib import Path
import numpy as np
import torch
from scipy import stats
from gluformer.model import Gluformer
from utils.darts_processing import *
from utils.darts_dataset import *
import hashlib
from urllib.parse import urlparse
from huggingface_hub import hf_hub_download
import plotly.graph_objects as go
import gradio as gr
from format_dexcom import *
from typing import Tuple, Union, List
from plotly.graph_objs._figure import Figure
from gradio.components import Slider
from gradio.components import Markdown


glucose = Path(os.path.abspath(__file__)).parent.resolve()
file_directory = glucose / "files"
       
def plot_forecast(forecasts: np.ndarray, filename: str,ind:int=10) -> Tuple[Path, Figure]:
    
    forecasts = (forecasts - scalers['target'].min_) / scalers['target'].scale_

    trues = [dataset_test_glufo.evalsample(i) for i in range(len(dataset_test_glufo))]
    trues = scalers['target'].inverse_transform(trues)

    trues = [ts.values() for ts in trues]  # Convert TimeSeries to numpy arrays
    trues = np.array(trues)

    inputs = [dataset_test_glufo[i][0] for i in range(len(dataset_test_glufo))]
    inputs = (np.array(inputs) - scalers['target'].min_) / scalers['target'].scale_

    # Select a specific sample to plot

    samples = np.random.normal(
        loc=forecasts[ind, :],  # Mean (center) of the distribution
        scale=0.1,  # Standard deviation (spread) of the distribution
        size=(forecasts.shape[1], forecasts.shape[2])
    )
    

    # Create figure
    fig = go.Figure()

    # Plot predictive distribution
    for point in range(samples.shape[0]):
        kde = stats.gaussian_kde(samples[point,:])
        maxi, mini = 1.2 * np.max(samples[point, :]), 0.8 * np.min(samples[point, :])
        y_grid = np.linspace(mini, maxi, 200)
        x = kde(y_grid)

        # Create gradient color
        color = f'rgba(53, 138, 217, {(point + 1) / samples.shape[0]})'
        
        # Add filled area
        fig.add_trace(go.Scatter(
            x=np.concatenate([np.full_like(y_grid, point), np.full_like(y_grid, point - x * 15)[::-1]]),
            y=np.concatenate([y_grid, y_grid[::-1]]),
            fill='tonexty',
            fillcolor=color,
            line=dict(color='rgba(0,0,0,0)'),
            showlegend=False
        ))

    
    true_values = np.concatenate([inputs[ind, -12:], trues[ind, :]])
    true_values_flat=true_values.flatten()
    
    fig.add_trace(go.Scatter(
        x=list(range(-12, 12)),
        y=true_values_flat.tolist(),  # Convert to list explicitly
        mode='lines+markers',
        line=dict(color='blue', width=2),
        marker=dict(size=6),
        name='True Values'
    ))

    # Plot median
    forecast = samples[:, :]
    median = np.quantile(forecast, 0.5, axis=-1)

    fig.add_trace(go.Scatter(
        x=list(range(12)),
        y=median.tolist(),  # Convert to list explicitly
        mode='lines+markers',
        line=dict(color='red', width=2),
        marker=dict(size=8),
        name='Median Forecast'
    ))


    # Update layout
    fig.update_layout(
        title='Gluformer Prediction with Gradient for dataset',
        xaxis_title='Time (in 5 minute intervals)',
        yaxis_title='Glucose (mg/dL)',
        font=dict(size=14),
        showlegend=True,
        width=1000,
        height=600
    ) 

    # Save figure
    where = file_directory / filename
    fig.write_html(str(where.with_suffix('.html')))
    fig.write_image(str(where))

    return where, fig


def generate_filename_from_url(url: str, extension: str = "png") -> str:
    """
    :param url:
    :param extension:
    :return:
    """
    # Extract the last segment of the URL
    last_segment = urlparse(url).path.split('/')[-1]

    # Compute the hash of the URL
    url_hash = hashlib.md5(url.encode('utf-8')).hexdigest()

    # Create the filename
    filename = f"{last_segment.replace('.','_')}_{url_hash}.{extension}"

    return filename


glufo = None
scalers = None
dataset_test_glufo = None
filename = None

def prep_predict_glucose_tool(file: Union[str, Path], model_name: str = "gluformer_1samples_10000epochs_10heads_32batch_geluactivation_livia_mini_weights.pth") -> Tuple[Slider, Markdown]:
    """
    Function to predict future glucose of user.
    """
    global formatter, series, scalers, glufo, dataset_test_glufo, filename
    
    model = "Livia-Zaharia/gluformer_models"
    model_path = hf_hub_download(repo_id=model, filename=model_name)
    
    formatter, series, scalers = load_data(
        url=str(file), 
        config_path=file_directory / "config.yaml",
        use_covs=True,
        cov_type='dual',
        use_static_covs=True
    )

    formatter.params['gluformer'] = {
        'in_len': 96,  # example input length, adjust as necessary
        'd_model': 512,  # model dimension
        'n_heads': 10,  # number of attention heads########################
        'd_fcn': 1024,  # fully connected layer dimension
        'num_enc_layers': 2,  # number of encoder layers
        'num_dec_layers': 2,  # number of decoder layers
        'length_pred': 12  # prediction length, adjust as necessary represents 1 h
    }

    num_dynamic_features = series['train']['future'][-1].n_components
    num_static_features = series['train']['static'][-1].n_components

    global glufo
    glufo = Gluformer(
        d_model=formatter.params['gluformer']['d_model'],
        n_heads=formatter.params['gluformer']['n_heads'],
        d_fcn=formatter.params['gluformer']['d_fcn'],
        r_drop=0.2,
        activ='gelu',
        num_enc_layers=formatter.params['gluformer']['num_enc_layers'],
        num_dec_layers=formatter.params['gluformer']['num_dec_layers'],
        distil=True,
        len_seq=formatter.params['gluformer']['in_len'],
        label_len=formatter.params['gluformer']['in_len'] // 3,
        len_pred=formatter.params['length_pred'],
        num_dynamic_features=num_dynamic_features,
        num_static_features=num_static_features
    )

    device = "cuda" if torch.cuda.is_available() else "cpu"
    glufo.load_state_dict(torch.load(str(model_path), map_location=torch.device(device)))

    global dataset_test_glufo
    dataset_test_glufo = SamplingDatasetInferenceDual(
        target_series=series['test']['target'],
        covariates=series['test']['future'],
        input_chunk_length=formatter.params['gluformer']['in_len'],
        output_chunk_length=formatter.params['length_pred'],
        use_static_covariates=True,
        array_output_only=True
    )

    global filename
    filename = generate_filename_from_url(file)

    max_index = len(dataset_test_glufo) - 1

    print(f"Total number of test samples: {max_index + 1}")
    
    return (
        gr.Slider(
            minimum=0,
            maximum=max_index-1,
            value=max_index,
            step=1,
            label="Select Sample Index",
            visible=True
        ),
        gr.Markdown(f"Total number of test samples: {max_index + 1}", visible=True)
    )


def predict_glucose_tool(ind: int) -> Figure:
       
    
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    forecasts, _ = glufo.predict(
      dataset_test_glufo,
      batch_size=16,#######
      num_samples=10,
      device=device
    )
    figure_path, result = plot_forecast(forecasts,filename,ind)
    
    return result