• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jveitchmichaelis / rascal / 35347081119

18 Sep 2026 12:53PM UTC coverage: 91.618% (-0.07%) from 91.691%
35347081119

Pull #99

github

cylammarco
more fine tuning
Pull Request #99: Ready for 0.3.11

13 of 14 new or added lines in 4 files covered. (92.86%)

2 existing lines in 1 file now uncovered.

1880 of 2052 relevant lines covered (91.62%)

3.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.14
/src/rascal/plotting.py
1
import logging
4 ✔
2
import numpy as np
4 ✔
3
from scipy import signal
4 ✔
4

5
from .util import load_calibration_lines
4 ✔
6
from .util import gauss
4 ✔
7
from .util import filter_intensity
4 ✔
8
from .util import filter_separation
4 ✔
9

10
logger = logging.getLogger("plotting")
4 ✔
11

12

13
def _import_matplotlib():
4 ✔
14
    """
15
    Call to import matplotlib.
16

17
    """
18

19
    try:
4 ✔
20
        global plt
21
        import matplotlib.pyplot as plt
4 ✔
22

23
    except ImportError:
×
24
        logger.error("matplotlib package not available.")
×
25

26

27
def _import_plotly():
4 ✔
28
    """
29
    Call to import plotly.
30

31
    """
32

33
    try:
4 ✔
34
        global go
35
        global pio
36
        global psp
37
        import plotly.graph_objects as go
4 ✔
38
        import plotly.io as pio
4 ✔
39
        import plotly.subplots as psp
4 ✔
40

41
        pio.templates["CN"] = go.layout.Template(
4 ✔
42
            layout_colorway=[
43
                "#1f77b4",
44
                "#ff7f0e",
45
                "#2ca02c",
46
                "#d62728",
47
                "#9467bd",
48
                "#8c564b",
49
                "#e377c2",
50
                "#7f7f7f",
51
                "#bcbd22",
52
                "#17becf",
53
            ]
54
        )
55

56
        # setting Google color palette as default
57
        pio.templates.default = "CN"
4 ✔
58

59
    except ImportError:
×
60
        logger.error("plotly package not available.")
×
61

62

63
def plot_calibration_lines(
4 ✔
64
    elements=[],
65
    min_atlas_wavelength=3000.0,
66
    max_atlas_wavelength=15000.0,
67
    min_intensity=5.0,
68
    min_distance=0.0,
69
    brightest_n_lines=None,
70
    pixel_scale=1.0,
71
    vacuum=False,
72
    pressure=101325.0,
73
    temperature=273.15,
74
    relative_humidity=0.0,
75
    label=False,
76
    log=False,
77
    save_fig=False,
78
    fig_type="png",
79
    filename=None,
80
    display=True,
81
    linelist="nist",
82
    fig_kwarg={"figsize": (12, 8)},
83
):
84
    """
85
    Plot the expected arc spectrum. Only available in matplotlib at the moment.
86

87
    Parameters
88
    ----------
89
    elements: list
90
        List of short element names, e.g. He as per NIST
91
    linelist: str
92
        Either 'nist' to use the default lines or path to a linelist file.
93
    min_atlas_wavelength: int
94
        Minimum wavelength to search, Angstrom
95
    max_atlas_wavelength: int
96
        Maximum wavelength to search, Angstrom
97
    min_intensity: int
98
        Minimum intensity to search, per NIST
99
    min_distance: int
100
        All ines within this distance from other lines are treated
101
        as unresolved, all of them get removed from the list.
102
    brightest_n_lines: int
103
        Only return the n brightest lines
104
    vacuum: bool
105
        Return vacuum wavelengths
106
    pressure: float
107
        Atmospheric pressure, Pascal
108
    temperature: float
109
        Temperature in Kelvin, default room temp
110
    relative_humidity: float
111
        Relative humidity, percent
112
    log: bool
113
        Plot intensities in log scale
114
    save_fig: boolean (default: False)
115
        Save an image if set to True. matplotlib uses the pyplot.save_fig()
116
        while the plotly uses the pio.write_html() or pio.write_image().
117
        The support format types should be provided in fig_type.
118
    fig_type: string (default: 'png')
119
        Image type to be saved, choose from:
120
        jpg, png, svg, pdf and iframe. Delimiter is '+'.
121
    filename: string (default: None)
122
        Provide a filename or full path. If the extension is not provided
123
        it is defaulted to png.
124
    display: boolean (Default: False)
125
        Set to True to display disgnostic plot.
126
    Returns
127
    -------
128
    fig: matplotlib figure object
129
    """
130

131
    _import_matplotlib()
4 ✔
132

133
    # the min_intensity and min_distance are set to 0.0 because the
134
    # simulated spectrum would contain them. These arguments only
135
    # affect the labelling.
136
    element_list, wavelength_list, intensity_list = load_calibration_lines(
4 ✔
137
        elements=elements,
138
        linelist=linelist,
139
        min_atlas_wavelength=min_atlas_wavelength,
140
        max_atlas_wavelength=max_atlas_wavelength,
141
        min_intensity=0.0,
142
        min_distance=0.0,
143
        brightest_n_lines=brightest_n_lines,
144
        vacuum=vacuum,
145
        pressure=pressure,
146
        temperature=temperature,
147
        relative_humidity=relative_humidity,
148
    )
149

150
    # Nyquist sampling rate (2.5) for CCD at seeing of 1 arcsec
151
    sigma = pixel_scale * 2.5 * 1.0
4 ✔
152
    x = np.arange(-100, 100.001, 0.001)
4 ✔
153
    gaussian = gauss(x, a=1.0, x0=0.0, sigma=sigma)
4 ✔
154

155
    # Generate the equally spaced-wavelength array, and the
156
    # corresponding intensity
157
    w = np.around(
4 ✔
158
        np.arange(min_atlas_wavelength, max_atlas_wavelength + 0.001, 0.001),
159
        decimals=3,
160
    ).astype("float64")
161
    i = np.zeros_like(w)
4 ✔
162

163
    for e in elements:
4 ✔
164
        i[np.isin(w, np.around(wavelength_list[element_list == e], decimals=3))] += intensity_list[element_list == e]
4 ✔
165
    # Convolve to simulate the arc spectrum
166
    model_spectrum = signal.convolve(i, gaussian, mode="same")
4 ✔
167

168
    # now clean up by min_intensity and min_distance
169
    intensity_mask = filter_intensity(
4 ✔
170
        elements,
171
        np.column_stack((element_list, wavelength_list, intensity_list)),
172
        min_intensity=min_intensity,
173
    )
174
    wavelength_list = wavelength_list[intensity_mask]
4 ✔
175
    intensity_list = intensity_list[intensity_mask]
4 ✔
176
    element_list = element_list[intensity_mask]
4 ✔
177

178
    distance_mask = filter_separation(wavelength_list, min_separation=min_distance)
4 ✔
179
    wavelength_list = wavelength_list[distance_mask]
4 ✔
180
    intensity_list = intensity_list[distance_mask]
4 ✔
181
    element_list = element_list[distance_mask]
4 ✔
182

183
    fig = plt.figure(**fig_kwarg)
4 ✔
184

185
    for j, e in enumerate(elements):
4 ✔
186
        e_mask = element_list == e
4 ✔
187
        markerline, stemline, _ = plt.stem(
4 ✔
188
            wavelength_list[e_mask],
189
            intensity_list[e_mask],
190
            label=e,
191
            linefmt="C{}-".format(j),
192
        )
193
        plt.setp(stemline, linewidth=2.0)
4 ✔
194
        plt.setp(markerline, markersize=2.5, color="C{}".format(j))
4 ✔
195

196
        if label:
4 ✔
197
            for _w in wavelength_list[e_mask]:
4 ✔
198
                plt.text(
4 ✔
199
                    _w,
200
                    max(model_spectrum) * 1.05,
201
                    s="{}: {:1.2f}".format(e, _w),
202
                    rotation=90,
203
                    bbox=dict(facecolor="white", alpha=1),
204
                )
205

206
            plt.vlines(
4 ✔
207
                wavelength_list[e_mask],
208
                intensity_list[e_mask],
209
                max(model_spectrum) * 1.25,
210
                linestyles="dashed",
211
                lw=0.5,
212
                color="grey",
213
            )
214

215
    plt.plot(w, model_spectrum, lw=1.0, c="k", label="Simulated Arc Spectrum")
4 ✔
216
    if vacuum:
4 ✔
217
        plt.xlabel("Vacuum Wavelength / A")
×
218
    else:
219
        plt.xlabel("Air Wavelength / A")
4 ✔
220
    plt.ylabel("NIST intensity")
4 ✔
221
    plt.grid()
4 ✔
222
    plt.xlim(min(w), max(w))
4 ✔
223
    plt.ylim(0, max(model_spectrum) * 1.25)
4 ✔
224
    plt.legend()
4 ✔
225
    plt.tight_layout()
4 ✔
226
    if log:
4 ✔
227
        plt.ylim(ymin=min_intensity * 0.75)
×
228
        plt.yscale("log")
×
229

230
    if save_fig:
4 ✔
231
        fig_type = fig_type.split("+")
4 ✔
232

233
        if filename is None:
4 ✔
234
            filename_output = "rascal_arc"
×
235

236
        else:
237
            filename_output = filename
4 ✔
238

239
        for t in fig_type:
4 ✔
240
            if t in ["jpg", "png", "svg", "pdf"]:
4 ✔
241
                plt.savefig(filename_output + "." + t, format=t)
4 ✔
242

243
    if display:
4 ✔
244
        plt.show()
4 ✔
245

246
    return fig
4 ✔
247

248

249
def plot_search_space(
4 ✔
250
    calibrator,
251
    fit_coeff=None,
252
    top_n_candidate=3,
253
    weighted=True,
254
    save_fig=False,
255
    fig_type="png",
256
    filename=None,
257
    return_jsonstring=False,
258
    renderer="default",
259
    display=True,
260
):
261
    """
262
    Plots the peak/arc line pairs that are considered as potential match
263
    candidates.
264

265
    If fit fit_coefficients are provided, the model solution will be
266
    overplotted.
267

268
    Parameters
269
    ----------
270
    fit_coeff: list (default: None)
271
        List of best polynomial fit_coefficients
272
    top_n_candidate: int (default: 3)
273
        Top ranked lines to be fitted.
274
    weighted: (default: True)
275
        Draw sample based on the distance from the matched known wavelength
276
        of the atlas.
277
    save_fig: boolean (default: False)
278
        Save an image if set to True. matplotlib uses the pyplot.save_fig()
279
        while the plotly uses the pio.write_html() or pio.write_image().
280
        The support format types should be provided in fig_type.
281
    fig_type: string (default: 'png')
282
        Image type to be saved, choose from:
283
        jpg, png, svg, pdf and iframe. Delimiter is '+'.
284
    filename: (default: None)
285
        The destination to save the image.
286
    return_jsonstring: (default: False)
287
        Set to True to save the plotly figure as json string. Ignored if
288
        matplotlib is used.
289
    renderer: (default: 'default')
290
        Set the rendered for the plotly display. Ignored if matplotlib is
291
        used.
292
    display: boolean (Default: False)
293
        Set to True to display disgnostic plot.
294

295
    Return
296
    ------
297
    json object if return_jsonstring is True.
298

299

300
    """
301

302
    # Get top linear estimates and combine
303
    candidate_peak, candidate_arc = calibrator._get_most_common_candidates(
4 ✔
304
        calibrator.candidates,
305
        top_n_candidate=top_n_candidate,
306
        weighted=weighted,
307
    )
308

309
    # Get the search space boundaries
310
    x = calibrator.pixel_list
4 ✔
311

312
    m_1 = (calibrator.max_wavelength - calibrator.min_wavelength) / calibrator.pixel_list.max()
4 ✔
313
    y_1 = m_1 * x + calibrator.min_wavelength
4 ✔
314

315
    m_2 = (
4 ✔
316
        calibrator.max_wavelength + calibrator.range_tolerance - (calibrator.min_wavelength + calibrator.range_tolerance)
317
    ) / calibrator.pixel_list.max()
318
    y_2 = m_2 * x + calibrator.min_wavelength + calibrator.range_tolerance
4 ✔
319

320
    m_3 = (
4 ✔
321
        calibrator.max_wavelength - calibrator.range_tolerance - (calibrator.min_wavelength - calibrator.range_tolerance)
322
    ) / calibrator.pixel_list.max()
323
    y_3 = m_3 * x + (calibrator.min_wavelength - calibrator.range_tolerance)
4 ✔
324

325
    if calibrator.plot_with_matplotlib:
4 ✔
326
        _import_matplotlib()
4 ✔
327

328
        fig = plt.figure(figsize=(10, 10))
4 ✔
329

330
        # Plot all-pairs
331
        plt.scatter(*calibrator.pairs.T, alpha=0.2, color="C0", label="All pairs")
4 ✔
332

333
        plt.scatter(
4 ✔
334
            calibrator._merge_candidates(calibrator.candidates)[:, 0],
335
            calibrator._merge_candidates(calibrator.candidates)[:, 1],
336
            alpha=0.2,
337
            color="C1",
338
            label="Candidate Pairs",
339
        )
340

341
        # Tolerance region around the minimum wavelength
342
        plt.text(
4 ✔
343
            5,
344
            calibrator.min_wavelength + 100,
345
            "Min wavelength (user-supplied)",
346
        )
347
        plt.hlines(
4 ✔
348
            calibrator.min_wavelength,
349
            0,
350
            calibrator.pixel_list.max(),
351
            color="k",
352
        )
353
        plt.hlines(
4 ✔
354
            calibrator.min_wavelength + calibrator.range_tolerance,
355
            0,
356
            calibrator.pixel_list.max(),
357
            linestyle="dashed",
358
            alpha=0.5,
359
            color="k",
360
        )
361
        plt.hlines(
4 ✔
362
            calibrator.min_wavelength - calibrator.range_tolerance,
363
            0,
364
            calibrator.pixel_list.max(),
365
            linestyle="dashed",
366
            alpha=0.5,
367
            color="k",
368
        )
369

370
        # Tolerance region around the maximum wavelength
371
        plt.text(
4 ✔
372
            5,
373
            calibrator.max_wavelength + 100,
374
            "Max wavelength (user-supplied)",
375
        )
376
        plt.hlines(
4 ✔
377
            calibrator.max_wavelength,
378
            0,
379
            calibrator.pixel_list.max(),
380
            color="k",
381
        )
382
        plt.hlines(
4 ✔
383
            calibrator.max_wavelength + calibrator.range_tolerance,
384
            0,
385
            calibrator.pixel_list.max(),
386
            linestyle="dashed",
387
            alpha=0.5,
388
            color="k",
389
        )
390
        plt.hlines(
4 ✔
391
            calibrator.max_wavelength - calibrator.range_tolerance,
392
            0,
393
            calibrator.pixel_list.max(),
394
            linestyle="dashed",
395
            alpha=0.5,
396
            color="k",
397
        )
398

399
        # The line from (first pixel, minimum wavelength) to
400
        # (last pixel, maximum wavelength), and the two lines defining the
401
        # tolerance region.
402
        plt.plot(x, y_1, label="Linear Fit", color="C3")
4 ✔
403
        plt.plot(x, y_2, linestyle="dashed", label="Tolerance Region", color="C3")
4 ✔
404
        plt.plot(x, y_3, linestyle="dashed", color="C3")
4 ✔
405

406
        if fit_coeff is not None:
4 ✔
407
            plt.scatter(
×
408
                calibrator.peaks,
409
                calibrator.polyval(calibrator.peaks, fit_coeff),
410
                color="C4",
411
                label="Solution",
412
            )
413

414
        plt.scatter(
4 ✔
415
            candidate_peak,
416
            candidate_arc,
417
            color="C2",
418
            label="Best Candidate Pairs",
419
        )
420

421
        plt.xlim(0, calibrator.pixel_list.max())
4 ✔
422
        plt.ylim(
4 ✔
423
            calibrator.min_wavelength - calibrator.range_tolerance,
424
            calibrator.max_wavelength + calibrator.range_tolerance,
425
        )
426

427
        plt.xlabel("Wavelength / A")
4 ✔
428
        plt.ylabel("Pixel")
4 ✔
429
        plt.legend()
4 ✔
430
        plt.grid()
4 ✔
431
        plt.tight_layout()
4 ✔
432

433
        if save_fig:
4 ✔
434
            fig_type = fig_type.split("+")
4 ✔
435

436
            if filename is None:
4 ✔
437
                filename_output = "rascal_hough_search_space"
×
438

439
            else:
440
                filename_output = filename
4 ✔
441

442
            for t in fig_type:
4 ✔
443
                if t in ["jpg", "png", "svg", "pdf"]:
4 ✔
444
                    plt.savefig(filename_output + "." + t, format=t)
4 ✔
445

446
        if display:
4 ✔
447
            plt.show()
×
448

449
        return fig
4 ✔
450

451
    elif calibrator.plot_with_plotly:
4 ✔
452
        _import_plotly()
4 ✔
453

454
        fig = go.Figure()
4 ✔
455

456
        # Plot all-pairs
457
        fig.add_trace(
4 ✔
458
            go.Scatter(
459
                x=calibrator.pairs[:, 0],
460
                y=calibrator.pairs[:, 1],
461
                mode="markers",
462
                name="All Pairs",
463
                marker=dict(color=pio.templates["CN"].layout.colorway[0], opacity=0.2),
464
            )
465
        )
466

467
        fig.add_trace(
4 ✔
468
            go.Scatter(
469
                x=calibrator._merge_candidates(calibrator.candidates)[:, 0],
470
                y=calibrator._merge_candidates(calibrator.candidates)[:, 1],
471
                mode="markers",
472
                name="Candidate Pairs",
473
                marker=dict(color=pio.templates["CN"].layout.colorway[1], opacity=0.2),
474
            )
475
        )
476
        fig.add_trace(
4 ✔
477
            go.Scatter(
478
                x=candidate_peak,
479
                y=candidate_arc,
480
                mode="markers",
481
                name="Best Candidate Pairs",
482
                marker=dict(color=pio.templates["CN"].layout.colorway[2]),
483
            )
484
        )
485

486
        # Tolerance region around the minimum wavelength
487
        fig.add_trace(
4 ✔
488
            go.Scatter(
489
                x=[0, calibrator.pixel_list.max()],
490
                y=[calibrator.min_wavelength, calibrator.min_wavelength],
491
                name="Min/Maximum",
492
                mode="lines",
493
                line=dict(color="black"),
494
            )
495
        )
496
        fig.add_trace(
4 ✔
497
            go.Scatter(
498
                x=[0, calibrator.pixel_list.max()],
499
                y=[
500
                    calibrator.min_wavelength + calibrator.range_tolerance,
501
                    calibrator.min_wavelength + calibrator.range_tolerance,
502
                ],
503
                name="Tolerance Range",
504
                mode="lines",
505
                line=dict(color="black", dash="dash"),
506
            )
507
        )
508
        fig.add_trace(
4 ✔
509
            go.Scatter(
510
                x=[0, calibrator.pixel_list.max()],
511
                y=[
512
                    calibrator.min_wavelength - calibrator.range_tolerance,
513
                    calibrator.min_wavelength - calibrator.range_tolerance,
514
                ],
515
                showlegend=False,
516
                mode="lines",
517
                line=dict(color="black", dash="dash"),
518
            )
519
        )
520

521
        # Tolerance region around the minimum wavelength
522
        fig.add_trace(
4 ✔
523
            go.Scatter(
524
                x=[0, calibrator.pixel_list.max()],
525
                y=[calibrator.max_wavelength, calibrator.max_wavelength],
526
                showlegend=False,
527
                mode="lines",
528
                line=dict(color="black"),
529
            )
530
        )
531
        fig.add_trace(
4 ✔
532
            go.Scatter(
533
                x=[0, calibrator.pixel_list.max()],
534
                y=[
535
                    calibrator.max_wavelength + calibrator.range_tolerance,
536
                    calibrator.max_wavelength + calibrator.range_tolerance,
537
                ],
538
                showlegend=False,
539
                mode="lines",
540
                line=dict(color="black", dash="dash"),
541
            )
542
        )
543
        fig.add_trace(
4 ✔
544
            go.Scatter(
545
                x=[0, calibrator.pixel_list.max()],
546
                y=[
547
                    calibrator.max_wavelength - calibrator.range_tolerance,
548
                    calibrator.max_wavelength - calibrator.range_tolerance,
549
                ],
550
                showlegend=False,
551
                mode="lines",
552
                line=dict(color="black", dash="dash"),
553
            )
554
        )
555

556
        # The line from (first pixel, minimum wavelength) to
557
        # (last pixel, maximum wavelength), and the two lines defining the
558
        # tolerance region.
559
        fig.add_trace(
4 ✔
560
            go.Scatter(
561
                x=x,
562
                y=y_1,
563
                mode="lines",
564
                name="Linear Fit",
565
                line=dict(color=pio.templates["CN"].layout.colorway[3]),
566
            )
567
        )
568
        fig.add_trace(
4 ✔
569
            go.Scatter(
570
                x=x,
571
                y=y_2,
572
                mode="lines",
573
                name="Tolerance Region",
574
                line=dict(
575
                    color=pio.templates["CN"].layout.colorway[3],
576
                    dash="dashdot",
577
                ),
578
            )
579
        )
580
        fig.add_trace(
4 ✔
581
            go.Scatter(
582
                x=x,
583
                y=y_3,
584
                showlegend=False,
585
                mode="lines",
586
                line=dict(
587
                    color=pio.templates["CN"].layout.colorway[3],
588
                    dash="dashdot",
589
                ),
590
            )
591
        )
592

593
        if fit_coeff is not None:
4 ✔
594
            fig.add_trace(
×
595
                go.Scatter(
596
                    x=calibrator.peaks,
597
                    y=calibrator.polyval(calibrator.peaks, fit_coeff),
598
                    mode="markers",
599
                    name="Solution",
600
                    marker=dict(color=pio.templates["CN"].layout.colorway[4]),
601
                )
602
            )
603

604
        # Layout, Title, Grid config
605
        fig.update_layout(
4 ✔
606
            autosize=True,
607
            yaxis=dict(
608
                title="Pixel",
609
                range=[
610
                    calibrator.min_wavelength - calibrator.range_tolerance * 1.1,
611
                    calibrator.max_wavelength + calibrator.range_tolerance * 1.1,
612
                ],
613
                showgrid=True,
614
            ),
615
            xaxis=dict(
616
                title="Wavelength / A",
617
                zeroline=False,
618
                range=[0.0, calibrator.pixel_list.max()],
619
                showgrid=True,
620
            ),
621
            hovermode="closest",
622
            showlegend=True,
623
            height=800,
624
            width=1000,
625
        )
626

627
        if save_fig:
4 ✔
628
            fig_type = fig_type.split("+")
4 ✔
629

630
            if filename is None:
4 ✔
631
                filename_output = "rascal_hough_search_space"
×
632

633
            else:
634
                filename_output = filename
4 ✔
635

636
            for t in fig_type:
4 ✔
637
                if t == "iframe":
4 ✔
638
                    pio.write_html(fig, filename_output + "." + t)
×
639

640
                elif t in ["jpg", "png", "svg", "pdf"]:
4 ✔
641
                    pio.write_image(fig, filename_output + "." + t)
4 ✔
642

643
        if display:
4 ✔
644
            if renderer == "default":
×
645
                fig.show()
×
646

647
            else:
648
                fig.show(renderer)
×
649

650
        if return_jsonstring:
4 ✔
651
            return fig.to_json()
×
652

653

654
def plot_fit(
4 ✔
655
    calibrator,
656
    fit_coeff,
657
    spectrum=None,
658
    tolerance=5.0,
659
    plot_atlas=True,
660
    log_spectrum=False,
661
    save_fig=False,
662
    fig_type="png",
663
    filename=None,
664
    return_jsonstring=False,
665
    renderer="default",
666
    display=True,
667
):
668
    """
669
    Plots of the wavelength calibrated arc spectrum, the residual and the
670
    pixel-to-wavelength solution.
671

672
    Parameters
673
    ----------
674
    fit_coeff: 1D numpy array or list
675
        Best fit polynomail fit_coefficients
676
    spectrum: 1D numpy array (N)
677
        Array of length N pixels
678
    tolerance: float (default: 5)
679
        Absolute difference between model and fitted wavelengths in unit
680
        of angstrom.
681
    plot_atlas: boolean (default: True)
682
        Display all the relavent lines available in the atlas library.
683
    log_spectrum: boolean (default: False)
684
        Display the arc in log-space if set to True.
685
    save_fig: boolean (default: False)
686
        Save an image if set to True. matplotlib uses the pyplot.save_fig()
687
        while the plotly uses the pio.write_html() or pio.write_image().
688
        The support format types should be provided in fig_type.
689
    fig_type: string (default: 'png')
690
        Image type to be saved, choose from:
691
        jpg, png, svg, pdf and iframe. Delimiter is '+'.
692
    filename: string (default: None)
693
        Provide a filename or full path. If the extension is not provided
694
        it is defaulted to png.
695
    return_jsonstring: boolean (default: False)
696
        Set to True to return json strings if using plotly as the plotting
697
        library.
698
    renderer: string (default: 'default')
699
        Indicate the Plotly renderer. Nothing gets displayed if
700
        return_jsonstring is set to True.
701
    display: boolean (Default: False)
702
        Set to True to display disgnostic plot.
703

704
    Returns
705
    -------
706
    Return json strings if using plotly as the plotting library and json
707
    is True.
708

709
    """
710

711
    if spectrum is None:
4 ✔
712
        try:
4 ✔
713
            spectrum = calibrator.spectrum
4 ✔
714

715
        except Exception as e:
×
716
            calibrator.logger.error(e)
×
NEW
717
            calibrator.logger.error(
×
718
                "Spectrum is not provided, it cannot be plotted."
719
            )
720

721
    if spectrum is not None:
4 ✔
722
        if log_spectrum:
4 ✔
723
            spectrum[spectrum < 0] = 1e-100
4 ✔
724
            spectrum = np.log10(spectrum)
4 ✔
725
            vline_max = np.nanmax(spectrum) * 2.0
4 ✔
726
            text_box_pos = 1.2 * max(spectrum)
4 ✔
727

728
        else:
729
            vline_max = np.nanmax(spectrum) * 1.2
4 ✔
730
            text_box_pos = 0.8 * max(spectrum)
4 ✔
731

732
    else:
733
        vline_max = 1.0
4 ✔
734
        text_box_pos = 0.5
4 ✔
735

736
    wave = calibrator.polyval(calibrator.pixel_list, fit_coeff)
4 ✔
737

738
    if calibrator.plot_with_matplotlib:
4 ✔
739
        _import_matplotlib()
4 ✔
740

741
        fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True, gridspec_kw={"hspace": 0.0}, figsize=(15, 9))
4 ✔
742
        fig.tight_layout()
4 ✔
743

744
        # Plot fitted spectrum
745
        if spectrum is not None:
4 ✔
746
            ax1.plot(wave, spectrum, label="Arc Spectrum")
4 ✔
747
            ax1.vlines(
4 ✔
748
                calibrator.polyval(calibrator.peaks, fit_coeff),
749
                np.array(spectrum)[calibrator.pix_to_rawpix(calibrator.peaks).astype("int")],
750
                vline_max,
751
                linestyles="dashed",
752
                colors="C1",
753
                label="Detected Peaks",
754
            )
755

756
        # Plot the atlas
757
        if plot_atlas:
4 ✔
758
            # spec = SyntheticSpectrum(
759
            #    fit, model_type='poly', degree=len(fit)-1)
760
            # x_locs = spec.get_pixels(calibrator.atlas)
761
            ax1.vlines(
4 ✔
762
                calibrator.atlas.get_lines(),
763
                0,
764
                vline_max,
765
                colors="C2",
766
                label="Given Lines",
767
            )
768

769
        fitted_peaks = []
4 ✔
770
        fitted_diff = []
4 ✔
771
        all_diff = []
4 ✔
772

773
        first_one = True
4 ✔
774
        for p in calibrator.peaks:
4 ✔
775
            x = calibrator.polyval(p, fit_coeff)
4 ✔
776
            diff = calibrator.atlas.get_lines() - x
4 ✔
777
            idx = np.argmin(np.abs(diff))
4 ✔
778
            all_diff.append(diff[idx])
4 ✔
779

780
            calibrator.logger.info("Peak at: {} A".format(x))
4 ✔
781

782
            fitted_peaks.append(p)
4 ✔
783
            fitted_diff.append(diff[idx])
4 ✔
784
            calibrator.logger.info("- matched to {} A".format(calibrator.atlas.get_lines()[idx]))
4 ✔
785

786
            if spectrum is not None:
4 ✔
787
                if first_one:
4 ✔
788
                    ax1.vlines(
4 ✔
789
                        calibrator.polyval(p, fit_coeff),
790
                        spectrum[calibrator.pix_to_rawpix(p).astype("int")],
791
                        vline_max,
792
                        colors="C1",
793
                        label="Fitted Peaks",
794
                    )
795
                    first_one = False
4 ✔
796

797
                else:
798
                    ax1.vlines(
4 ✔
799
                        calibrator.polyval(p, fit_coeff),
800
                        spectrum[calibrator.pix_to_rawpix(p).astype("int")],
801
                        vline_max,
802
                        colors="C1",
803
                    )
804

805
            ax1.text(
4 ✔
806
                x - 3,
807
                text_box_pos,
808
                s="{}:{:1.2f}".format(
809
                    calibrator.atlas.get_elements()[idx],
810
                    calibrator.atlas.get_lines()[idx],
811
                ),
812
                rotation=90,
813
                bbox=dict(facecolor="white", alpha=1),
814
            )
815

816
        rms = np.sqrt(np.mean(np.array(fitted_diff) ** 2.0))
4 ✔
817

818
        ax1.grid(linestyle=":")
4 ✔
819
        ax1.set_ylabel("Electron Count / e-")
4 ✔
820

821
        if spectrum is not None:
4 ✔
822
            if log_spectrum:
4 ✔
823
                ax1.set_ylim(0, vline_max)
4 ✔
824

825
            else:
826
                ax1.set_ylim(np.nanmin(spectrum), vline_max)
4 ✔
827

828
        ax1.legend(loc="center right")
4 ✔
829

830
        # Plot the residuals
831
        ax2.scatter(
4 ✔
832
            calibrator.polyval(fitted_peaks, fit_coeff),
833
            fitted_diff,
834
            marker="+",
835
            color="C1",
836
        )
837
        ax2.hlines(0, wave.min(), wave.max(), linestyles="dashed")
4 ✔
838
        ax2.hlines(
4 ✔
839
            rms,
840
            wave.min(),
841
            wave.max(),
842
            linestyles="dashed",
843
            color="k",
844
            label="RMS",
845
        )
846
        ax2.hlines(-rms, wave.min(), wave.max(), linestyles="dashed", color="k")
4 ✔
847
        ax2.grid(linestyle=":")
4 ✔
848
        ax2.set_ylabel("Residual / A")
4 ✔
849
        ax2.legend()
4 ✔
850
        """
4 ✔
851
        ax2.text(
852
            min(wave) + np.ptp(wave) * 0.05,
853
            max(spectrum),
854
            'RMS =' + str(rms)[:6]
855
            )
856
        """
857

858
        # Plot the polynomial
859
        ax3.scatter(
4 ✔
860
            calibrator.polyval(fitted_peaks, fit_coeff),
861
            fitted_peaks,
862
            marker="+",
863
            color="C1",
864
            label="Fitted Peaks",
865
        )
866
        ax3.plot(wave, calibrator.pixel_list, color="C2", label="Solution")
4 ✔
867
        ax3.grid(linestyle=":")
4 ✔
868
        ax3.set_xlabel("Wavelength / A")
4 ✔
869
        ax3.set_ylabel("Pixel")
4 ✔
870
        ax3.legend(loc="lower right")
4 ✔
871
        w_min = calibrator.polyval(min(fitted_peaks), fit_coeff)
4 ✔
872
        w_max = calibrator.polyval(max(fitted_peaks), fit_coeff)
4 ✔
873
        ax3.set_xlim(w_min * 0.95, w_max * 1.05)
4 ✔
874

875
        plt.tight_layout()
4 ✔
876

877
        if save_fig:
4 ✔
878
            fig_type = fig_type.split("+")
4 ✔
879

880
            if filename is None:
4 ✔
881
                filename_output = "rascal_solution"
×
882

883
            else:
884
                filename_output = filename
4 ✔
885

886
            for t in fig_type:
4 ✔
887
                if t in ["jpg", "png", "svg", "pdf"]:
4 ✔
888
                    plt.savefig(filename_output + "." + t, format=t)
4 ✔
889

890
        if display:
4 ✔
891
            plt.show()
4 ✔
892

893
        return fig
4 ✔
894

895
    elif calibrator.plot_with_plotly:
4 ✔
896
        _import_plotly()
4 ✔
897

898
        fig = go.Figure()
4 ✔
899

900
        # Top plot - arc spectrum and matched peaks
901
        if spectrum is not None:
4 ✔
902
            fig.add_trace(
4 ✔
903
                go.Scatter(
904
                    x=wave,
905
                    y=spectrum,
906
                    mode="lines",
907
                    yaxis="y3",
908
                    name="Arc Spectrum",
909
                )
910
            )
911

912
            spec_max = np.nanmax(spectrum) * 1.05
4 ✔
913

914
        else:
915
            spec_max = vline_max
×
916

917
        fitted_peaks = []
4 ✔
918
        fitted_peaks_adu = []
4 ✔
919
        fitted_diff = []
4 ✔
920
        all_diff = []
4 ✔
921

922
        for p in calibrator.peaks:
4 ✔
923
            x = calibrator.polyval(p, fit_coeff)
4 ✔
924

925
            # Add vlines
926
            fig.add_shape(
4 ✔
927
                type="line",
928
                xref="x",
929
                yref="y3",
930
                x0=x,
931
                y0=0,
932
                x1=x,
933
                y1=spec_max,
934
                line=dict(color=pio.templates["CN"].layout.colorway[1], width=1),
935
            )
936

937
            diff = calibrator.atlas.get_lines() - x
4 ✔
938
            idx = np.argmin(np.abs(diff))
4 ✔
939
            all_diff.append(diff[idx])
4 ✔
940

941
            calibrator.logger.info("Peak at: {} A".format(x))
4 ✔
942

943
            if np.abs(diff[idx]) < tolerance:
4 ✔
944
                fitted_peaks.append(p)
4 ✔
945
                if spectrum is not None:
4 ✔
946
                    fitted_peaks_adu.append(spectrum[int(calibrator.pix_to_rawpix(p))])
4 ✔
947
                fitted_diff.append(diff[idx])
4 ✔
948
                calibrator.logger.info(
4 ✔
949
                    "- matched to {} A".format(
950
                        calibrator.atlas.get_lines()[idx]
951
                    )
952
                )
953

954
            fig.add_annotation(
4 ✔
955
                x=x,
956
                y=text_box_pos,
957
                xref="x",
958
                yref="y3",
959
                text="{}:{:1.2f}".format(
960
                    calibrator.atlas.get_elements()[idx],
961
                    calibrator.atlas.get_lines()[idx],
962
                ),
963
                textangle=-90,
964
                showarrow=False,
965
                bordercolor="#000000",
966
                borderwidth=1,
967
                bgcolor="#FFFFFF",
968
            )
969

970
        x_fitted = calibrator.polyval(fitted_peaks, fit_coeff)
4 ✔
971

972
        fig.add_trace(
4 ✔
973
            go.Scatter(
974
                x=x_fitted,
975
                y=fitted_peaks_adu,
976
                mode="markers",
977
                marker=dict(color=pio.templates["CN"].layout.colorway[1]),
978
                yaxis="y3",
979
                showlegend=False,
980
            )
981
        )
982

983
        # Middle plot - Residual plot
984
        rms = np.sqrt(np.mean(np.array(fitted_diff) ** 2.0))
4 ✔
985
        fig.add_trace(
4 ✔
986
            go.Scatter(
987
                x=x_fitted,
988
                y=fitted_diff,
989
                mode="markers",
990
                marker=dict(color=pio.templates["CN"].layout.colorway[1]),
991
                yaxis="y2",
992
                showlegend=False,
993
            )
994
        )
995
        fig.add_trace(
4 ✔
996
            go.Scatter(
997
                x=[wave.min(), wave.max()],
998
                y=[0, 0],
999
                mode="lines",
1000
                line=dict(color=pio.templates["CN"].layout.colorway[0], dash="dash"),
1001
                yaxis="y2",
1002
                showlegend=False,
1003
            )
1004
        )
1005
        fig.add_trace(
4 ✔
1006
            go.Scatter(
1007
                x=[wave.min(), wave.max()],
1008
                y=[rms, rms],
1009
                mode="lines",
1010
                line=dict(color="black", dash="dash"),
1011
                yaxis="y2",
1012
                showlegend=False,
1013
            )
1014
        )
1015
        fig.add_trace(
4 ✔
1016
            go.Scatter(
1017
                x=[wave.min(), wave.max()],
1018
                y=[-rms, -rms],
1019
                mode="lines",
1020
                line=dict(color="black", dash="dash"),
1021
                yaxis="y2",
1022
                name="RMS",
1023
            )
1024
        )
1025

1026
        # Bottom plot - Polynomial fit for Pixel to Wavelength
1027
        fig.add_trace(
4 ✔
1028
            go.Scatter(
1029
                x=x_fitted,
1030
                y=fitted_peaks,
1031
                mode="markers",
1032
                marker=dict(color=pio.templates["CN"].layout.colorway[1]),
1033
                yaxis="y1",
1034
                name="Fitted Peaks",
1035
            )
1036
        )
1037
        fig.add_trace(
4 ✔
1038
            go.Scatter(
1039
                x=wave,
1040
                y=calibrator.pixel_list,
1041
                mode="lines",
1042
                line=dict(color=pio.templates["CN"].layout.colorway[2]),
1043
                yaxis="y1",
1044
                name="Solution",
1045
            )
1046
        )
1047

1048
        # Layout, Title, Grid config
1049
        if spectrum is not None:
4 ✔
1050
            if log_spectrum:
4 ✔
1051
                fig.update_layout(
×
1052
                    yaxis3=dict(
1053
                        title="Electron Count / e-",
1054
                        range=[
1055
                            np.log10(np.percentile(spectrum, 15)),
1056
                            np.log10(spec_max),
1057
                        ],
1058
                        domain=[0.67, 1.0],
1059
                        showgrid=True,
1060
                        type="log",
1061
                    )
1062
                )
1063

1064
            else:
1065
                fig.update_layout(
4 ✔
1066
                    yaxis3=dict(
1067
                        title="Electron Count / e-",
1068
                        range=[np.percentile(spectrum, 15), spec_max],
1069
                        domain=[0.67, 1.0],
1070
                        showgrid=True,
1071
                    )
1072
                )
1073

1074
        fig.update_layout(
4 ✔
1075
            autosize=True,
1076
            yaxis2=dict(
1077
                title="Residual / A",
1078
                range=[min(fitted_diff), max(fitted_diff)],
1079
                domain=[0.33, 0.66],
1080
                showgrid=True,
1081
            ),
1082
            yaxis=dict(
1083
                title="Pixel",
1084
                range=[0.0, max(calibrator.pixel_list)],
1085
                domain=[0.0, 0.32],
1086
                showgrid=True,
1087
            ),
1088
            xaxis=dict(
1089
                title="Wavelength / A",
1090
                zeroline=False,
1091
                range=[
1092
                    calibrator.polyval(min(fitted_peaks), fit_coeff) * 0.95,
1093
                    calibrator.polyval(max(fitted_peaks), fit_coeff) * 1.05,
1094
                ],
1095
                showgrid=True,
1096
            ),
1097
            hovermode="closest",
1098
            showlegend=True,
1099
            height=800,
1100
            width=1000,
1101
        )
1102

1103
        if save_fig:
4 ✔
1104
            fig_type = fig_type.split("+")
4 ✔
1105

1106
            if filename is None:
4 ✔
1107
                filename_output = "rascal_solution"
×
1108

1109
            else:
1110
                filename_output = filename
4 ✔
1111

1112
            for t in fig_type:
4 ✔
1113
                if t == "iframe":
4 ✔
1114
                    pio.write_html(fig, filename_output + "." + t)
×
1115

1116
                elif t in ["jpg", "png", "svg", "pdf"]:
4 ✔
1117
                    pio.write_image(fig, filename_output + "." + t)
4 ✔
1118

1119
        if display:
4 ✔
1120
            if renderer == "default":
×
1121
                fig.show()
×
1122

1123
            else:
1124
                fig.show(renderer)
×
1125

1126
        if return_jsonstring:
4 ✔
1127
            return fig.to_json()
×
1128

1129
    else:
1130
        assert calibrator.matplotlib_imported, "matplotlib package not available. " + "Plot cannot be generated."
×
1131
        assert calibrator.plotly_imported, "plotly package is not available. " + "Plot cannot be generated."
×
1132

1133

1134
def plot_arc(
4 ✔
1135
    calibrator,
1136
    pixel_list=None,
1137
    log_spectrum=False,
1138
    save_fig=False,
1139
    fig_type="png",
1140
    filename=None,
1141
    return_jsonstring=False,
1142
    renderer="default",
1143
    display=True,
1144
):
1145
    """
1146
    Plots the 1D spectrum of the extracted arc.
1147

1148
    parameters
1149
    ----------
1150
    pixel_list: array (default: None)
1151
        pixel value of the of the spectrum, this is only needed if the
1152
        spectrum spans multiple detector arrays.
1153
    log_spectrum: boolean (default: False)
1154
        Set to true to display the wavelength calibrated arc spectrum in
1155
        logarithmic space.
1156
    save_fig: boolean (default: False)
1157
        Save an image if set to True. matplotlib uses the pyplot.save_fig()
1158
        while the plotly uses the pio.write_html() or pio.write_image().
1159
        The support format types should be provided in fig_type.
1160
    fig_type: string (default: 'png')
1161
        Image type to be saved, choose from:
1162
        jpg, png, svg, pdf and iframe. Delimiter is '+'.
1163
    filename: string (default: None)
1164
        Provide a filename or full path. If the extension is not provided
1165
        it is defaulted to png.
1166
    return_jsonstring: boolean (default: False)
1167
        Set to True to return json strings if using plotly as the plotting
1168
        library.
1169
    renderer: string (default: 'default')
1170
        Indicate the Plotly renderer. Nothing gets displayed if
1171
        return_jsonstring is set to True.
1172

1173
    display: boolean (Default: False)
1174
        Set to True to display disgnostic plot.
1175

1176
    Returns
1177
    -------
1178
    Return json strings if using plotly as the plotting library and json
1179
    is True.
1180

1181
    """
1182

1183
    if pixel_list is None:
4 ✔
1184
        pixel_list = np.arange(len(calibrator.spectrum))
4 ✔
1185

1186
    if calibrator.plot_with_matplotlib:
4 ✔
1187
        _import_matplotlib()
4 ✔
1188

1189
        fig = plt.figure(figsize=(18, 5))
4 ✔
1190

1191
        if calibrator.spectrum is not None:
4 ✔
1192
            if log_spectrum:
4 ✔
1193
                plt.plot(
4 ✔
1194
                    pixel_list,
1195
                    np.log10(calibrator.spectrum / calibrator.spectrum.max()),
1196
                    label="Arc Spectrum",
1197
                )
1198
                plt.vlines(calibrator.peaks, -2, 0, label="Detected Peaks", color="C1")
4 ✔
1199
                plt.ylabel("log(Normalised Count)")
4 ✔
1200
                plt.ylim(-2, 0)
4 ✔
1201
            else:
1202
                plt.plot(
4 ✔
1203
                    pixel_list,
1204
                    calibrator.spectrum / calibrator.spectrum.max(),
1205
                    label="Arc Spectrum",
1206
                )
1207
                plt.ylabel("Normalised Count")
4 ✔
1208
                plt.vlines(
4 ✔
1209
                    calibrator.peaks,
1210
                    0,
1211
                    1.05,
1212
                    label="Detected Peaks",
1213
                    color="C1",
1214
                )
1215
            plt.title("Number of pixels: " + str(calibrator.spectrum.shape[0]))
4 ✔
1216
            plt.xlim(0, calibrator.spectrum.shape[0])
4 ✔
1217
            plt.legend()
4 ✔
1218

1219
        else:
1220
            plt.xlim(0, max(calibrator.peaks))
×
1221

1222
        plt.xlabel("Pixel (Spectral Direction)")
4 ✔
1223
        plt.grid()
4 ✔
1224
        plt.tight_layout()
4 ✔
1225

1226
        if save_fig:
4 ✔
1227
            fig_type = fig_type.split("+")
4 ✔
1228

1229
            if filename is None:
4 ✔
1230
                filename_output = "rascal_arc"
4 ✔
1231

1232
            else:
1233
                filename_output = filename
4 ✔
1234

1235
            for t in fig_type:
4 ✔
1236
                if t in ["jpg", "png", "svg", "pdf"]:
4 ✔
1237
                    plt.savefig(filename_output + "." + t, format=t)
4 ✔
1238

1239
        if display:
4 ✔
1240
            plt.show()
×
1241

1242
        return fig
4 ✔
1243

1244
    if calibrator.plot_with_plotly:
4 ✔
1245
        _import_plotly()
4 ✔
1246

1247
        fig = go.Figure()
4 ✔
1248

1249
        if log_spectrum:
4 ✔
1250
            # Plot all-pairs
1251
            fig.add_trace(
4 ✔
1252
                go.Scatter(
1253
                    x=list(pixel_list),
1254
                    y=list(np.log10(calibrator.spectrum / calibrator.spectrum.max())),
1255
                    mode="lines",
1256
                    name="Arc",
1257
                )
1258
            )
1259
            xmin = min(np.log10(calibrator.spectrum / calibrator.spectrum.max()))
4 ✔
1260
            xmax = max(np.log10(calibrator.spectrum / calibrator.spectrum.max()))
4 ✔
1261

1262
        else:
1263
            # Plot all-pairs
1264
            fig.add_trace(
4 ✔
1265
                go.Scatter(
1266
                    x=list(pixel_list),
1267
                    y=list(calibrator.spectrum / calibrator.spectrum.max()),
1268
                    mode="lines",
1269
                    name="Arc",
1270
                )
1271
            )
1272
            xmin = min(calibrator.spectrum / calibrator.spectrum.max())
4 ✔
1273
            xmax = max(calibrator.spectrum / calibrator.spectrum.max())
4 ✔
1274

1275
        # Add vlines
1276
        for i in calibrator.peaks:
4 ✔
1277
            fig.add_shape(
4 ✔
1278
                type="line",
1279
                xref="x",
1280
                yref="y",
1281
                x0=i,
1282
                y0=0,
1283
                x1=i,
1284
                y1=1.05,
1285
                line=dict(color=pio.templates["CN"].layout.colorway[1], width=1),
1286
            )
1287

1288
        fig.update_layout(
4 ✔
1289
            autosize=True,
1290
            yaxis=dict(title="Normalised Count", range=[xmin, xmax], showgrid=True),
1291
            xaxis=dict(
1292
                title="Pixel",
1293
                zeroline=False,
1294
                range=[0.0, len(calibrator.spectrum)],
1295
                showgrid=True,
1296
            ),
1297
            hovermode="closest",
1298
            showlegend=True,
1299
            height=800,
1300
            width=1000,
1301
        )
1302

1303
        fig.update_xaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
4 ✔
1304

1305
        fig.update_yaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
4 ✔
1306

1307
        if save_fig:
4 ✔
1308
            fig_type = fig_type.split("+")
4 ✔
1309

1310
            if filename is None:
4 ✔
1311
                filename_output = "rascal_arc"
4 ✔
1312

1313
            else:
1314
                filename_output = filename
4 ✔
1315

1316
            for t in fig_type:
4 ✔
1317
                if t == "iframe":
4 ✔
1318
                    pio.write_html(fig, filename_output + "." + t)
×
1319

1320
                elif t in ["jpg", "png", "svg", "pdf"]:
4 ✔
1321
                    pio.write_image(fig, filename_output + "." + t)
4 ✔
1322

1323
        if display:
4 ✔
1324
            if renderer == "default":
×
1325
                fig.show()
×
1326

1327
            else:
1328
                fig.show(renderer)
×
1329

1330
        if return_jsonstring:
4 ✔
1331
            return fig.to_json()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc