Skip to content

Sources¤

BaseSource

dLux.sources.BaseSource ¤

Bases: Base

Abstract base class for source models.

Concrete source classes define normalisation behavior and the optical modelling interface via normalise(...) and model(...).

UML

UML

Source code in dLux/sources.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class BaseSource(zdx.Base):
    """
    Abstract base class for source models.

    Concrete source classes define normalisation behavior and the optical
    modelling interface via `normalise(...)` and `model(...)`.

    ??? abstract "UML"
        ![UML](../../assets/uml/BaseSource.png)
    """

    def __init_subclass__(cls, **kwargs):
        """Inherit docstrings from parent classes for model method."""
        super().__init_subclass__(**kwargs)
        dlu.helpers.inherit_docstrings(cls, ["model"])

    @abstractmethod
    def normalise(self: BaseSource) -> BaseSource:  # pragma: no cover
        pass

    @abstractmethod
    def model(
        self: BaseSource,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:  # pragma: no cover
        """
        Models the source object through the provided optics.

        Parameters
        ----------
        optics : OpticalSystem
            The optics through which to model the source object.
        return_wf : bool = False
            Should the Wavefront object be returned instead of the PSF array?
        return_psf : bool = False
            Should the PSF object be returned instead of the PSF array?

        Returns
        -------
        result : Array | Wavefront | PSF
            If `return_wf` is False and `return_psf` is False, returns the PSF array.
            If `return_wf` is True and `return_psf` is False, returns the Wavefront
                object.
            If `return_wf` is False and `return_psf` is True, returns the PSF object.
        """

__init_subclass__(**kwargs) ¤

Inherit docstrings from parent classes for model method.

Source code in dLux/sources.py
143
144
145
146
def __init_subclass__(cls, **kwargs):
    """Inherit docstrings from parent classes for model method."""
    super().__init_subclass__(**kwargs)
    dlu.helpers.inherit_docstrings(cls, ["model"])

model(optics, return_wf=False, return_psf=False) abstractmethod ¤

Models the source object through the provided optics.

Parameters:

Name Type Description Default
optics OpticalSystem

The optics through which to model the source object.

required
return_wf bool = False

Should the Wavefront object be returned instead of the PSF array?

False
return_psf bool = False

Should the PSF object be returned instead of the PSF array?

False

Returns:

Name Type Description
result Array | Wavefront | PSF

If return_wf is False and return_psf is False, returns the PSF array. If return_wf is True and return_psf is False, returns the Wavefront object. If return_wf is False and return_psf is True, returns the PSF object.

Source code in dLux/sources.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
@abstractmethod
def model(
    self: BaseSource,
    optics: BaseOpticalSystem,
    return_wf: bool = False,
    return_psf: bool = False,
) -> Array | Wavefront | PSF:  # pragma: no cover
    """
    Models the source object through the provided optics.

    Parameters
    ----------
    optics : OpticalSystem
        The optics through which to model the source object.
    return_wf : bool = False
        Should the Wavefront object be returned instead of the PSF array?
    return_psf : bool = False
        Should the PSF object be returned instead of the PSF array?

    Returns
    -------
    result : Array | Wavefront | PSF
        If `return_wf` is False and `return_psf` is False, returns the PSF array.
        If `return_wf` is True and `return_psf` is False, returns the Wavefront
            object.
        If `return_wf` is False and `return_psf` is True, returns the PSF object.
    """
PointSource

dLux.sources.PointSource ¤

Bases: Source

A simple point source with a spectrum, position and flux.

UML

UML

Attributes:

Name Type Description
position (Array, radians)

The (x, y) on-sky position of this object.

flux (float, photons)

The flux of the object.

spectrum Spectrum

The spectrum of this object, represented by a Spectrum object.

Source code in dLux/sources.py
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
class PointSource(Source):
    """
    A simple point source with a spectrum, position and flux.

    ??? abstract "UML"
        ![UML](../../assets/uml/PointSource.png)

    Attributes
    ----------
    position : Array, radians
        The (x, y) on-sky position of this object.
    flux : float, photons
        The flux of the object.
    spectrum : Spectrum
        The spectrum of this object, represented by a Spectrum object.
    """

    position: Array
    flux: float

    def __init__(
        self: PointSource,
        wavelengths: Array = None,
        position: Array = np.zeros(2),
        flux: float = 1.0,
        weights: Array = None,
        spectrum: spectra.BaseSpectrum = None,
    ):
        """
        Parameters
        ----------
        wavelengths : Array, metres = None
            The array of wavelengths at which the spectrum is defined. This input is
            ignored if a Spectrum object is provided.
        position : Array, radians = np.zeros(2)
            The (x, y) on-sky position of this object.
        flux : float, photons = 1.
            The flux of the object.
        spectrum : Spectrum = None
            The spectrum of this object, represented by a Spectrum object.
        """
        # Position and Flux
        self.position = _as_position_2d(position)
        self.flux = float(flux)

        super().__init__(wavelengths=wavelengths, weights=weights, spectrum=spectrum)

    def model(
        self: PointSource,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        self = self.normalise()
        weights = self.weights * self.flux
        return optics.propagate(
            self.wavelengths, self.position, weights, return_wf, return_psf
        )

__init__(wavelengths=None, position=np.zeros(2), flux=1.0, weights=None, spectrum=None) ¤

Parameters:

Name Type Description Default
wavelengths Array, metres = None

The array of wavelengths at which the spectrum is defined. This input is ignored if a Spectrum object is provided.

None
position Array, radians = np.zeros(2)

The (x, y) on-sky position of this object.

zeros(2)
flux float, photons = 1.

The flux of the object.

1.0
spectrum Spectrum = None

The spectrum of this object, represented by a Spectrum object.

None
Source code in dLux/sources.py
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
def __init__(
    self: PointSource,
    wavelengths: Array = None,
    position: Array = np.zeros(2),
    flux: float = 1.0,
    weights: Array = None,
    spectrum: spectra.BaseSpectrum = None,
):
    """
    Parameters
    ----------
    wavelengths : Array, metres = None
        The array of wavelengths at which the spectrum is defined. This input is
        ignored if a Spectrum object is provided.
    position : Array, radians = np.zeros(2)
        The (x, y) on-sky position of this object.
    flux : float, photons = 1.
        The flux of the object.
    spectrum : Spectrum = None
        The spectrum of this object, represented by a Spectrum object.
    """
    # Position and Flux
    self.position = _as_position_2d(position)
    self.flux = float(flux)

    super().__init__(wavelengths=wavelengths, weights=weights, spectrum=spectrum)
PointSources

dLux.sources.PointSources ¤

Bases: Source

A set of point sources with the same spectrum, but different positions and fluxes.

UML

UML

Attributes:

Name Type Description
position (Array, radians)

The ((x0, y0), (x1, y1), ...) on-sky positions of these sources.

flux (Array, photons)

The fluxes of the sources.

spectrum Spectrum

The spectrum of this object, represented by a Spectrum object.

Source code in dLux/sources.py
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
class PointSources(Source):
    """
    A set of point sources with the same spectrum, but different positions and fluxes.

    ??? abstract "UML"
        ![UML](../../assets/uml/PointSources.png)

    Attributes
    ----------
    position : Array, radians
        The ((x0, y0), (x1, y1), ...) on-sky positions of these sources.
    flux : Array, photons
        The fluxes of the sources.
    spectrum : Spectrum
        The spectrum of this object, represented by a Spectrum object.
    """

    position: Array
    flux: Array

    def __init__(
        self: PointSources,
        wavelengths: Array = None,
        position: Array = np.zeros((1, 2)),
        flux: Array = None,
        weights: Array = None,
        spectrum: spectra.BaseSpectrum = None,
    ):
        """
        Parameters
        ----------
        wavelengths : Array, metres
            The array of wavelengths at which the spectrum is defined.
        position : Array, radians = np.zeros((1, 2))
            The (x, y) on-sky position of this object.
        flux : Array, photons = None
            The flux of the object.
        weights : Array = None
            The spectral weights of the object.
        spectrum : Spectrum = None
            The spectrum of this object, represented by a Spectrum object.
        """
        super().__init__(spectrum=spectrum, wavelengths=wavelengths, weights=weights)

        # More complex parameter checks here because of extra dims
        self.position = np.asarray(position, dtype=float)
        if self.position.ndim != 2:
            raise ValueError("position must be a 2d array.")

        if flux is None:
            self.flux = np.ones(len(self.position))
        else:
            self.flux = np.asarray(flux, dtype=float)

            if self.flux.ndim != 1:
                raise ValueError("flux must be a 1d array.")

            if len(self.flux) != len(self.position):
                raise ValueError(
                    "Length of flux must be equal to length of " "positions."
                )

    def model(
        self: PointSources,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        self = self.normalise()
        weights = self.weights[None, :] * self.flux[:, None]
        prop_fn = lambda position, weight: optics.propagate(
            self.wavelengths, position, weight, return_wf=True
        )
        wfs = eqx.filter_vmap(prop_fn)(self.position, weights)

        if return_wf:
            return wfs
        if return_psf:
            return PSF(wfs.psf.sum((0, 1)), wfs.pixel_scale.mean())
        else:
            return wfs.psf.sum((0, 1))

__init__(wavelengths=None, position=np.zeros((1, 2)), flux=None, weights=None, spectrum=None) ¤

Parameters:

Name Type Description Default
wavelengths (Array, metres)

The array of wavelengths at which the spectrum is defined.

None
position Array, radians = np.zeros((1, 2))

The (x, y) on-sky position of this object.

zeros((1, 2))
flux Array, photons = None

The flux of the object.

None
weights Array = None

The spectral weights of the object.

None
spectrum Spectrum = None

The spectrum of this object, represented by a Spectrum object.

None
Source code in dLux/sources.py
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
def __init__(
    self: PointSources,
    wavelengths: Array = None,
    position: Array = np.zeros((1, 2)),
    flux: Array = None,
    weights: Array = None,
    spectrum: spectra.BaseSpectrum = None,
):
    """
    Parameters
    ----------
    wavelengths : Array, metres
        The array of wavelengths at which the spectrum is defined.
    position : Array, radians = np.zeros((1, 2))
        The (x, y) on-sky position of this object.
    flux : Array, photons = None
        The flux of the object.
    weights : Array = None
        The spectral weights of the object.
    spectrum : Spectrum = None
        The spectrum of this object, represented by a Spectrum object.
    """
    super().__init__(spectrum=spectrum, wavelengths=wavelengths, weights=weights)

    # More complex parameter checks here because of extra dims
    self.position = np.asarray(position, dtype=float)
    if self.position.ndim != 2:
        raise ValueError("position must be a 2d array.")

    if flux is None:
        self.flux = np.ones(len(self.position))
    else:
        self.flux = np.asarray(flux, dtype=float)

        if self.flux.ndim != 1:
            raise ValueError("flux must be a 1d array.")

        if len(self.flux) != len(self.position):
            raise ValueError(
                "Length of flux must be equal to length of " "positions."
            )
BinarySource

dLux.sources.BinarySource ¤

Bases: Source

A binary source parameterised by the position, flux, separation, position_angle, and contrast between the two sources.

UML

UML

Attributes:

Name Type Description
position (Array, radians)

The mean (x, y) on-sky position of this object.

mean_flux (float, photons)

The mean flux of the sources.

separation (float, radians)

The separation of the two sources in radians.

position_angle (float, radians)

The position angle between the two sources measured clockwise from the vertical axis.

contrast float

The contrast ratio between the two sources.

spectrum Spectrum

The spectrum of this object, represented by a Spectrum object.

Source code in dLux/sources.py
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
class BinarySource(Source):
    """
    A binary source parameterised by the position, flux, separation, position_angle,
    and contrast between the two sources.

    ??? abstract "UML"
        ![UML](../../assets/uml/BinarySource.png)

    Attributes
    ----------
    position : Array, radians
        The mean (x, y) on-sky position of this object.
    mean_flux : float, photons
        The mean flux of the sources.
    separation : float, radians
        The separation of the two sources in radians.
    position_angle : float, radians
        The position angle between the two sources measured clockwise from the
        vertical axis.
    contrast : float
        The contrast ratio between the two sources.
    spectrum : Spectrum
        The spectrum of this object, represented by a Spectrum object.
    """

    position: Array
    mean_flux: float
    separation: float
    position_angle: float
    contrast: float

    def __init__(
        self: BinarySource,
        wavelengths: Array = None,
        position: Array = np.zeros(2),
        mean_flux: float = 1.0,
        separation: float = 0.0,
        position_angle: float = np.pi / 2,
        contrast: float = 1.0,
        spectrum: spectra.BaseSpectrum = None,
        weights: Array = None,
    ):
        """
        Parameters
        ----------
        wavelengths : Array, metres = None
            The array of wavelengths at which the spectrum is defined.
        position : Array, radians = np.zeros(2)
            The (x, y) on-sky position of this object.
        mean_flux : float, photons = 1.
            The mean flux of the sources.
        separation : float, radians = 0.
            The separation of the two sources in radians.
        position_angle : float, radians = np.pi / 2
            The position angle between the two sources measured clockwise from the
            vertical axis.
        contrast : float = 1.
            The contrast ratio between the two sources.
        spectrum : Spectrum = None
            The spectrum of this object, represented by a Spectrum object.
        """
        wavelengths = _as_wavelengths_1d(wavelengths)
        if weights is None:
            n_wavelengths = _infer_n_wavelengths(wavelengths, spectrum)
            weights = np.ones((2, n_wavelengths))

        # Position and Flux
        self.position = _as_position_2d(position)
        self.mean_flux = float(mean_flux)

        # Binary values
        self.separation = float(separation)
        self.position_angle = float(position_angle)
        self.contrast = float(contrast)

        super().__init__(
            wavelengths=wavelengths,
            spectrum=spectrum,
            weights=weights,
        )

    def model(
        self: BinarySource,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        # Normalise and get input values
        self = self.normalise()
        positions = dlu.positions_from_sep(
            self.position, self.separation, self.position_angle
        )
        flux = dlu.fluxes_from_contrast(self.mean_flux, self.contrast)
        weights = self.weights * flux[:, None]

        # Return wf case is simple
        prop_fn = lambda position, weight: optics.propagate(
            self.wavelengths, position, weight, return_wf, return_psf
        )
        output = eqx.filter_vmap(prop_fn)(positions, weights)

        # Return wf is simple case
        if return_wf:
            return output

        # Return PSF case just requires constructing the object
        if return_psf:
            return PSF(output.data.sum(0), output.pixel_scale.mean())

        # Return array is simple
        return output.sum(0)

__init__(wavelengths=None, position=np.zeros(2), mean_flux=1.0, separation=0.0, position_angle=np.pi / 2, contrast=1.0, spectrum=None, weights=None) ¤

Parameters:

Name Type Description Default
wavelengths Array, metres = None

The array of wavelengths at which the spectrum is defined.

None
position Array, radians = np.zeros(2)

The (x, y) on-sky position of this object.

zeros(2)
mean_flux float, photons = 1.

The mean flux of the sources.

1.0
separation float, radians = 0.

The separation of the two sources in radians.

0.0
position_angle float, radians = np.pi / 2

The position angle between the two sources measured clockwise from the vertical axis.

pi / 2
contrast float = 1.

The contrast ratio between the two sources.

1.0
spectrum Spectrum = None

The spectrum of this object, represented by a Spectrum object.

None
Source code in dLux/sources.py
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
def __init__(
    self: BinarySource,
    wavelengths: Array = None,
    position: Array = np.zeros(2),
    mean_flux: float = 1.0,
    separation: float = 0.0,
    position_angle: float = np.pi / 2,
    contrast: float = 1.0,
    spectrum: spectra.BaseSpectrum = None,
    weights: Array = None,
):
    """
    Parameters
    ----------
    wavelengths : Array, metres = None
        The array of wavelengths at which the spectrum is defined.
    position : Array, radians = np.zeros(2)
        The (x, y) on-sky position of this object.
    mean_flux : float, photons = 1.
        The mean flux of the sources.
    separation : float, radians = 0.
        The separation of the two sources in radians.
    position_angle : float, radians = np.pi / 2
        The position angle between the two sources measured clockwise from the
        vertical axis.
    contrast : float = 1.
        The contrast ratio between the two sources.
    spectrum : Spectrum = None
        The spectrum of this object, represented by a Spectrum object.
    """
    wavelengths = _as_wavelengths_1d(wavelengths)
    if weights is None:
        n_wavelengths = _infer_n_wavelengths(wavelengths, spectrum)
        weights = np.ones((2, n_wavelengths))

    # Position and Flux
    self.position = _as_position_2d(position)
    self.mean_flux = float(mean_flux)

    # Binary values
    self.separation = float(separation)
    self.position_angle = float(position_angle)
    self.contrast = float(contrast)

    super().__init__(
        wavelengths=wavelengths,
        spectrum=spectrum,
        weights=weights,
    )
ResolvedSource

dLux.sources.ResolvedSource ¤

Bases: PointSource

A single resolved source with a spectrum, position, flux, and distribution array that represents the resolved component.

UML

UML

Attributes:

Name Type Description
position (Array, radians)

The (x, y) on-sky position of this object.

flux (float, photons)

The flux of the object.

distribution Array

The array of intensities representing the resolved source.

spectrum Spectrum

The spectrum of this object, represented by a Spectrum object.

Source code in dLux/sources.py
414
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
479
480
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
class ResolvedSource(PointSource):
    """
    A single resolved source with a spectrum, position, flux, and distribution array
    that represents the resolved component.

    ??? abstract "UML"
        ![UML](../../assets/uml/ResolvedSource.png)

    Attributes
    ----------
    position : Array, radians
        The (x, y) on-sky position of this object.
    flux : float, photons
        The flux of the object.
    distribution : Array
        The array of intensities representing the resolved source.
    spectrum : Spectrum
        The spectrum of this object, represented by a Spectrum object.
    """

    distribution: Array

    def __init__(
        self: ResolvedSource,
        wavelengths: Array = None,
        position: Array = np.zeros(2),
        flux: float = 1.0,
        distribution: Array = np.ones((3, 3)),
        weights: Array = None,
        spectrum: spectra.BaseSpectrum = None,
    ):
        """
        Parameters
        ----------
        wavelengths : Array, metres
            The array of wavelengths at which the spectrum is defined.
        position : Array, radians = np.zeros(2)
            The (x, y) on-sky position of this object.
        flux : float, photons = 1.
            The flux of the object.
        distribution : Array = np.ones((3, 3))
            The array of intensities representing the resolved source.
        weights : Array = None
            The spectral weights of the object.
        spectrum : Spectrum = None
            The spectrum of this object, represented by a Spectrum object.
        """
        distribution = np.asarray(distribution, dtype=float)
        self.distribution = distribution / distribution.sum()

        if self.distribution.ndim != 2:
            raise ValueError("distribution must be a 2d array.")

        super().__init__(
            position=position,
            flux=flux,
            spectrum=spectrum,
            wavelengths=wavelengths,
            weights=weights,
        )

    def normalise(self: ResolvedSource) -> ResolvedSource:
        """
        Method for returning a new source object with a normalised total
        spectrum and source distribution.

        Returns
        -------
        source : Source
            The source object with the normalised spectrum and distribution.
        """
        spectrum = self.spectrum.normalise()
        distribution_floor = np.maximum(self.distribution, 0.0)
        distribution = distribution_floor / distribution_floor.sum()
        return self.set(["spectrum", "distribution"], [spectrum, distribution])

    def model(
        self: ResolvedSource,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        # Normalise and get parameters
        self = self.normalise()
        weights = self.weights * self.flux

        # Note that we always return a wavefront here so we can convolve each
        # wavelength
        # individually if a chromatic wavefront output is required.
        wf = optics.propagate(self.wavelengths, self.position, weights, return_wf=True)

        # Returning wf is a special case
        if return_wf:
            raise NotImplementedError(
                "Wavefront information cannot be preserved through convolution. "
                "Convolution can only operate on PSFs (incoherent light). "
                "Please use return_wf=False to get the PSF array or return_psf=True "
                "to get a PSF object."
            )

        # Return PSF object
        conv_psf = jsp.signal.convolve(wf.psf.sum(0), self.distribution, mode="same")
        if return_psf:
            return PSF(conv_psf, wf.pixel_scale.mean())

        # Return PSF array
        return conv_psf

__init__(wavelengths=None, position=np.zeros(2), flux=1.0, distribution=np.ones((3, 3)), weights=None, spectrum=None) ¤

Parameters:

Name Type Description Default
wavelengths (Array, metres)

The array of wavelengths at which the spectrum is defined.

None
position Array, radians = np.zeros(2)

The (x, y) on-sky position of this object.

zeros(2)
flux float, photons = 1.

The flux of the object.

1.0
distribution Array = np.ones((3, 3))

The array of intensities representing the resolved source.

ones((3, 3))
weights Array = None

The spectral weights of the object.

None
spectrum Spectrum = None

The spectrum of this object, represented by a Spectrum object.

None
Source code in dLux/sources.py
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
def __init__(
    self: ResolvedSource,
    wavelengths: Array = None,
    position: Array = np.zeros(2),
    flux: float = 1.0,
    distribution: Array = np.ones((3, 3)),
    weights: Array = None,
    spectrum: spectra.BaseSpectrum = None,
):
    """
    Parameters
    ----------
    wavelengths : Array, metres
        The array of wavelengths at which the spectrum is defined.
    position : Array, radians = np.zeros(2)
        The (x, y) on-sky position of this object.
    flux : float, photons = 1.
        The flux of the object.
    distribution : Array = np.ones((3, 3))
        The array of intensities representing the resolved source.
    weights : Array = None
        The spectral weights of the object.
    spectrum : Spectrum = None
        The spectrum of this object, represented by a Spectrum object.
    """
    distribution = np.asarray(distribution, dtype=float)
    self.distribution = distribution / distribution.sum()

    if self.distribution.ndim != 2:
        raise ValueError("distribution must be a 2d array.")

    super().__init__(
        position=position,
        flux=flux,
        spectrum=spectrum,
        wavelengths=wavelengths,
        weights=weights,
    )

normalise() ¤

Method for returning a new source object with a normalised total spectrum and source distribution.

Returns:

Name Type Description
source Source

The source object with the normalised spectrum and distribution.

Source code in dLux/sources.py
475
476
477
478
479
480
481
482
483
484
485
486
487
488
def normalise(self: ResolvedSource) -> ResolvedSource:
    """
    Method for returning a new source object with a normalised total
    spectrum and source distribution.

    Returns
    -------
    source : Source
        The source object with the normalised spectrum and distribution.
    """
    spectrum = self.spectrum.normalise()
    distribution_floor = np.maximum(self.distribution, 0.0)
    distribution = distribution_floor / distribution_floor.sum()
    return self.set(["spectrum", "distribution"], [spectrum, distribution])
PointResolvedSource

dLux.sources.PointResolvedSource ¤

Bases: ResolvedSource

A class for modelling a point source and a resolved source that is defined relative to the point source. An example would be an unresolved star with a resolved dust shell or debris disk. These two objects share the same spectra but have their flux defined by flux (the mean flux) and the flux ratio (contrast) between the point source and resolved distribution. The resolved component is defined by an array of intensities that represent the resolved distribution.

UML

UML

Attributes:

Name Type Description
position (Array, radians)

The (x, y) on-sky position of this object.

flux (float, photons)

The mean flux of the point and resolved source.

distribution Array

The array of intensities representing the resolved source.

contrast float

The contrast ratio between the point source and the resolved source.

spectrum Spectrum

The spectrum of this object, represented by a Spectrum object.

Source code in dLux/sources.py
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
class PointResolvedSource(ResolvedSource):
    """
    A class for modelling a point source and a resolved source that is defined
    relative to the point source. An example would be an unresolved star with
    a resolved dust shell or debris disk. These two objects share the same
    spectra but have their flux defined by flux (the mean flux) and the flux
    ratio (contrast) between the point source and resolved distribution. The
    resolved component is defined by an array of intensities that represent
    the resolved distribution.

    ??? abstract "UML"
        ![UML](../../assets/uml/PointResolvedSource.png)

    Attributes
    ----------
    position : Array, radians
        The (x, y) on-sky position of this object.
    flux : float, photons
        The mean flux of the point and resolved source.
    distribution : Array
        The array of intensities representing the resolved source.
    contrast : float
        The contrast ratio between the point source and the resolved source.
    spectrum : Spectrum
        The spectrum of this object, represented by a Spectrum object.
    """

    contrast: float

    def __init__(
        self: PointResolvedSource,
        wavelengths: Array = None,
        position: Array = np.zeros(2),
        flux: float = 1.0,
        distribution: Array = np.ones((3, 3)),
        contrast: float = 1.0,
        weights: Array = None,
        spectrum: spectra.BaseSpectrum = None,
    ) -> PointResolvedSource:
        """
        Parameters
        ----------
        wavelengths : Array, metres = None
            The array of wavelengths at which the spectrum is defined.
        position : Array, radians = np.zeros(2)
            The (x, y) on-sky position of this object.
        flux : float, photons = 1.
            The mean flux of the point and resolved source.
        distribution : Array = np.ones((3, 3))
            The array of intensities representing the resolved source.
        contrast : float = 1.
            The contrast ratio between the point source and the resolved source.
        weights : Array = None
            The spectral weights of the object.
        spectrum : Spectrum = None
            The spectrum of this object, represented by a Spectrum object.
        """
        wavelengths = _as_wavelengths_1d(wavelengths)
        if weights is None:
            n_wavelengths = _infer_n_wavelengths(wavelengths, spectrum)
            weights = np.ones((2, n_wavelengths))

        self.contrast = float(contrast)

        super().__init__(
            wavelengths=wavelengths,
            position=position,
            flux=flux,
            distribution=distribution,
            spectrum=spectrum,
            weights=weights,
        )

    def model(
        self: PointResolvedSource,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        # Normalise and get parameters
        self = self.normalise()
        flux = dlu.fluxes_from_contrast(self.flux, self.contrast)
        weights = self.weights * flux[:, None]

        # Note that we always return a wavefront here so we can convolve each
        # wavelength
        # individually if a chromatic wavefront output is required. We also
        # cannot propagate the weights since they have different values
        # for the point and resolved source.
        wf = optics.propagate(self.wavelengths, self.position, return_wf=True)

        # Returning wf is a special case
        if return_wf:
            raise NotImplementedError(
                "Wavefront information cannot be preserved through convolution. "
                "Convolution can only operate on PSFs (incoherent light). "
                "Please use return_wf=False to get the PSF array or return_psf=True "
                "to get a PSF object."
            )

        # Create single PSF-array object
        point_psf = (np.expand_dims(weights[0], (1, 2)) * wf.psf).sum(0)
        resolved_psf = (np.expand_dims(weights[1], (1, 2)) * wf.psf).sum(0)
        conv_psf = jsp.signal.convolve(resolved_psf, self.distribution, mode="same")
        psf = point_psf + conv_psf
        if return_psf:
            return PSF(psf, wf.pixel_scale.mean())

        # Return PSF array
        return psf

__init__(wavelengths=None, position=np.zeros(2), flux=1.0, distribution=np.ones((3, 3)), contrast=1.0, weights=None, spectrum=None) ¤

Parameters:

Name Type Description Default
wavelengths Array, metres = None

The array of wavelengths at which the spectrum is defined.

None
position Array, radians = np.zeros(2)

The (x, y) on-sky position of this object.

zeros(2)
flux float, photons = 1.

The mean flux of the point and resolved source.

1.0
distribution Array = np.ones((3, 3))

The array of intensities representing the resolved source.

ones((3, 3))
contrast float = 1.

The contrast ratio between the point source and the resolved source.

1.0
weights Array = None

The spectral weights of the object.

None
spectrum Spectrum = None

The spectrum of this object, represented by a Spectrum object.

None
Source code in dLux/sources.py
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
def __init__(
    self: PointResolvedSource,
    wavelengths: Array = None,
    position: Array = np.zeros(2),
    flux: float = 1.0,
    distribution: Array = np.ones((3, 3)),
    contrast: float = 1.0,
    weights: Array = None,
    spectrum: spectra.BaseSpectrum = None,
) -> PointResolvedSource:
    """
    Parameters
    ----------
    wavelengths : Array, metres = None
        The array of wavelengths at which the spectrum is defined.
    position : Array, radians = np.zeros(2)
        The (x, y) on-sky position of this object.
    flux : float, photons = 1.
        The mean flux of the point and resolved source.
    distribution : Array = np.ones((3, 3))
        The array of intensities representing the resolved source.
    contrast : float = 1.
        The contrast ratio between the point source and the resolved source.
    weights : Array = None
        The spectral weights of the object.
    spectrum : Spectrum = None
        The spectrum of this object, represented by a Spectrum object.
    """
    wavelengths = _as_wavelengths_1d(wavelengths)
    if weights is None:
        n_wavelengths = _infer_n_wavelengths(wavelengths, spectrum)
        weights = np.ones((2, n_wavelengths))

    self.contrast = float(contrast)

    super().__init__(
        wavelengths=wavelengths,
        position=position,
        flux=flux,
        distribution=distribution,
        spectrum=spectrum,
        weights=weights,
    )
Scene

dLux.sources.Scene ¤

Bases: BaseSource

A source object that holds a set of sources that are model simultaneously.

UML

UML

Attributes:

Name Type Description
sources dict

A dictionary of source objects to model simultaneously.

Source code in dLux/sources.py
751
752
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
class Scene(BaseSource):
    """
    A source object that holds a set of sources that are model simultaneously.

    ??? abstract "UML"
        ![UML](../../assets/uml/Scene.png)

    Attributes
    ----------
    sources : dict
        A dictionary of source objects to model simultaneously.
    """

    sources: dict

    def __init__(self: Scene, sources: list[Source]):
        """
        Parameters
        ----------
        sources : list[Source]
            A list of source objects to model simultaneously.
        """
        super().__init__()
        if isinstance(sources, BaseSource):
            sources = [sources]
        elif isinstance(sources, tuple):
            sources = list(sources)
        self.sources = dlu.list2dictionary(sources, False, BaseSource)

    def normalise(self: Scene) -> Scene:
        """
        Method for returning a new scene with normalised source objects.

        Returns
        -------
        scene : Scene
            The normalised scene object.
        """
        is_source = lambda leaf: isinstance(leaf, BaseSource)
        norm_fn = lambda source: source.normalise()
        sources = jtu.map(norm_fn, self.sources, is_leaf=is_source)
        return self.set("sources", sources)

    def __getattr__(self: Scene, key: str) -> Any:
        """
        Raises the individual sources via their keys.

        Parameters
        ----------
        key : str
            The key of the item to be searched for in the sub-dictionaries.

        Returns
        -------
        item : Any
            The item corresponding to the supplied key in the sub-dictionaries.
        """
        if key in self.sources.keys():
            return self.sources[key]
        raise dlu.helpers.missing_attribute_error(
            self,
            key,
            list(self.sources.keys()),
        )

    def model(
        self: Scene,
        optics: BaseOpticalSystem,
        return_wf: bool = False,
        return_psf: bool = False,
    ) -> Array | Wavefront | PSF:
        _validate_return_mode(return_wf, return_psf)
        self = self.normalise()

        # Define leaf_fn and map across sources
        leaf_fn = lambda leaf: isinstance(leaf, BaseSource)
        output = jtu.map(
            lambda s: s.model(optics, return_wf, return_psf),
            self.sources,
            is_leaf=leaf_fn,
        )

        # Return wf case is simple
        if return_wf:
            return output

        # Return PSF case requires mapping across the PSF outputs
        if return_psf:
            # Define mapping function
            leaf_fn = lambda leaf: isinstance(leaf, PSF)
            get_psfs = lambda psf: psf.data.sum(tuple(range(psf.ndim)))
            get_pscales = lambda psf: psf.pixel_scale.mean()

            # Get values and return PSF
            psf = dlu.map2array(get_psfs, output, leaf_fn).sum(0)
            pixel_scale = dlu.map2array(get_pscales, output, leaf_fn).mean()
            return PSF(psf, pixel_scale)

        # Return array is simple
        return dlu.map2array(lambda x: x, output).sum(0)

__getattr__(key) ¤

Raises the individual sources via their keys.

Parameters:

Name Type Description Default
key str

The key of the item to be searched for in the sub-dictionaries.

required

Returns:

Name Type Description
item Any

The item corresponding to the supplied key in the sub-dictionaries.

Source code in dLux/sources.py
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
def __getattr__(self: Scene, key: str) -> Any:
    """
    Raises the individual sources via their keys.

    Parameters
    ----------
    key : str
        The key of the item to be searched for in the sub-dictionaries.

    Returns
    -------
    item : Any
        The item corresponding to the supplied key in the sub-dictionaries.
    """
    if key in self.sources.keys():
        return self.sources[key]
    raise dlu.helpers.missing_attribute_error(
        self,
        key,
        list(self.sources.keys()),
    )

__init__(sources) ¤

Parameters:

Name Type Description Default
sources list[Source]

A list of source objects to model simultaneously.

required
Source code in dLux/sources.py
766
767
768
769
770
771
772
773
774
775
776
777
778
def __init__(self: Scene, sources: list[Source]):
    """
    Parameters
    ----------
    sources : list[Source]
        A list of source objects to model simultaneously.
    """
    super().__init__()
    if isinstance(sources, BaseSource):
        sources = [sources]
    elif isinstance(sources, tuple):
        sources = list(sources)
    self.sources = dlu.list2dictionary(sources, False, BaseSource)

normalise() ¤

Method for returning a new scene with normalised source objects.

Returns:

Name Type Description
scene Scene

The normalised scene object.

Source code in dLux/sources.py
780
781
782
783
784
785
786
787
788
789
790
791
792
def normalise(self: Scene) -> Scene:
    """
    Method for returning a new scene with normalised source objects.

    Returns
    -------
    scene : Scene
        The normalised scene object.
    """
    is_source = lambda leaf: isinstance(leaf, BaseSource)
    norm_fn = lambda source: source.normalise()
    sources = jtu.map(norm_fn, self.sources, is_leaf=is_source)
    return self.set("sources", sources)