Skip to content

Plot

plot_J_error_details(conf, tags_true, tags_pred, nbins=10, linear_bins=False)

Function to plot the histograms per single bin of each single screen tag to quantify the relative error as a function of J.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • tags_true (list) –

    The true tags of the validation set

  • tags_pred (list) –

    The predicted tags of the validation set

  • nbins (int, default: 10 ) –

    The number of bins in which to partition the data

  • linear_bins (bool, default: False ) –

    If True, the bins are linearly spaced, otherwise they are log spaced

Source code in src/speckcn2/plots.py
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
def plot_J_error_details(conf: dict,
                         tags_true: list,
                         tags_pred: list,
                         nbins: int = 10,
                         linear_bins: bool = False) -> None:
    """Function to plot the histograms per single bin of each single screen tag
    to quantify the relative error as a function of J.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    tags_true : list
        The true tags of the validation set
    tags_pred : list
        The predicted tags of the validation set
    nbins : int
        The number of bins in which to partition the data
    linear_bins : bool
        If True, the bins are linearly spaced, otherwise they are log spaced
    """

    nscreens = conf['speckle']['nscreens']
    data_dir = conf['speckle']['datadirectory']
    model_name = conf['model']['name']

    dirname = f'{data_dir}/{model_name}_score/J_bin_details'
    ensure_directory(dirname)

    if conf['preproc'].get('J_details', False):
        for screen_id in range(nscreens):
            print(f'\nComputing screen-{screen_id} details')

            # Collect the data
            params = []
            loss = []
            for i in range(len(tags_true)):
                params.append(tags_true[i][0,
                                           screen_id].detach().cpu().numpy())
                loss.append((
                    (tags_pred[i][0, screen_id] - tags_true[i][0, screen_id]) /
                    (tags_true[i][0, screen_id])).detach().cpu().numpy())
            params = np.array(params)
            loss = np.array(loss)

            if linear_bins:
                bins = np.linspace(min(params), max(params), num=nbins)
            else:
                bins = np.logspace(np.log10(min(params)),
                                   np.log10(max(params)),
                                   num=nbins)
            bin_indices = np.digitize(params, bins)
            # get the average and std of the error per bin of J[screen_id]
            bin_centers = 0.5 * (bins[:-1] + bins[1:])

            for idx, single_bin in enumerate(bin_centers):
                l_data = loss[bin_indices == idx]
                if len(l_data) == 0:
                    continue
                fig, axs = plt.subplots(1, 1, figsize=(5, 5))
                axs.hist(l_data, bins=50, alpha=0.5, density=True)
                mu = np.mean(l_data)
                sigma = np.std(l_data)
                print(
                    f'J-{screen_id} = {single_bin:.3g} -> mu = {mu:.3f}, sigma = {sigma:.3f}'
                )
                if sigma > 0:
                    x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100)
                    x = x[x > min(l_data)]
                    x = x[x < max(l_data)]
                    axs.plot(x,
                             stats.norm.pdf(x, mu, sigma),
                             label=f'Average err: {mu:.3f}, Std: {sigma:.3f}')
                axs.set_xlabel(f'Relative error J (screen-{screen_id})')
                axs.set_ylabel('Frequency')
                axs.legend()
                plt.title(f'J (screen-{screen_id}) value = {single_bin:.3g}')
                plt.tight_layout()
                plt.savefig(f'{dirname}/Jscreen{screen_id}_bin{idx}.png')
                plt.close()

plot_histo_losses(conf, test_losses, data_dir)

Plots the histogram of the losses.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • test_losses (list[dict]) –

    List of all the losses of the test set

  • data_dir (str) –

    The directory where the data is stored

Source code in src/speckcn2/plots.py
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
def plot_histo_losses(conf: dict, test_losses: list[dict],
                      data_dir: str) -> None:
    """Plots the histogram of the losses.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    test_losses : list[dict]
        List of all the losses of the test set
    data_dir : str
        The directory where the data is stored
    """
    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']

    dirname = f'{data_dir}/{model_name}_score/histo_losses'
    ensure_directory(dirname)

    fig, axs = plt.subplots(1, 1, figsize=(5, 5))
    for key in ['MAE', 'Fried', 'Isoplanatic', 'Scintillation_w']:
        loss = [d[key].detach().cpu() for d in test_losses]
        bins = np.logspace(np.log10(min(loss)), np.log10(max(loss)),
                           num=50).tolist()
        axs.hist(loss, bins=bins, alpha=0.5, label=key, density=True)
    axs.set_xlabel('Loss')
    axs.set_ylabel('Frequency')
    axs.set_yscale('log')
    axs.set_xscale('log')
    axs.legend()
    plt.title(f'Model: {model_name}')
    plt.tight_layout()
    plt.savefig(f'{dirname}/histo_losses_{model_name}.png')
    plt.close()

plot_loss(conf, model, data_dir)

Plots the loss of the model.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • model (Module) –

    The model to plot the loss of

  • data_dir (str) –

    The directory where the data is stored

Source code in src/speckcn2/plots.py
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
def plot_loss(conf: dict, model, data_dir: str) -> None:
    """Plots the loss of the model.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    model : torch.nn.Module
        The model to plot the loss of
    data_dir : str
        The directory where the data is stored
    """

    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']

    dirname = f'{data_dir}/{model_name}_score'
    ensure_directory(dirname)

    fig, axs = plt.subplots(1, 1, figsize=(5, 5))
    axs.plot(model.epoch, model.loss, label='Training loss')
    axs.plot(model.epoch, model.val_loss, label='Validation loss')
    axs.set_xlabel('Epoch')
    axs.set_ylabel('Loss')
    axs.set_yscale('log')
    axs.legend()
    plt.title(f'Model: {model_name}')
    plt.tight_layout()
    plt.savefig(f'{dirname}/loss_{model_name}.png')
    plt.close()

plot_param_histo(conf, test_losses, data_dir, measures)

Plots the histograms of different parameters.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • test_losses (list[dict]) –

    List of all the losses of the test set

  • data_dir (str) –

    The directory where the data is stored

  • measures (list) –

    The measures of the model

Source code in src/speckcn2/plots.py
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
def plot_param_histo(conf: dict, test_losses: list[dict], data_dir: str,
                     measures: list) -> None:
    """Plots the histograms of different parameters.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    test_losses : list[dict]
        List of all the losses of the test set
    data_dir : str
        The directory where the data is stored
    measures : list
        The measures of the model
    """
    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']

    dirname = f'{data_dir}/{model_name}_score'
    ensure_directory(dirname)

    for param_model, param_true, name, units in zip(
        ['Fried_pred', 'Isoplanatic_pred', 'Scintillation_w_pred'],
        ['Fried_true', 'Isoplanatic_true', 'Scintillation_w_true'],
        ['Fried parameter', 'Isoplanatic angle', 'Rytov index'],
        ['[m]', '[rad]', '[1]'],
    ):
        fig, axs = plt.subplots(1, 1, figsize=(5, 5))

        params_model = [d[param_model].detach().cpu() for d in measures]
        params_true = [d[param_true].detach().cpu() for d in measures]

        pairs = sorted(zip(params_true, params_model))
        params_true, params_model = zip(*pairs)
        params_true = np.array(params_true)
        params_model = np.array(params_model)

        bins = np.logspace(np.log10(min(params_true)),
                           np.log10(max(params_true)),
                           num=50).tolist()
        axs.hist(params_true,
                 bins=bins,
                 alpha=0.5,
                 label=param_true,
                 density=True)

        bins = np.logspace(np.log10(min(params_model)),
                           np.log10(max(params_model)),
                           num=50).tolist()
        axs.hist(params_model,
                 bins=bins,
                 alpha=0.5,
                 label=param_model,
                 density=True)

        axs.set_xlabel(f'{name} {units}')
        axs.set_xscale('log')
        axs.set_yscale('log')
        axs.set_ylabel('Frequency')
        axs.legend()
        plt.title(f'Model: {model_name}')
        plt.tight_layout()
        plt.savefig(f'{dirname}/histo_{param_true}_{model_name}.png')
        plt.close()

plot_param_vs_loss(conf, test_losses, data_dir, measures, no_sign=False, nbins=10, linear_bins=False)

Plots the parameter vs the loss. Optionally, it also plots the detailed histo for all the bins for the desired metrics.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • test_losses (list[dict]) –

    List of all the losses of the test set

  • data_dir (str) –

    The directory where the data is stored

  • measures (list) –

    The measures of the model

  • no_sign (bool, default: False ) –

    If True, it will plot the abs of the relative error

  • nbins (int, default: 10 ) –

    The number of bins in which to partition the data

  • linear_bins (bool, default: False ) –

    If True, the bins are linearly spaced, otherwise they are log spaced

Source code in src/speckcn2/plots.py
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
412
def plot_param_vs_loss(conf: dict,
                       test_losses: list[dict],
                       data_dir: str,
                       measures: list,
                       no_sign: bool = False,
                       nbins: int = 10,
                       linear_bins: bool = False) -> None:
    """Plots the parameter vs the loss. Optionally, it also plots the detailed
    histo for all the bins for the desired metrics.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    test_losses : list[dict]
        List of all the losses of the test set
    data_dir : str
        The directory where the data is stored
    measures : list
        The measures of the model
    no_sign : bool
        If True, it will plot the abs of the relative error
    nbins : int
        The number of bins in which to partition the data
    linear_bins : bool
        If True, the bins are linearly spaced, otherwise they are log spaced
    """
    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']

    dirname = f'{data_dir}/{model_name}_score'
    ensure_directory(dirname)

    for param, lname, name, units in zip(
        ['Fried_true', 'Isoplanatic_true', 'Scintillation_w_true'],
        ['Fried', 'Isoplanatic', 'Scintillation_w'],
        ['Fried parameter', 'Isoplanatic angle', 'Rytov index'],
        ['[m]', '[rad]', '[1]'],
    ):

        dirname = f'{data_dir}/{model_name}_score'
        p_data = [d[param].detach().cpu() for d in measures]
        if no_sign:
            l_data = [d[lname].detach().cpu() for d in test_losses]
        else:
            pname = lname.split('_true')[0] + '_pred'
            l_data = [((d[pname] - d[param]) / d[param]).detach().cpu()
                      for d in measures]

        pairs = sorted(zip(p_data, l_data))
        params, loss = zip(*pairs)
        params = np.array(params)
        loss = np.array(loss)

        if linear_bins:
            bins = np.linspace(min(params), max(params), num=nbins)
        else:
            bins = np.logspace(np.log10(min(params)),
                               np.log10(max(params)),
                               num=nbins)
        bin_indices = np.digitize(params, bins)
        bin_means = [
            loss[bin_indices == i].mean() if np.any(bin_indices == i) else 0
            for i in range(1, len(bins))
        ]
        bin_stds = [
            loss[bin_indices == i].std() if np.any(bin_indices == i) else 0
            for i in range(1, len(bins))
        ]
        bin_centers = 0.5 * (bins[:-1] + bins[1:])

        # Plotting the results
        fig, axs = plt.subplots(1, 1, figsize=(5, 5))
        axs.errorbar(bin_centers,
                     bin_means,
                     yerr=bin_stds,
                     marker='o',
                     linestyle='-',
                     alpha=0.75)

        # Plot error reference lines+shade
        axs.axhline(y=1.0, linestyle='--', color='tab:red', label='100% error')
        axs.axhline(y=0.5,
                    linestyle='--',
                    color='tab:orange',
                    label='50% error')
        axs.axhline(y=0.1,
                    linestyle='--',
                    color='tab:green',
                    label='10% error')
        axs.axhline(y=-1.0, linestyle='--', color='tab:red')
        axs.axhline(
            y=-0.5,
            linestyle='--',
            color='tab:orange',
        )
        axs.axhline(
            y=-0.1,
            linestyle='--',
            color='tab:green',
        )
        axs.axhline(
            y=0,
            linestyle='--',
            color='black',
        )
        plt.tight_layout()
        x_min, x_max = axs.get_xlim()
        axs.fill_between([x_min, x_max],
                         -0.1,
                         0.1,
                         color='tab:green',
                         alpha=0.1)
        axs.fill_between([x_min, x_max],
                         0.1,
                         0.5,
                         color='tab:orange',
                         alpha=0.1)
        axs.fill_between([x_min, x_max],
                         -0.5,
                         -0.1,
                         color='tab:orange',
                         alpha=0.1)
        axs.fill_between([x_min, x_max], 0.5, 1.0, color='tab:red', alpha=0.1)
        axs.fill_between([x_min, x_max],
                         -1.0,
                         -0.5,
                         color='tab:red',
                         alpha=0.1)
        axs.set_xlabel(f'{name} {units}')
        axs.set_xscale('log')
        axs.set_yscale('symlog', linthresh=0.1)
        axs.set_ylabel('Relative error')
        yticks = [-1, -0.5, -0.1, 0, 0.1, 0.5, 1]
        plt.yticks(yticks)
        yticklabels = ['-100%', '-50%', '-10%', '0', '10%', '50%', '100%']
        plt.gca().set_yticklabels(yticklabels)
        plt.title(f'Model: {model_name}')
        plt.tight_layout()
        plt.savefig(f'{dirname}/{param}_vs_sum_{model_name}.png')
        plt.close()

        # If specified, plot the histogram per single bin
        if conf['preproc'].get(lname + '_details', False):
            print(f'\nComputing {lname} details')
            dirname = f'{data_dir}/{model_name}_score/{lname}_bin_details'
            ensure_directory(dirname)

            for idx, single_bin in enumerate(bin_centers):
                l_data = loss[bin_indices == idx]
                if len(l_data) == 0:
                    continue
                fig, axs = plt.subplots(1, 1, figsize=(5, 5))
                axs.hist(l_data, bins=50, alpha=0.5, density=True)
                mu = np.mean(l_data)
                sigma = np.std(l_data)
                if no_sign:
                    print(
                        'Warning: you are requesting the analysis of absolute value using'
                        + ' normal gaussian assumption.' +
                        'This is not correct and the error will be overestimated.'
                    )
                print(
                    f'{lname} = {single_bin:.3f} -> mu = {mu:.3f}, sigma = {sigma:.3f}'
                )
                if sigma > 0:
                    x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100)
                    x = x[x > min(l_data)]
                    x = x[x < max(l_data)]
                    axs.plot(x,
                             stats.norm.pdf(x, mu, sigma),
                             label=f'Average err: {mu:.3f}, Std: {sigma:.3f}')
                axs.set_xlabel(f'Relative error {lname}')
                axs.set_ylabel('Frequency')
                axs.legend()
                plt.title(f'{lname} value = {single_bin:.3f} {units}')
                plt.tight_layout()
                plt.savefig(f'{dirname}/{lname}_bin{idx}.png')
                plt.close()

plot_samples_in_ensemble(conf, test_set, device, model, criterion, trimming=0.2, n_max_plots=100)

Plot the prediction over a sample and compare it with the ones from its ensemble.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • test_set (list) –

    The test set

  • device (device) –

    The device to use

  • model (Torch) –

    The trained model

  • criterion (ComposableLoss) –

    The loss function

  • trimming (float, default: 0.2 ) –

    The trimming to use for the mean

  • n_max_plots (int, default: 100 ) –

    The maximum number of plots

Source code in src/speckcn2/plots.py
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
def plot_samples_in_ensemble(conf: dict,
                             test_set: list,
                             device: Device,
                             model: nn.Torch,
                             criterion: ComposableLoss,
                             trimming: float = 0.2,
                             n_max_plots: int = 100) -> None:
    """Plot the prediction over a sample and compare it with the ones
    from its ensemble.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    test_set : list
        The test set
    device : torch.device
        The device to use
    model : nn.Torch
        The trained model
    criterion : ComposableLoss
        The loss function
    trimming : float
        The trimming to use for the mean
    n_max_plots : int
        The maximum number of plots
    """

    data_directory = conf['speckle']['datadirectory']
    model_name = conf['model']['name']
    n_screens = conf['speckle']['nscreens']

    dirname = f'{data_directory}/{model_name}_score/single-shot_predictions'
    ensure_directory(dirname)

    # group the sets that have the same n[1]
    grouped_test_set: Dict = {}
    for n in test_set:
        key = tuple(n[1])
        if key not in grouped_test_set:
            grouped_test_set[key] = []
        grouped_test_set[key].append(n)
    print('\nAnalysis of single shot predictions')
    print(f'Number of samples: {len(test_set)}')
    print(f'Number of speckle groups: {len(grouped_test_set)}')
    # Define a random probability to plot each ensemble
    p_plot = n_max_plots / len(grouped_test_set)

    # In the end, we will plot groups that have uncommon values of loss
    loss_min = 1e10
    loss_max = 0
    ensemble_count = 0

    ensemble = EnsembleModel(conf, device)
    with torch.no_grad():
        model.eval()

        for key, value in grouped_test_set.items():
            _outputs = []
            _all_tags_pred = []

            for count, speckle in enumerate(value, 1):
                output, target, _ = ensemble(model, [speckle])
                _outputs.append(output.detach().cpu().numpy())
                loss, losses = criterion(output, target)

                # Get the Cn2 profile and the recovered tags
                Cn2_pred = criterion.reconstruct_cn2(output)
                Cn2_true = criterion.reconstruct_cn2(target)
                recovered_tag_pred = criterion.get_J(output)
                _all_tags_pred.append(recovered_tag_pred.detach().cpu())
                # and get all the measures
                all_measures = criterion._get_all_measures(
                    output, target, Cn2_pred, Cn2_true)

                if count == 1:
                    fig, ax = plt.subplots(1, 3, figsize=(12, 4))
                    loss_0 = loss

                    # (0) Plot the speckle pattern
                    ax[0].axis('off')  # Hide axis
                    ax[0].imshow(speckle[0][0, :, :], cmap='bone')

                    # (1) Plot J vs nscreens
                    recovered_tag_true = criterion.get_J(target)
                    ax[1].plot(recovered_tag_true.squeeze(0).detach().cpu(),
                               '*',
                               label='True',
                               color='tab:green',
                               markersize=10,
                               markeredgecolor='black',
                               zorder=100)
                    ax[1].plot(recovered_tag_pred.squeeze(0).detach().cpu(),
                               'o',
                               label='This speckle',
                               color='tab:red',
                               markersize=7,
                               markeredgecolor='black',
                               zorder=90)

                    # (2) Plot the parameters of this speckle prediction
                    ax[2].axis('off')  # Hide axis
                    recap_info = f'LOSS TERMS:\nTotal Loss: {loss.item():.4g}\n'
                    # the individual losses
                    for key, value in losses.items():
                        recap_info += f'{key}: {value.item():.4g}\n'
                    recap_info += '-------------------\nPARAMETERS:\n'
                    # then the single parameters
                    for key, value in all_measures.items():
                        recap_info += f'{key}: {value:.4g}\n'
                    ax[2].text(0.5,
                               0.5,
                               recap_info,
                               horizontalalignment='center',
                               verticalalignment='center',
                               fontsize=10,
                               color='black')

            # Now at the end of the loop, we decide if this set needs to plotted or not
            # by checking that the loss is uncommon, or via a random probability
            if loss_0 > loss_max or loss_0 < loss_min or np.random.rand(
            ) < p_plot:
                avg_tags_trim = stats.trim_mean(_all_tags_pred,
                                                trimming).squeeze()
                percentiles_50 = np.percentile(_all_tags_pred, [25, 75],
                                               axis=0).squeeze()
                percentiles_68 = np.percentile(_all_tags_pred, [16, 84],
                                               axis=0).squeeze()
                percentiles_95 = np.percentile(_all_tags_pred, [2.5, 97.5],
                                               axis=0).squeeze()

                x_vals = np.arange(n_screens)
                alp = 0.3
                ax[1].plot(avg_tags_trim,
                           label='Mean',
                           color='tab:red',
                           zorder=50)
                ax[1].fill_between(x_vals,
                                   percentiles_50[0],
                                   percentiles_50[1],
                                   color='gold',
                                   alpha=alp,
                                   label='50% CI',
                                   zorder=5)
                ax[1].fill_between(x_vals,
                                   percentiles_68[0],
                                   percentiles_50[0],
                                   color='cadetblue',
                                   alpha=alp,
                                   label='68% CI',
                                   zorder=4)
                ax[1].fill_between(x_vals,
                                   percentiles_50[1],
                                   percentiles_68[1],
                                   color='cadetblue',
                                   alpha=alp,
                                   zorder=4)
                ax[1].fill_between(x_vals,
                                   percentiles_95[0],
                                   percentiles_68[0],
                                   color='blue',
                                   label='95% CI',
                                   alpha=alp,
                                   zorder=3)
                ax[1].fill_between(x_vals,
                                   percentiles_68[1],
                                   percentiles_95[1],
                                   color='blue',
                                   alpha=alp,
                                   zorder=3)

                ax[1].set_yscale('log')
                ax[1].set_ylabel('J')
                ax[1].set_xlabel('# screen')
                ax[1].legend()
                fig.tight_layout()
                plt.subplots_adjust(top=0.92)
                plt.suptitle(
                    'Prediction from a single speckle, compared to similar')
                plt.savefig(
                    f'{dirname}/single_speckle_loss{loss_0.item():.4g}.png')
                loss_max = max(loss_0, loss_max)
                loss_min = min(loss_0, loss_min)
                ensemble_count += 1

            plt.close()

            if ensemble_count >= n_max_plots:
                break

plot_time(conf, model, data_dir)

Plots the time per epoch of the model.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • model (Module) –

    The model to plot the loss of

  • data_dir (str) –

    The directory where the data is stored

Source code in src/speckcn2/plots.py
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
def plot_time(conf: dict, model, data_dir: str) -> None:
    """Plots the time per epoch of the model.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    model : torch.nn.Module
        The model to plot the loss of
    data_dir : str
        The directory where the data is stored
    """

    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']

    dirname = f'{data_dir}/{model_name}_score'
    ensure_directory(dirname)

    fig, axs = plt.subplots(1, 1, figsize=(5, 5))
    axs.plot(model.epoch, model.time, label='Time per epoch')
    axs.set_xlabel('Epoch')
    axs.set_ylabel('Time [s]')
    axs.legend()
    plt.title(f'Model: {model_name}')
    plt.tight_layout()
    plt.savefig(f'{dirname}/time_{model_name}.png')
    plt.close()

score_plot(conf, inputs, tags, loss, losses, i, counter, measures, Cn2_pred, Cn2_true, recovered_tag_pred, recovered_tag_true)

Plots side by side: - [0:Nensemble] the input images (single or ensemble) - [-3] the predicted/exact tags J - [-2] the Cn2 profile - [-1] the different information of the loss normalize value in model units.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

  • inputs (Tensor) –

    The input speckle patterns

  • tags (list) –

    The exact tags of the data

  • loss (Tensor) –

    The total loss of the model (for this prediction)

  • losses (dict) –

    The individual losses of the model

  • i (int) –

    The batch index of the image

  • counter (int) –

    The global index of the image

  • measures (dict) –

    The different measures of the model

  • Cn2_pred (Tensor) –

    The predicted Cn2 profile

  • Cn2_true (Tensor) –

    The true Cn2 profile

  • recovered_tag_pred (Tensor) –

    The predicted tags

  • recovered_tag_true (Tensor) –

    The true tags

Source code in src/speckcn2/plots.py
 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
def score_plot(
    conf: dict,
    inputs: torch.Tensor,
    tags: list,
    loss: torch.Tensor,
    losses: dict,
    i: int,
    counter: int,
    measures: dict,
    Cn2_pred: torch.Tensor,
    Cn2_true: torch.Tensor,
    recovered_tag_pred: torch.Tensor,
    recovered_tag_true: torch.Tensor,
) -> None:
    """Plots side by side:
    - [0:Nensemble] the input images (single or ensemble)
    - [-3] the predicted/exact tags J
    - [-2] the Cn2 profile
    - [-1] the different information of the loss
    normalize value in model units.

    Parameters
    ----------
    conf : dict
        Dictionary containing the configuration
    inputs : torch.Tensor
        The input speckle patterns
    tags : list
        The exact tags of the data
    loss : torch.Tensor
        The total loss of the model (for this prediction)
    losses : dict
        The individual losses of the model
    i : int
        The batch index of the image
    counter : int
        The global index of the image
    measures : dict
        The different measures of the model
    Cn2_pred : torch.Tensor
        The predicted Cn2 profile
    Cn2_true : torch.Tensor
        The true Cn2 profile
    recovered_tag_pred : torch.Tensor
        The predicted tags
    recovered_tag_true : torch.Tensor
        The true tags
    """
    model_name = conf['model']['name']
    data_dir = conf['speckle']['datadirectory']
    ensemble = conf['preproc'].get('ensemble', 1)
    hs = conf['speckle']['splits']
    nscreens = conf['speckle']['nscreens']
    if len(hs) != nscreens:
        print(
            'WARNING: The number of screens does not match the number of splits'
        )
        return

    dirname = f'{data_dir}/{model_name}_score/single-shot_predictions'
    ensure_directory(dirname)

    fig, axs = plt.subplots(1, 3 + ensemble, figsize=(4 * (2 + ensemble), 3.5))

    # (1) Plot the input images
    for n in range(ensemble):
        img = inputs[ensemble * i + n].detach().cpu().squeeze().abs()
        axs[n].imshow(img, cmap='bone')
    title_string = f'Input {ensemble} images' if ensemble > 1 else 'Input single image'
    axs[1].set_title(title_string)

    # (2) Plot J vs nscreens
    axs[-3].plot(recovered_tag_true.squeeze(0).detach().cpu(),
                 'o',
                 label='True')
    axs[-3].plot(recovered_tag_pred.squeeze(0).detach().cpu(),
                 '.',
                 color='tab:red',
                 label='Predicted')
    axs[-3].set_yscale('log')
    axs[-3].set_ylabel('J')
    axs[-3].set_xlabel('# screen')
    axs[-3].legend()

    # (3) Plot Cn2 vs altitude
    axs[-2].plot(hs, Cn2_true.squeeze(0).detach().cpu(), 'o', label='True')
    axs[-2].plot(hs,
                 Cn2_pred.squeeze(0).detach().cpu(),
                 '.',
                 color='tab:red',
                 label='Predicted')
    axs[-2].set_xscale('log')
    axs[-2].set_yscale('log')
    axs[-2].set_ylabel(r'$Cn^2$')
    axs[-2].set_xlabel('Altitude [m]')

    # (4) Plot the recap information
    axs[-1].axis('off')  # Hide axis
    recap_info = f'LOSS TERMS:\nTotal Loss: {loss.item():.4g}\n'
    # the individual losses
    for key, value in losses.items():
        recap_info += f'{key}: {value.item():.4g}\n'
    recap_info += '-------------------\nPARAMETERS:\n'
    # then the single parameters
    for key, value in measures.items():
        recap_info += f'{key}: {value:.4g}\n'
    axs[-1].text(0.5,
                 0.5,
                 recap_info,
                 horizontalalignment='center',
                 verticalalignment='center',
                 fontsize=10,
                 color='black')

    plt.tight_layout()
    plt.savefig(f'{dirname}/single_speckle_loss{loss.item():.4g}.png')
    plt.close()