File size: 11,130 Bytes
91f863a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import pandas as pd
import numpy as np

from sklearn.linear_model import LinearRegression

import plotly.graph_objects as go
from plotly.subplots import make_subplots

rangeLabel_col = ["MW_range", "PSA_range", "NRB_range", "HBA_range", "HBD_range", "LogP_range"]
superscript_map = {
    0: "\u2070",
    1: "\u00B9",
    2: "\u00B2",
    3: "\u00B3",
    4: "\u2074",
    5: "\u2075",
    6: "\u2076",
    7: "\u2077",
    8: "\u2078",
    9: "\u2079"
}


def draw_plot(result_path:str, save_path:str):
    ## -- predict plot save -- ##
    # df_plot = pd.read_csv(os.path.join("../", result_path))
    df_plot = pd.read_csv(result_path)
    labels, predict = df_plot['Clint'], df_plot['predict']
    x_ax = list(range(len(labels)))

    fig = make_subplots(rows=2, cols=1)

    fig.add_trace(go.Scatter(x=x_ax, y=labels,
                        mode='lines',
                        name='labels',
                        legendgroup="1"), row=1, col=1)
    fig.add_trace(go.Scatter(x=x_ax, y=predict,
                        mode='lines',
                        name='predicted',
                        legendgroup="1"), row=1, col=1)


    ## -- Compare model result Scatter plot -- ##
    model = LinearRegression().fit(np.array(labels).reshape(-1,1), (np.array(predict)))
    y_hat = model.predict(np.array(labels).reshape(-1,1))

    fig.add_trace(go.Scatter(x=labels, y=predict,
                            mode='markers',
                            legendgroup="2"), row=2, col=1)

    fig.add_trace(go.Scatter(x=labels, y=y_hat,
                            mode="lines",
                            name="trend line",
                            line=dict(shape="linear", color="red", width=2, dash="dot"),
                            legendgroup="2"), row=2, col=1)


    ideal_list = list(range(-1, int(max(labels)) + 3))
    ideal_x, ideal_y = ideal_list, ideal_list

    ideal_x_rev = ideal_x[::-1]
    ideal_y_uper,ideal_y_lower = [val+1 for val in ideal_y], [val-1 for val in ideal_y]
    ideal_y_lower = ideal_y_lower[::-1]

    ideal_y_uper_dot,ideal_y_lower_dot = [val+0.5 for val in ideal_y], [val-0.5 for val in ideal_y]
    ideal_y_lower_dot = ideal_y_lower_dot[::-1]

    fig.add_trace(go.Scatter(x=ideal_x, y=ideal_y,
                            mode="lines",
                            name="ideal",
                            line=dict(shape="linear", color="black", width=2, dash="dot"),
                            legendgroup="2"), row=2, col=1)

    fig.add_trace(go.Scatter(x=ideal_x + ideal_x_rev, y=ideal_y_uper + ideal_y_lower,
                            mode="lines",
                            name="margin 1",
                            line=dict(shape="linear", color='rgba(231,107,243,0.8)', width=2, dash="dashdot"),
                            legendgroup="2"), row=2, col=1)

    fig.add_trace(go.Scatter(x=ideal_x + ideal_x_rev, y=ideal_y_uper_dot + ideal_y_lower_dot,
                            mode="lines",
                            name="margin 2",
                            line=dict(shape="linear", color='rgba(0,176,246,0.8)', width=2, dash="dash"),
                            legendgroup="2"), row=2, col=1)

    fig.update_layout(
        height=800, 
        width=800, 
        title_text="Compare Observed Clint to Prediction results", 
        xaxis1_title = 'Data Length',
        yaxis1_title = 'Clint_logScale',
        xaxis2_title = 'Observed',
        yaxis2_title = 'Predicted',
        legend_tracegroupgap = 320,
        xaxis2_range=[-0.5, int(max(labels)) + 1],
        yaxis2_range=[-0.5, int(max(labels)) + 1],
    )
    
    # fig.write_image(os.path.join("../", save_path))
    fig.write_image(save_path)


def draw_boundaryplot(df_plot:pd.DataFrame, save_path:str, df_duplData:pd.DataFrame = None):
    labels, predict = np.expm1(df_plot["Clint"]), np.expm1(df_plot["predict"])
    labels, predict = np.log10(labels), np.log10(predict)

    # labels, predict = df_plot["Clint"], df_plot["predict"]
        
    variance_list =[abs(data - labels[idx])/predict.mean() for idx, data in enumerate(predict)]
    df_plot["variance"] = variance_list

    variance_boundary = 0.5
    in_labels, in_predict = df_plot[df_plot["variance"] <= variance_boundary]['Clint'], df_plot[df_plot["variance"] <= variance_boundary]['predict']
    out_labels, out_predict = df_plot[df_plot["variance"] > variance_boundary]['Clint'], df_plot[df_plot["variance"] > variance_boundary]['predict']

    x_extended = np.linspace(df_plot['Clint'].min() - 1, df_plot['Clint'].max() + 1, 200)

    grad, bias = np.polyfit(labels, predict, 1)  
    yhat = grad* x_extended + bias

    in_grad, in_bias = np.polyfit(in_labels, in_predict, 1)  
    in_yhat = in_grad* x_extended + in_bias

    ## -- y축: 예측치, x축: 관측치 -- ##
    scatter = go.Scatter(x=labels, y=predict,
                            mode='markers',
                            name="Predict_point")

    out_scatter = go.Scatter(x=out_labels, y=out_predict,
                            mode='markers',
                            name="out_bound",
                            )

    trend_line = go.Scatter(x=x_extended, y=yhat,
                            mode="lines",
                            name="trend line",
                            line=dict(shape="linear", color="red", width=2, dash="dot"))

    in_trend_line = go.Scatter(x=x_extended, y=in_yhat,
                            mode="lines",
                            name="in-variance trend line",
                            line=dict(shape="linear", color="olive", width=2, dash="dot"))

    ## -- log scale된 데이터의 Ideal 기울기와 fold 기울기 -- ##
    ideal_list = list(range(-1, int(max(labels)) + 3))
    ideal_x, ideal_y = ideal_list, ideal_list

    ideal_line = go.Scatter(x=ideal_x, y=ideal_y,
                                mode="lines",
                                name="ideal",
                                line=dict(shape="linear", color="black", width=2))
        
    ## -- 2-fold 기울기 : 관측치 대비 예측치가 /2 ~ *2인 범위
        
    fold2_top = [x + np.log10(2.0) for x in ideal_list]
    fold2_bottom = [x - np.log10(2.0) for x in ideal_list]

    fold2_topline = go.Scatter(x=ideal_list, y=fold2_top,
                            mode="lines",
                            name="2-fold",
                            line=dict(shape="linear", color="black", width=2, dash="dash"))
    fold2_bottomline = go.Scatter(x=ideal_list, y=fold2_bottom,
                            mode="lines",
                            name="2-fold",
                            showlegend =False,
                            line=dict(shape="linear", color="black", width=2, dash="dash"))


    fold5_top = [x + np.log10(5.0) for x in ideal_list]
    fold5_bottom = [x - np.log10(5.0) for x in ideal_list]

    fold5_topline = go.Scatter(x=ideal_x, y=fold5_top,
                            mode="lines",
                            name="5-fold",
                            line=dict(shape="linear", color="black", width=2, dash="dot"))
    fold5_bottomline = go.Scatter(x=ideal_x, y=fold5_bottom,
                            mode="lines",
                            name="5-fold",
                            showlegend =False,
                            line=dict(shape="linear", color="black", width=2, dash="dot"))

    fig = go.Figure(data=[scatter, out_scatter, trend_line, in_trend_line, ideal_line, 
                        fold2_topline, fold2_bottomline, fold5_topline, fold5_bottomline])

    axis_map = [f'10{superscript_map[0]}', f'10{superscript_map[1]}', f'10{superscript_map[2]}', f'10{superscript_map[3]}']

    # x축 제목과 폰트 설정
    fig.update_xaxes(title_text='Observed Clint', title_font=dict(size=18))

    # y축 제목과 폰트 설정
    fig.update_yaxes(title_text='Predicted Clint', title_font=dict(size=18))

    fig.update_layout(
        height=800, 
        width=900, 
        title_text="", 
        legend=dict(font=dict(size=16)),
        legend_tracegroupgap = 320,
        xaxis_range=[0.0, int(max(labels)) + 1],
        yaxis_range=[0.0, int(max(labels)) + 1],
        plot_bgcolor='white',  # Set the background color to white
        xaxis=dict(
            showgrid=True, 
            gridcolor='lightgrey', 
            tickfont=dict(size=18),
            tickvals = [0.0, 1.0, 2.0, 3.0],
            ticktext = axis_map,
            gridwidth=1, 
            griddash='dash',  # Set the x-axis grid lines to light grey dashed
            linecolor='black',  # Set x-axis line color to black
            linewidth=2,  # Set x-axis line width
        ),
        yaxis=dict(
            showgrid=True, 
            gridcolor='lightgrey', 
            gridwidth=1, 
            tickfont=dict(size=18),
            tickvals = [0.0, 1.0, 2.0, 3.0],
            ticktext = axis_map,
            griddash='dash',  # Set the y-axis grid lines to light grey dashed
            linecolor='black',  # Set y-axis line color to black
            linewidth=2,  # Set y-axis line width
        )
    )

    fig.write_image(save_path)
    
    return grad, in_grad
    


def draw_boxplot(result_path:str, save_path:str):
    ## -- predict plot save -- ##
    x_axisName = ["molecular weight", "PSA", "Number of Rotatable Bonds",
                  "Number of Hydrogen-Bond Acceptors", "Number of Hydrogen-Bond Donors", "LogP"]
    
    df_plot = pd.read_csv(result_path)
    y_labels, y_predict = df_plot['Clint'], df_plot['predict']
    fig = make_subplots(rows=3, cols=2)

    for idx, axisName in enumerate(x_axisName):
        x_labels, x_predict = df_plot[rangeLabel_col[idx]], df_plot[rangeLabel_col[idx]]
        row_numb, col_numb = (idx // 2) + 1, (idx % 2) + 1

        fig.add_trace(go.Box(x=x_labels, y=y_labels,
                            name="Label",
                            jitter=0.3,
                            pointpos=-1.8,
                            boxpoints='all', # represent all points
                            marker_color='rgb(7,40,89)',
                            line_color='rgb(7,40,89)',
                            legendgroup="1"), row=row_numb, col=col_numb)
        
        fig.add_trace(go.Scatter(x=x_predict, y=y_predict,
                                 name="Prediction",
                                 jitter=0.3,
                                 pointpos=-1.8,
                                 boxpoints='all', # represent all points
                                 marker_color='rgb(7,40,89)',
                                 line_color='rgb(7,40,89)',
                                 legendgroup="1"), row=row_numb, col=col_numb)
            
        fig.update_xaxes(title_text=x_axisName[idx], row=row_numb, col=col_numb)
        fig.update_yaxes(title_text='Intrinsic CL(Log Scale)', row=row_numb, col=col_numb)


    fig.update_layout(
        height=800, 
        width=800, 
        title_text="Compare Observed Clint to Prediction results", 
        legend_tracegroupgap = 320,
    )
    
    # fig.write_image(os.path.join("../", save_path))
    fig.write_image(save_path)