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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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from pathlib import Path

from IPython.display import display
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from rdkit import Chem
from rdkit.Chem import Draw
from sklearn.preprocessing import scale
from umap import UMAP

# General plt parameters
plt.rcParams.update({
    "axes.titlesize": 20,        # Subplot title
    "axes.labelsize": 16,        # X and Y labels
    "figure.titlesize": 24,      # Suptitle
    "xtick.labelsize": 14,       # X tick labels
    "ytick.labelsize": 14,       # Y tick labels
    "legend.fontsize": 14,       # Legend text
    "legend.title_fontsize": 14, # Legend titles
    "font.family": "Helvetica"   # Font
    })

# Define a colormap for plotting
doyle_colors = ["#CE4C6F", "#1561C2", "#188F9D","#C4ADA2","#515798", "#CB7D85", "#A9A9A9"]
colors = [doyle_colors[1],"#FFFFFFD1",doyle_colors[0]]
cont_cmap = LinearSegmentedColormap.from_list("Doyle_cont", colors)


def UMAP_suggestions(filename,
              obj_to_show = None,
              obj_bounds = None,
              objectives = None,
              display_cut_samples = True,
              display_suggestions = True,
              display_alternatives = True,
              figsize = (10,8),
              dpi = 600,
              draw_structures = True,
              show_figure = True,
              cbar_title = None,
              return_dfs = False,
              directory = "."):
    """
    Creates a UMAP for the search space, highlighting the picked samples.
    ----------
    filename : str or Path
        Path to the CSV file containing the reaction search space.
    obj_to_show : str or None
        Name of the objective that is visualized.
        If None (Default), the first listed objective is used.
    obj_bounds : tuple or list, optional
        (max, min) values to manually set the colorbar range for `obj_to_show`.
        If None, the min/max are taken from the observed evaluated samples.
    objectives : list-like, optional
        List of column names containing objective values (including "PENDING").
        If None, they are automatically inferred from columns containing
        "PENDING" strings.
    display_cut_samples : bool, default=True
        Whether cut samples (priority = -1) are shown as X markers.
        If False, they are plotted as unseen points.
    display_suggestions: bool, default=True
        Whether suggested samples (priority=1) are shown as squares.
        If Ffalse, they are plotted as unseen points.
    display_alternatives: bool, default=True
        Whether alternatively suggested samples (0<priority<1) are shown as diamonds.
        Decreasing size indicates decreasing priority.
        If False, they are plotted as unseen points.
    figsize : tuple, default=(10, 8)
        Size of the generated UMAP figure in inches.
    dpi : int, default=600
        Resolution of the output figure.
    draw_structures : bool, default=True
        Draw the structures of the evaluated samples. Requires SMILES strings as index in the CSV file.
    show_figure : bool, default=True
        Whether to display the UMAP plot.
    cbar_title : str, optional
        Custom title for the colorbar. If None, uses the objective name.
    return_dfs : bool, default=False
        If True, returns a dictionary of DataFrames for:
            - seen     (evaluated samples)
            - neutral  (unseen priority = 0)
            - cut      (unseen priority = -1)
    directory : str or Path, default="."
        Directory containing the CSV file.
    """

    df_umap, df_scope, obj_name = _UMAP_creation(filename = filename,
                             obj_to_show = obj_to_show,
                             objectives = objectives,
                             directory = directory)
    
    # add the sample status to the df ("priority")
    if "priority" in df_scope.columns.to_list():
        df_umap["priority"] = df_scope["priority"]
    else:  # assume all samples are measured if no priority column
        df_umap["priority"] = -2

    # separate the labelled and unlabelled samples
    df_seen = df_umap.loc[df_umap[obj_name] != "PENDING"].copy()
    df_seen[obj_name] = df_seen[obj_name].astype(float)
    df_unseen = df_umap.loc[df_umap[obj_name] == "PENDING"]

    df_neutral = df_unseen[df_unseen["priority"] == 0].copy()
    df_cut = df_unseen[df_unseen["priority"] == -1].copy()
    df_sugg = df_unseen[df_unseen["priority"] == 1].copy()  # suggested samples
    mask = (df_unseen["priority"] > 0) & (df_unseen["priority"] < 1)
    df_alt = df_unseen[mask].copy()  # alternative suggestions

    # draw the structures if requested
    if draw_structures:
        # Extract the suggested molecules and draw them.
        scope_substrates  = df_seen.index.to_list()
        scope_substrates = [str(entry.encode().decode('unicode_escape')) for entry in scope_substrates]
        scope_labels = df_seen[obj_name].to_list()

        print("Structures of the scope substrates:")
        try:
            mol_list = [Chem.MolFromSmiles(smiles) for smiles in scope_substrates]
            # Draw the aligned molecules
            depiction = Draw.MolsToGridImage(
                mol_list,
                molsPerRow=6,
                subImgSize=(200, 200),
                legends=[str(result_val) for result_val in scope_labels]
                )
            display(depiction)
        except:
            print(f"Could not draw the molecules in {scope_substrates}.")
            print(f"Please label your molecules with SMILES strings to draw the results of the scope.")
            print("Alternatively, set draw_structures = False in the function call to avoid structure drawing.")

    # print the UMAP if requested
    if show_figure:
        print("UMAP projection of the reaction space:")
        if display_cut_samples:
            print("(Evaluated samples are shown as colored circles. Pruned samples are marked with X.)")
        else:
            print("(Evaluated samples are shown as colored circles.)")
        if display_suggestions and display_alternatives:
            print("(Suggested samples are marked with squares and alternative suggestions with diamonds.)")
        elif display_suggestions and not display_alternatives:
            print("(Suggested samples are marked with squares.)")
        elif not display_suggestions and display_alternatives:
            print("(Alternative suggestions are marked with diamonds.)")
        plt.figure(figsize=figsize, dpi = dpi, constrained_layout = True)

        colormap = cont_cmap
        if obj_bounds is None:
            vmin = df_seen[obj_name].min()
            vmax = df_seen[obj_name].max()
        else:
            vmin = obj_bounds[1]
            vmax = obj_bounds[0]
        norm = plt.Normalize(vmin,vmax)

        # plot the neutral points
        plt.scatter(
            df_neutral["UMAP1"], df_neutral["UMAP2"], s=40, 
            linewidth=0.3, edgecolor="k", color=doyle_colors[6],
            marker="o", alpha = 0.8, label = "neutral", zorder=1)

        # check if cut samples should be highlighted
        if display_cut_samples:
            # plot the cut samples
            plt.scatter(
                df_cut["UMAP1"], df_cut["UMAP2"], s=100, edgecolor="k", marker = "X",
                color=doyle_colors[4], alpha=0.6, label = "cut", linewidth = 0.3, zorder=2)
        else:
            # plot the cut samples the same way as the neutral samples
            plt.scatter(
                df_cut["UMAP1"], df_cut["UMAP2"], s=40, edgecolor="k", marker = "o",
                color=doyle_colors[6], alpha=0.8, linewidth = 0.3, zorder=2)
            
        # check if suggested samples should be highlighted
        if display_suggestions:
            # plot the suggested samples
            plt.scatter(
                df_sugg["UMAP1"], df_sugg["UMAP2"], s=100, edgecolor="k", marker = "s",
                color=doyle_colors[2], alpha=0.8, label = "suggested", linewidth = 1.2, zorder=4)
        else:
            # plot the sugggested samples the same way as the neutral samples
            plt.scatter(
                df_sugg["UMAP1"], df_sugg["UMAP2"], s=40, edgecolor="k", marker = "o",
                color=doyle_colors[6], alpha=0.8, linewidth = 0.3, zorder=1)
            
        # check if alternative sugegstions should be highlighted
        if display_alternatives:
            # plot the alternative samples
            plt.scatter(
                df_alt["UMAP1"], df_alt["UMAP2"], s=100*df_alt["priority"], edgecolor="k", marker = "D",
                color=doyle_colors[2], alpha=0.8, label = "alt. suggestion", linewidth = 1.2, zorder=3)
        else:
            # plot the alt. sugg. samples the same way as the neutral samples
            plt.scatter(
                df_alt["UMAP1"], df_alt["UMAP2"], s=40, edgecolor="k", marker = "o",
                color=doyle_colors[6], alpha=0.8, linewidth = 0.3, zorder=1)
        
        # plot the selected samples
        scatter_numeric = plt.scatter(df_seen["UMAP1"], df_seen["UMAP2"], c=df_seen[obj_name],
                                        cmap=cont_cmap, norm=norm,s=250, alpha=1, edgecolor='k', 
                                        label = "measured", linewidth=2, zorder = 5)

        cbar = plt.colorbar(scatter_numeric)
        if cbar_title is None:
            cbar_label = obj_name.capitalize()
            cbar.set_label(cbar_label)
        else:
            cbar.set_label(cbar_title)

        plt.legend()
        plt.xlabel("UMAP1")
        plt.ylabel("UMAP2")
        plt.show()

    if return_dfs:
        return {"seen": df_seen, 
                "suggested": df_sugg,
                "alternatives": df_alt,
                "neutral": df_neutral, 
                "cut": df_cut}
    

def UMAP_predictions(filename,
                      df_pred,
                      obj_to_show = None,
                      obj_bounds = None,
                      objectives = None,
                      pred_for_cut = False,
                      figsize = (10,8),
                      dpi = 600,
                      cbar_title = None,
                      directory = "."):
    """
    Creates a UMAP for the search space, highlighting the picked samples.
    ----------
    filename : str or Path
        Path to the CSV file containing the reaction search space.
    df_pred: pd.DataFrame
        dataframe with the predictions from either 
        ScopeBO.expected_improvement() or ScopeBO.predict_performance()
    obj_to_show : str or None
        Name of the objective that is visualized.
        If None (Default), the first listed objective is used.
    obj_bounds : tuple or list, optional
        (max, min) values to manually set the colorbar range for `obj_to_show`.
        If None, the min/max are taken from the observed evaluated samples.
    objectives : list-like, optional
        List of column names containing objective values (including "PENDING").
        If None, they are automatically inferred from columns containing
        "PENDING" strings.
    pred_for_cut: Boolean
        Default = False
        Show predictions also for cut samples if True
    figsize : tuple, default=(10, 8)
        Size of the generated UMAP figure in inches.
    dpi : int, default=600
        Resolution of the output figure.
    cbar_title : str, optional
        Custom title for the colorbar. If None, uses the objective name.
    directory : str or Path, default="."
        Directory containing the CSV file.
    """

    # generate a 2D and also read in the scope information
    df_umap, df_scope, obj_name = _UMAP_creation(filename = filename,
                             obj_to_show = obj_to_show,
                             objectives = objectives,
                             directory = directory)

    # separate the observed, cut, and predicted samples
    df_seen = df_umap.loc[df_umap.index.isin(df_scope[df_scope[obj_name] != "PENDING"].index.to_list())].copy()
    df_seen[obj_name] = df_seen[obj_name].astype(float)
    prio_val = -1  # priority value corresponding to cut values
    if pred_for_cut:
        # unrealistic priority value in case predictions for cut samples are to be shown
        # will result in empty df for cut samples
        prio_val = 42  
    df_cut = df_umap[df_umap.index.isin(df_scope[df_scope["priority"] == prio_val].index.to_list())].copy()
    df_unseen = df_umap.loc[(~df_umap.index.isin(df_seen.index.to_list())) &
                            (~df_umap.index.isin(df_cut.index.to_list()))].copy()

    # determine the source of the predictions (predict_performance() or expected_improvement())
    # (using characteristic naming of the columns)
    pred_type = None
    if f"{obj_name}_pred" in df_pred.columns.values:
        pred_type = "mlr"  # from predict_performance()
    elif f"Prediction_{obj_name}" in df_pred.columns.values:
        pred_type = "ei"  # from expected_improvement()
    # assign the predicted values
    for idx in df_unseen.index:
        if pred_type == "mlr":
            df_unseen.loc[idx,obj_name] = df_pred.loc[idx,f"{obj_name}_pred"]
        elif pred_type == "ei":
            df_unseen.loc[idx,obj_name] = df_pred.loc[idx,f"Prediction_{obj_name}"]

    # plot the figure
    plt.figure(figsize=figsize, dpi = dpi, constrained_layout = True)

    colormap = cont_cmap
    if obj_bounds is None:
        vmin = df_seen[obj_name].min()
        vmax = df_seen[obj_name].max()
    else:
        vmin = obj_bounds[1]
        vmax = obj_bounds[0]
    norm = plt.Normalize(vmin,vmax)

    # set the size for predicted values based on their standard deviation if available
    # smaller points have a higher standad deviation (i. e. more uncertain predictions)
    size_vals = [40] * len(df_unseen)
    if pred_type == "ei":
        size_min, size_max = 50, 200
        std_vals = [df_pred.loc[idx,f"Std. dev. of pred._{obj_name}"] for idx in df_unseen.index]
        std_min = np.min(std_vals)
        std_max = np.max(std_vals)
        if std_max - std_min == 0:
            # no standard deviation range, set a default size
            size_vals = [(size_min+size_max)/2] * len(df_unseen)
        else:
            size_vals = [size_min + (std_max - std_val) * (size_max - size_min) / (std_max - std_min)
                    for std_val in std_vals]
    
    if not pred_for_cut:
        # plot the cut points
        plt.scatter(df_cut["UMAP1"], df_cut["UMAP2"], s=100, edgecolor="k", marker = "X",
                    color=doyle_colors[4], alpha=0.6, linewidth = 0.3, zorder=0, label = "cut")

    # plot the predicted points
    plt.scatter(df_unseen["UMAP1"], df_unseen["UMAP2"], c=df_unseen[obj_name],
                cmap=cont_cmap, norm=norm, s= size_vals, alpha=1, edgecolor='k',
                linewidth=1, marker = "o", label = "predicted",zorder = 1)
        
    # plot the observed samples
    scatter_numeric = plt.scatter(df_seen["UMAP1"], df_seen["UMAP2"], c=df_seen[obj_name],
                                    cmap=cont_cmap, norm=norm,s=250, alpha=1, edgecolor='k', 
                                    linewidth=2, label = "measured", marker = "s", zorder = 2)

    cbar = plt.colorbar(scatter_numeric)
    if cbar_title is None:
        cbar_label = obj_name.capitalize()
        cbar.set_label(cbar_label)
    else:
        cbar.set_label(cbar_title)

    plt.legend()
    plt.xlabel("UMAP1")
    plt.ylabel("UMAP2")
    plt.show()
    

def _UMAP_creation(filename,
              obj_to_show = None,
              objectives = None,
              directory = "."):
    
    """
    Creates a 2D UMAP for plotting.
    Returns a df with the compound names as index and the coordinates ("UMAP1", "UMAP2") as columns.
    Returns the df of the input file ("filename") after sorting the index.
    Returns the name of the objective that will be plotted.
    
    See function UMAP_view() for input variable information (above).
    """
    
    # Set directory.
    wdir = Path(directory)

    # read the results file
    df_scope = pd.read_csv(wdir / filename, index_col=0, header=0)
    df_scope.sort_index(inplace=True)
    if "priority" in df_scope.columns.to_list():
        df_scope["priority"] = df_scope["priority"].astype(float)

    # identify the objectives (containing PENDING entries) if none are given
    if objectives is None:
        objectives = df_scope.columns[df_scope.eq("PENDING").any()].to_list()
        if (objectives == []) and (obj_to_show is not None):
            print("hello")
            objectives = [obj_to_show]

    # show the first objective in the UMAP if none has been specified in the function input
    if obj_to_show is None:
        obj_to_show = objectives[0]
    
    # scale the featurization data
    df_scaled = df_scope.copy(deep=True)
    if "priority" in df_scope.columns.to_list():
        df_scaled.drop(columns=objectives + ["priority"], inplace=True)
    else:
        df_scaled.drop(columns=objectives, inplace=True)
    df_scaled = pd.DataFrame(scale(df_scaled),
                             df_scaled.index,
                             df_scaled.columns)

    # create a UMAP
    fit = UMAP(n_neighbors=40, 
               min_dist=0.7,
               n_components=2,
               metric="euclidean",
               random_state=42)
    df_umap = pd.DataFrame(fit.fit_transform(df_scaled), 
                           index = df_scaled.index,
                           columns = ["UMAP1","UMAP2"])
    df_umap.index = df_umap.index.astype(str)

    # add the result labels
    df_umap[obj_to_show] = df_scope[obj_to_show]

    return df_umap, df_scope, obj_to_show