Skip to content

Visualization

Classes

NumpyEncoder

Bases: JSONEncoder

Used to serialize numpy arrays before a .json export.

Functions
default(obj)

Handle numpy array serialization.

Parameters:

Name Type Description Default
obj

object to serialize

required

Returns:

Type Description

serializable representation of the object

Source code in opentak/visualization.py
67
68
69
70
71
72
73
74
75
def default(self, obj):
    """Handle numpy array serialization.

    :param obj: object to serialize
    :return: serializable representation of the object
    """
    if isinstance(obj, np.ndarray):
        return obj.tolist()
    return json.JSONEncoder.default(self, obj)

ExportTAK(sorted_array, dico_label_color, dict_label_id) dataclass

Class for exporting the TAK.

Initialize the ExportTAK object.

Parameters:

Name Type Description Default
sorted_array

sorted array of patient sequences

required
dico_label_color

dictionary mapping labels to colors

required
dict_label_id

dictionary mapping treatments to IDs

required
Source code in opentak/visualization.py
86
87
88
89
90
91
92
93
94
95
def __init__(self, sorted_array, dico_label_color, dict_label_id):
    """Initialize the ExportTAK object.

    :param sorted_array: sorted array of patient sequences
    :param dico_label_color: dictionary mapping labels to colors
    :param dict_label_id: dictionary mapping treatments to IDs
    """
    self.sorted_base = sorted_array
    self.dict_colors = dico_label_color
    self.dict_id_labels = dict_label_id
Functions

TakVisualizer(tak, dico_evt_for_legend=None)

Creation of the TakVisualizer object.

Parameters:

Name Type Description Default
tak Tak

Tak object with fitted clustering results

required
dico_evt_for_legend dict[str, str] | None

the mapping for legend names of events

None
Source code in opentak/visualization.py
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
def __init__(
    self, tak: Tak, dico_evt_for_legend: dict[str, str] | None = None
) -> None:
    """Creation of the TakVisualizer object.

    :param tak: Tak object with fitted clustering results
    :param dico_evt_for_legend: the mapping for legend names of events
    """
    # Input
    self.tak = tak

    # Used to reset the visualization if needed
    self.memory_tak: list[npt.NDArray] | None = None
    self.memory_clusters: list[list[int]] | None = None

    # Params
    # Colors
    self.default_colors = dict(DEFAULT_EVENTS_COLORS)

    self.dico_label_color: dict[str, str] = {}
    self._create_dict_label_color()

    if dico_evt_for_legend is None:
        self.dico_evt_for_legend: dict[str, str] = {}
    else:
        self.dico_evt_for_legend = dico_evt_for_legend

    # Output of the visualization
    self.array_tak_viz = None
    self.axes: dict = {}
    self.sampled_patients = np.array([])

    self.dico_id_evt: dict[int, str] = {
        value: key for key, value in self.tak.dict_label_id.items()
    }

    self.set_values: set[str | int] = set()
    self.nb_patients: int | None = None
    self.base_date: datetime | None = None
    self.fig_x_axis: dict[str, Any] | None = None

    # Duration
    self.clusters_names: list[str] | None = None
    self.list_len_clusters_ordered: list[int] | None = None

    # Additional attributes set later
    self.coef_width: float = 1.0
    self.coef_length: float = 1.0
    self.current_processed_patients: npt.NDArray = np.array([])
    self.fig: go.Figure | None = None
Functions
update_colors(dict_new_colors=None, **kwargs)

Update the color dictionary.

Allows the user to set colors for specific labels.

Parameters:

Name Type Description Default
dict_new_colors dict[str, str] | None

Dict 'label'->'color'

None
kwargs Any

You can specify new colors here, for example by using .update_colors(drug_A=(24,90,0))

{}
Source code in opentak/visualization.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
def update_colors(
    self, dict_new_colors: dict[str, str] | None = None, **kwargs: Any
) -> None:
    """Update the color dictionary.

    Allows the user to set colors for specific labels.

    :param dict_new_colors: Dict 'label'->'color'
    :param kwargs: You can specify new colors here, for example by using
    .update_colors(drug_A=(24,90,0))
    """
    if dict_new_colors is not None:
        self.dico_label_color.update(dict_new_colors)

    self.dico_label_color.update(kwargs)
split(list_ids)

Split the TAK array and only keep list_ids IDs.

Parameters:

Name Type Description Default
list_ids list[int]

IDs to keep in the TAK

required
Source code in opentak/visualization.py
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
def split(self, list_ids: list[int]) -> None:
    """Split the TAK array and only keep list_ids IDs.

    :param list_ids: IDs to keep in the TAK
    """
    if self.memory_tak is not None:
        raise ValueError(
            "The TAK has already been split. Please reset with the reset_split() method and try again."
        )

    complete_id_list = np.concatenate(self.tak.list_ids_clusters).ravel()

    if not set(list_ids).issubset(set(complete_id_list)):
        raise ValueError(
            "The parameter 'list_id' does not match with the IDs in the TAK object"
        )

    # Save the current TAK output that will be modified
    self.memory_tak = deepcopy(self.tak.sorted_array)
    # Restrict TAK array to patients in the sub-cohort
    self.tak.sorted_array = [
        self.tak.sorted_array[cluster][
            pd.Series(self.tak.list_ids_clusters[cluster]).isin(list_ids).to_numpy()
        ]
        for cluster in range(len(self.tak.sorted_array))
    ]

    self.memory_clusters = deepcopy(self.tak.list_ids_clusters)

    self.tak.list_ids_clusters = [
        [id_patient for id_patient in original_cluster if id_patient in list_ids]
        for original_cluster in self.tak.list_ids_clusters
    ]
reset_split()

Reset the TAK after a split so it contains the whole initial population.

Source code in opentak/visualization.py
290
291
292
293
294
295
296
297
298
def reset_split(self) -> None:
    """Reset the TAK after a split so it contains the whole initial population."""
    if self.memory_tak is not None:
        self.tak.sorted_array = self.memory_tak
        self.memory_tak = None

    if self.memory_clusters is not None:
        self.tak.list_ids_clusters = self.memory_clusters
        self.memory_clusters = None
process_visualization(num_cluster=None, soften_angles=0, unblurred_events=None, base_date=None, process_medoids=False, **kwargs)

Transform the image into an array of sequence (with a daily timeline) of similar patients.

Parameters:

Name Type Description Default
num_cluster int | None

cluster to process

None
soften_angles

size of the blur filter

0
unblurred_events Sequence[str] | None

events to not consider for the blur

None
base_date datetime | None

date to be filled in to set the first date of the x-axis

None
process_medoids bool

whether to use medoids instead of patients Keyword Arguments: - agg_patients : aggregation method ('max', 'median' or 'mode') - sampling_size : Nb pixels de la dimension maximale - min_samples : Borne min de pixels de la dimension minimale - x_range - y_range - image_length - image_width If no date is entered, the x-axis remains in number of years.

False
Source code in opentak/visualization.py
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
def process_visualization(
    self,
    num_cluster: int | None = None,
    soften_angles=0,
    unblurred_events: Sequence[str] | None = None,
    base_date: datetime | None = None,
    process_medoids: bool = False,
    **kwargs,
):
    """Transform the image into an array of sequence (with a daily timeline) of similar patients.

    :param num_cluster: cluster to process
    :param soften_angles: size of the blur filter
    :param unblurred_events: events to not consider for the blur
    :param base_date: date to be filled in to set the first date of the x-axis
    :param process_medoids: whether to use medoids instead of patients
    Keyword Arguments:
        - `agg_patients` : aggregation method ('max', 'median' or 'mode')
        - `sampling_size` : Nb pixels de la dimension maximale
        - `min_samples` : Borne min de pixels de la dimension minimale
        - `x_range`
        - `y_range`
        - `image_length`
        - `image_width`

    If no date is entered, the x-axis remains in number of years.

    """
    if not self.tak.is_fitted:
        raise ValueError(
            "TAK is not fitted, the .fit() method has to be used in order to sort the patients."
        )
    if process_medoids:
        if not hasattr(self.tak, "sorted_array_medoides"):
            raise TypeError("TAK should be a MetaTak if process_medoids is True")
        patients = (
            self.tak.sorted_array_medoides[num_cluster]
            if num_cluster is not None
            else np.concatenate(self.tak.sorted_array_medoides)
        )
    else:
        patients = (
            self.tak.sorted_array[num_cluster]
            if num_cluster is not None
            else np.concatenate(self.tak.sorted_array)
        )

    self._run_process_visualisation(
        patients, soften_angles, unblurred_events, base_date, **kwargs
    )
get_plot(plot_title='Analysis of treatment lines', unit_as_years=False, unit_as_months=False, nb_months=None, offset=0, add_sep=False, dendrogram=False, **kwargs)

Allow the display of TAK with Plotly.

Note

if unit_as_years=False and unit_as_month=False, units are in TIMESTAMP

if self.base_date = True, xaxes will be in years

Kwargs

  • list_len_clusters_ordered: (list) number of patients in each cluster, starting from the bottom of the TAK
  • write_annotation: (bool) whether or not to write the annotation at the right of the TAK. Default: True
  • clusters_names: (list) liste des noms des clusters à associer à list_len_clusters_ordered. Default: ["A", "B", etc...]
  • color_annotation: (str) color of the lines and of the text at the left of the graph. Default: "#4CA094" (green-blue)
  • width_line: (int) width of the lines. Default: 3
  • size_annotation: (int) size of the font for annotation at the right of the TAK. Default: 10

Parameters:

Name Type Description Default
plot_title str

Title of the plot (str)

'Analysis of treatment lines'
unit_as_years bool

specify whether the units are years (True) or other (False)

False
unit_as_months bool

specify whether the units are months (True) or other (False)

False
nb_months int | None

specify every how many months do we add a xtick (ignored if unit_as_month=False)

None
offset int

specify if an offset (in TIMESTAMP) should be apply to xaxes

0
add_sep bool

whether to add horizontal line on the tak or not

False
dendrogram bool

whether to add a dendrogram on the side or not

False
kwargs

options to deal with cluster sizes, names, colors and width of the lines and size of the annotations

{}

Returns:

Type Description
Figure

Plotly figure

Source code in opentak/visualization.py
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
def get_plot(
    self,
    plot_title: str = "Analysis of treatment lines",
    unit_as_years: bool = False,
    unit_as_months: bool = False,
    nb_months: int | None = None,
    offset: int = 0,
    add_sep: bool = False,
    dendrogram: bool = False,
    **kwargs,
) -> go.Figure:
    # ruff: noqa: D301
    """Allow the display of TAK with Plotly.

    !!! note
        if unit_as_years=False and unit_as_month=False, units are in TIMESTAMP \n
        if self.base_date = True, xaxes will be in years

    !!! question "Kwargs"
        - `list_len_clusters_ordered`: (list) number of patients in each cluster,
        starting from the bottom of the TAK
        - `write_annotation`: (bool) whether or not to write the annotation at the right
        of the TAK.
        Default: True
        - `clusters_names`: (list) liste des noms des clusters à associer
        à list_len_clusters_ordered.
        Default: ["A", "B", etc...]
        - `color_annotation`: (str) color of the lines and of the text at the left of
        the graph.
        Default: "#4CA094" (green-blue)
        - `width_line`: (int) width of the lines.
        Default: 3
        - `size_annotation`: (int) size of the font for annotation at the right of the
        TAK.
        Default: 10

    :param plot_title: Title of the plot (str)
    :param unit_as_years: specify whether the units are years (True) or other (False)
    :param unit_as_months: specify whether the units are months (True) or other (False)
    :param nb_months: specify every how many months do we add a xtick (ignored if unit_as_month=False)
    :param offset: specify if an offset (in TIMESTAMP) should be apply to xaxes
    :param add_sep: whether to add horizontal line on the tak or not
    :param dendrogram: whether to add a dendrogram on the side or not
    :param kwargs: options to deal with cluster sizes, names, colors
    and width of the lines and size of the annotations

    :return: Plotly figure
    """
    dico_id_color = self._generate_plotly_color_dictionary(self.dico_id_evt)
    traces = [
        go.Heatmap(
            x=self.axes["x"],
            y=self.axes["y"],
            z=np.where(self.sampled_patients == value, value, np.nan),
            hoverinfo="none",
            colorscale=[(0.0, dico_id_color[value]), (1.0, dico_id_color[value])],
            name=f"{self.dico_evt_for_legend.get(self.dico_id_evt[int(value)], self.dico_id_evt[int(value)])}",
            showscale=False,
            showlegend=True,
            autocolorscale=False,
        )
        for i, value in enumerate(self.set_values)
    ]

    xaxis = self._get_x_axis(unit_as_years, unit_as_months, nb_months, offset)

    optimal_y_ticks = NiceScale(0, self.nb_patients)

    ticktext = [
        optimal_y_ticks.nice_min + i * optimal_y_ticks.tick_spacing
        for i in range(optimal_y_ticks.max_ticks)
    ]
    tickvals = [tick // self.coef_length for tick in ticktext]

    yaxis = {
        "title": "Patients",
        "tickmode": "array",
        "tickvals": tickvals,
        "ticktext": ticktext,
        "ticklen": 5,
        "ticks": "outside",
    }

    self.fig_x_axis = xaxis

    if dendrogram:
        fig = make_subplots(rows=1, cols=2, column_widths=[0.2, 0.8])
        fig.add_traces(traces, rows=1, cols=2)
    else:
        fig = go.Figure()
        fig.add_traces(traces)

    fig.update_layout(
        title_text=f"{plot_title} ({self.nb_patients} patients)",
        template=base_template,
    )
    fig["layout"]["title"].update(x=0.5)

    # If a dendrogram is displayed, 2 subplots are displayed
    if dendrogram:
        fig.update_layout(xaxis2=xaxis, yaxis2=yaxis)

        dendro_fig, yaxis_range, threshold_vertical = self.get_dendro(
            add_sep, **kwargs
        )

        dendro_yaxis_layout = copy.deepcopy(DENDRO_AXIS_LAYOUT)
        dendro_yaxis_layout["range"] = yaxis_range

        fig.update_layout(yaxis=dendro_yaxis_layout, xaxis=DENDRO_AXIS_LAYOUT)

        for trace in dendro_fig["data"]:
            trace["showlegend"] = False
            fig.add_trace(trace, row=1, col=1)

        fig.add_vline(
            x=-threshold_vertical,
            line_width=1,
            line_dash="dot",
            line_color="rgb(0,0,255)",
            row=1,
            col=1,
        )

    # Otherwise we only display one plot
    else:
        fig.update_layout(xaxis=xaxis, yaxis=yaxis)

    self.fig = (
        self._add_sep_on_tak_fig(fig, dendrogram=dendrogram, **kwargs)
        if add_sep
        else fig
    )

    return self.fig
imshow()

Allow the display of TAK with Plotly using imshow.

TO DO : Very basic function -> needs better implantation

Returns:

Type Description

Plotly figure

Source code in opentak/visualization.py
718
719
720
721
722
723
724
725
726
727
def imshow(self):
    """Allow the display of TAK with Plotly using imshow.

    TO DO : Very basic function -> needs better implantation

    :return: Plotly figure
    """
    patients = self.array_tak_viz

    return px.imshow(patients)
graph_events_rep_on_tak(events_not_shown=None, events_not_in_percent=None, threshold_percent=2, represented_patients_name='patients involved')

Generate the graph of the distribution of each event along the tak_viz, based on the values of the TAK array.

Parameters:

Name Type Description Default
events_not_shown list | None

The list of events not to show on the graph.

None
events_not_in_percent list | None

List of events to ignore in the %age of curves. It changes the denominator of other curves.

None
threshold_percent float

% minimum of patients represented on the first graph

2
represented_patients_name str

Name to give to patients, default value patients involved

'patients involved'

Returns:

Type Description
Figure

Plotly figure

Source code in opentak/visualization.py
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
def graph_events_rep_on_tak(
    self,
    events_not_shown: list | None = None,
    events_not_in_percent: list | None = None,
    threshold_percent: float = 2,
    represented_patients_name: str = "patients involved",
) -> go.Figure:
    # ruff: noqa: D205
    """Generate the graph of the distribution of each event along the tak_viz,
    based on the values of the TAK array.

    :param events_not_shown: The list of events not to show on the graph.
    :param events_not_in_percent: List of events to ignore in the %age of curves. It changes the denominator
    of other curves.
    :param threshold_percent: % minimum of patients represented on the first graph
    :param represented_patients_name: Name to give to patients, default value `patients involved`
    :return: Plotly figure
    """
    if self.nb_patients is None:
        raise ValueError(
            "TAK visualization is not processed, the .process_visualization() "
            "method has to be used in order to call this function."
        )
    events_not_shown = [] if events_not_shown is None else events_not_shown
    events_not_shown = list(set(events_not_shown))
    events_not_in_percent = (
        [] if events_not_in_percent is None else events_not_in_percent
    )
    events_not_in_percent = list(set(events_not_in_percent))

    base_after_tak = self.current_processed_patients.copy()
    x = np.arange(0, len(self.tak.sorted_array[0][0]))

    y_nb_meds = [
        self._count_evt_at_day_d(base_after_tak, day_d)
        for day_d in range(base_after_tak.shape[1])
    ]

    df_base_tak = pd.DataFrame(self.tak.base)

    evt_of_interest = [
        evt for evt in df_base_tak["EVT"].unique() if evt not in events_not_shown
    ]

    # Axe des y - valeurs des courbes
    # Dico des series temporelles, avec valeur par défaut au cas où aucun patient n'a l'un des evt à ignorer
    data_lines: MutableMapping[str, list[int]] = defaultdict(
        lambda: [0] * len(y_nb_meds)
    )
    for evt in df_base_tak["EVT"].unique():
        data_lines[evt] = [count_med[evt] for count_med in y_nb_meds]

    # Calcul du nouveau dénominateur (nombre de patients traités à l'instant t) : patient à exclure à chaque
    # timestamp
    if events_not_in_percent:
        percent_patients = [
            (1 - sum(nb_pat_remove))
            for nb_pat_remove in zip(
                *[data_lines[x] for x in events_not_in_percent], strict=True
            )
        ]
        fig = make_subplots(rows=2, cols=1, row_heights=[0.8, 0.2])
        prefix_top_graph = (
            '<b><span style="text-decoration: underline;">Top graph :</span><br>'
        )
    else:
        percent_patients = [1] * len(y_nb_meds)
        fig = make_subplots(rows=1, cols=1)
        prefix_top_graph = ""
    threshold_cent = threshold_percent / 100

    mode = "lines"
    fig.add_trace(
        go.Scatter(
            x=[len(y_nb_meds) / 2],
            y=[1],
            mode="markers",
            line={"color": "rgba(0,0,0,0)"},
            name=f"{prefix_top_graph}Events of the TAK</b><br>",
        ),
        row=1,
        col=1,
    )

    for evt in evt_of_interest:
        # Division pour chaque timestamp, par le nombre de patients en cours de suivi
        if evt not in events_not_in_percent:
            y_evt = [
                np.nan if percent_pt < threshold_cent else percent_evt / percent_pt
                for percent_evt, percent_pt in zip(
                    data_lines[evt], percent_patients, strict=True
                )
            ]

            line = {"color": f"{self.dico_label_color[evt]}"}

            fig.add_trace(
                go.Scatter(
                    x=x,
                    y=y_evt,
                    line=line,
                    mode=mode,
                    name=self.dico_evt_for_legend.get(evt, evt),
                ),
                row=1,
                col=1,
            )

    if events_not_in_percent:
        fig.add_trace(
            go.Scatter(
                x=[len(y_nb_meds) / 2],
                y=[1],
                mode="markers",
                line={"color": "rgba(0,0,0,0)"},
                name='<br><br><b><span style="text-decoration: underline;">Bottom graph :</span><br>% of patients '
                "involved <br>on top graph</b><br>",
            ),
            row=1,
            col=1,
        )
        fig.add_trace(
            go.Scatter(
                x=x,
                y=percent_patients,
                line={"color": "#9491AD", "dash": "dash"},
                name=f"% {represented_patients_name}",
                yaxis="y2",
                mode=mode,
            ),
            row=2,
            col=1,
        )

    fig.update_layout(
        title=f"Events distribution according to the TAK ({self.nb_patients} patients)",
        title_x=0.5,
        yaxis={
            "dtick": 0.1,
            "title": f"Percentage of patients <br> <i>(among {represented_patients_name})</i>",
        },
        yaxis2={
            "dtick": 0.25,
            "title": f"Percentage of <br> {represented_patients_name} <br> <i>(over the whole cohort)</i>",
        },
    )
    if self.fig_x_axis is not None:
        fig.update_yaxes(range=[0, 1], tickformat="0%").update_xaxes(
            self.fig_x_axis, title_standoff=0
        )
    else:
        fig.update_yaxes(range=[0, 1], tickformat="0%")
    return fig
export_tak(path_folder, file_name='tak_result.json')

Export the TAK for datashader visualization.

Parameters:

Name Type Description Default
path_folder str | Path

Folder name to store results

required
file_name str

Result file name

'tak_result.json'
Source code in opentak/visualization.py
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
def export_tak(self, path_folder: str | Path, file_name: str = "tak_result.json"):
    """Export the TAK for datashader visualization.

    :param path_folder: Folder name to store results
    :param file_name: Result file name
    """
    export_data = ExportTAK(
        self.tak.sorted_array, self.dico_label_color, self.tak.dict_label_id
    )

    path_folder = Path(path_folder)

    # Create directory if it doesn't exist
    path_folder.mkdir(parents=True, exist_ok=True)

    with (path_folder / file_name).open("w", encoding="utf-8") as outfile:
        json.dump(asdict(export_data), outfile, cls=NumpyEncoder)

NiceScale(minv, maxv)

Compute optimal parameters to set the scale.

Parameters:

Name Type Description Default
minv

Min value of the range

required
maxv

Max value of the range

required
Source code in opentak/visualization.py
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
def __init__(self, minv, maxv):
    """Compute optimal parameters to set the scale.

    :param minv: Min value of the range
    :param maxv: Max value of the range
    """
    self.max_ticks = 10
    self.tick_spacing = 0
    self.lst = 10
    self.nice_min = 0
    self.nice_max = 0
    self.min_point = minv
    self.max_point = maxv

    self.calculate()
Functions
calculate()

Calculate and updates values for tick spacing and nice minimum and maximum data points on the axis.

Source code in opentak/visualization.py
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
def calculate(self):
    """Calculate and updates values for tick spacing
    and nice minimum and maximum data points on the axis.
    """
    self.lst = self.nice_num(self.max_point - self.min_point, False)
    self.tick_spacing = self.nice_num(self.lst / (self.max_ticks - 1), True)
    self.nice_min = (
        math.floor(self.min_point / self.tick_spacing) * self.tick_spacing
    )
    self.nice_max = (
        math.ceil(self.max_point / self.tick_spacing) * self.tick_spacing
    )
nice_num(lst, rround)

Return a "nice" number approximately equal to range.

Parameters:

Name Type Description Default
rround

Rounds the number if rround = true Takes the ceiling if rround = false.

required
lst

Local range

required
Source code in opentak/visualization.py
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
def nice_num(self, lst, rround):
    """Return a "nice" number approximately equal to range.

    :param rround: Rounds the number if rround = true
    Takes the ceiling if rround = false.
    :param lst: Local range
    """
    self.lst = lst

    exponent = math.floor(math.log10(self.lst))
    fraction = self.lst / math.pow(10, exponent)

    if rround:
        if fraction < 1.5:
            nice_fraction = 1
        elif fraction < 3:
            nice_fraction = 2
        elif fraction < 7:
            nice_fraction = 5
        else:
            nice_fraction = 10
    # ruff: noqa: PLR5501
    else:
        if fraction <= 1:
            nice_fraction = 1
        elif fraction <= 2:
            nice_fraction = 2
        elif fraction <= 5:
            nice_fraction = 5
        else:
            nice_fraction = 10

    return nice_fraction * math.pow(10, exponent)

Functions

soften_angles_sample(base_image, soften_angles, image_length, image_width)

Apply blur on the picture.

Parameters:

Name Type Description Default
base_image NDArray

incoming image

required
soften_angles int

size of the blur filter

required
image_length int | None

length of the image before the filter is applied

required
image_width int | None

width of the image before the filter is applied

required

Returns:

Type Description
tuple[Image, Image]

image_bigsize: image with size (image_width, image_length) and image_outlines: same size and blurred

Source code in opentak/visualization.py
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
def soften_angles_sample(
    base_image: npt.NDArray,
    soften_angles: int,
    image_length: int | None,
    image_width: int | None,
) -> tuple[Image, Image]:
    """Apply blur on the picture.

    :param base_image: incoming image
    :param soften_angles: size of the blur filter
    :param image_length: length of the image before the filter is applied
    :param image_width: width of the image before the filter is applied
    :return: image_bigsize: image with size (image_width, image_length)
    and image_outlines: same size and blurred
    """
    # Convert into image and resize
    image = PIL.Image.fromarray(base_image.astype(np.uint8))
    if image_width is None or image_length is None:
        raise ValueError("image_width and image_length must be defined")
    image_bigsize = image.resize(
        (image_width, int(image_length)), resample=PIL.Image.Resampling.NEAREST
    )

    # Apply blur
    image_outlines = image_bigsize.filter(ImageFilter.ModeFilter(soften_angles))

    # Returns enlarged and blurred image
    return image_bigsize, image_outlines

get_clusters_names_default(list_len_clusters_ordered, max_nb_clusters=26)

Return the list of default clusters names (reversed alphabetic order, ending by A).

Parameters:

Name Type Description Default
list_len_clusters_ordered list

list of the number of patients in each cluster, order starting at the botton of the TAK

required
max_nb_clusters int

max number of clusters before raising an error

26

Returns:

Type Description
list

list of default clusters names (["F", "E", "D", ...])

Source code in opentak/visualization.py
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
def get_clusters_names_default(
    list_len_clusters_ordered: list, max_nb_clusters: int = 26
) -> list:
    """Return the list of default clusters names (reversed alphabetic order, ending by A).

    :param list_len_clusters_ordered: list of the number of patients in each
    cluster, order starting at the botton of the TAK
    :param max_nb_clusters: max number of clusters before raising an error
    :return: list of default clusters names (["F", "E", "D", ...])
    """
    nb_clusters = len(list_len_clusters_ordered)
    if nb_clusters > max_nb_clusters:
        raise ValueError(
            f"The number of clusters should be lower than {max_nb_clusters}."
        )
    clusters_names_default = list(string.ascii_uppercase[:nb_clusters])
    return clusters_names_default

nice_plotly_show(fig)

Display nicely a plotly graph.

Parameters:

Name Type Description Default
fig

Plotly figure

required
Source code in opentak/visualization.py
1143
1144
1145
1146
1147
1148
1149
def nice_plotly_show(fig):
    """Display nicely a plotly graph.

    :param fig: Plotly figure
    """
    config = {"toImageButtonOptions": {"height": None, "width": None}}
    fig.show(config=config)

add_grid_on_tak_fig(fig, grid='xy', params=None)

Add grid on the TAK heatmap in the plotly fig "fig".

Note

default params for xgrid_params and ygrid_params: line_width = 0.5, line_dash = "dot", line_color = "grey", opacity = 0.5

Parameters:

Name Type Description Default
fig Figure

plotly heatmap TAK on which to add the grid

required
grid str

whether to add grid on x and/or y axes

'xy'
params dict | None

dict params for add_vline (grid for x), 2 keys : "x" and/or "y", and each value is a dictionary of parameters

None

Returns:

Type Description
Figure

copy of fig with grid

Source code in opentak/visualization.py
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
def add_grid_on_tak_fig(
    fig: go.Figure,
    grid: str = "xy",
    params: dict | None = None,
) -> go.Figure:
    """Add grid on the TAK heatmap in the plotly fig "fig".

    !!! note
        default params for xgrid_params and ygrid_params: line_width = 0.5, line_dash = "dot", line_color = "grey",
        opacity = 0.5

    :param fig: plotly heatmap TAK on which to add the grid
    :param grid: whether to add grid on x and/or y axes
    :param params: dict params for add_vline (grid for x), 2 keys : "x" and/or "y", and each value is a dictionary of
    parameters
    :return: copy of fig with grid
    """
    fig_cop = go.Figure(fig)

    if params is None:
        params = {}

    # Pre-compute subplot configuration
    multiple_plots = fig.layout.grid.columns is not None
    xaxis = "xaxis2" if multiple_plots else "xaxis"
    yaxis = "yaxis2" if multiple_plots else "yaxis"
    col = 2 if multiple_plots else 1

    # Define grid line functions mapping
    # python
    grid_functions = {
        "x": lambda **kwargs: fig_cop.add_vline(**kwargs),
        "y": lambda **kwargs: fig_cop.add_hline(**kwargs),
    }

    for dim in grid:
        dim_params = params.get(dim, {})
        if not isinstance(dim_params, dict):
            raise TypeError(f"params[{dim}] should be a dictionnary")

        default_params = {
            "line_width": 0.5,
            "line_dash": "dot",
            "line_color": "grey",
            "opacity": 0.5,
        }
        default_params.update(dim_params)

        # Computing max value of the TAK graph (we must exclude graphs that are not heatmaps, eg scatters
        # representing the dendogram
        val_max = max(
            [max(trace[dim]) for trace in fig_cop.data if isinstance(trace, go.Heatmap)]
        )

        # Get the appropriate grid function for this dimension
        add_grid_line = grid_functions.get(dim)
        if add_grid_line is None:
            continue

        selected_axis_dim = {"x": xaxis, "y": yaxis}
        # Add grid lines for all valid tick values
        for val in fig_cop["layout"][selected_axis_dim[dim]]["tickvals"]:
            if val <= val_max:
                line_params = default_params.copy()
                line_params[dim] = val
                line_params["col"] = col
                add_grid_line(**line_params)

    return fig_cop