Skip to content

Commit

Permalink
Type checks added to analysis.photometry
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Stolker committed May 6, 2020
1 parent d82d3d8 commit 9a4f9e5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 47 deletions.
18 changes: 6 additions & 12 deletions docs/tutorials/synthetic_photometry.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Initiating species v0.1.4... [DONE]\n",
"Creating species_config.ini... [DONE]\n",
"Initiating species v0.2.2... [DONE]\n",
"Database: /Users/tomasstolker/applications/species/docs/tutorials/species_database.hdf5\n",
"Data folder: /Users/tomasstolker/applications/species/docs/tutorials/data\n",
"Working folder: /Users/tomasstolker/applications/species/docs/tutorials\n",
"Creating species_database.hdf5... [DONE]\n",
"Creating data folder... [DONE]\n"
"Working folder: /Users/tomasstolker/applications/species/docs/tutorials\n"
]
},
{
"data": {
"text/plain": [
"<species.core.setup.SpeciesInit at 0x12666a358>"
"<species.core.setup.SpeciesInit at 0x10a93e080>"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -102,7 +99,7 @@
{
"data": {
"text/plain": [
"('data/plnt_Jupiter.txt', <http.client.HTTPMessage at 0x1255d8d30>)"
"('data/plnt_Jupiter.txt', <http.client.HTTPMessage at 0x129989cc0>)"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -168,7 +165,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Adding filter: JWST/NIRCam.F115W... [DONE]\n",
"Plotting spectrum: spectrum.png... [DONE]\n"
]
}
Expand Down Expand Up @@ -243,7 +239,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Flux [W m-2 micron-1] = 2.64e-09 +/- 1.00e-13\n"
"Flux [W m-2 micron-1] = 2.64e-09 +/- 9.89e-14\n"
]
}
],
Expand All @@ -268,9 +264,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading Vega spectrum (270 kB)... [DONE]\n",
"Adding Vega spectrum... [DONE]\n",
"Apparent magnitude [mag] = 0.49 +/- 4.05e-05\n"
"Apparent magnitude [mag] = 0.49 +/- 4.10e-05\n"
]
}
],
Expand Down
81 changes: 50 additions & 31 deletions species/analysis/photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import warnings
import configparser

from typing import Optional, Union, Tuple, List

import h5py
import numpy as np

from typeguard import typechecked

from species.data import database
from species.read import read_filter, read_calibration
from species.util import phot_util
Expand All @@ -20,8 +24,9 @@ class SyntheticPhotometry:
Class for calculating synthetic photometry from a spectrum.
"""

@typechecked
def __init__(self,
filter_name):
filter_name: str) -> None:
"""
Parameters
----------
Expand All @@ -48,7 +53,8 @@ def __init__(self,

self.database = config['species']['database']

def zero_point(self):
@typechecked
def zero_point(self) -> np.float64:
"""
Internal function for calculating the zero point of the provided ``filter_name``.
Expand Down Expand Up @@ -89,22 +95,25 @@ def zero_point(self):

return self.spectrum_to_flux(wavelength_crop, flux_crop)[0]

@typechecked
def spectrum_to_flux(self,
wavelength,
flux,
error=None,
threshold=0.05):
wavelength: np.ndarray,
flux: np.ndarray,
error: Optional[np.ndarray] = None,
threshold: Optional[float] = 0.05) -> Tuple[
Union[np.float32, np.float64],
Union[Optional[np.float32], Optional[np.float64]]]:
"""
Function for calculating the average flux from a spectrum and a filter profile. The error
is propagated by sampling 200 random values from the error distributions.
Parameters
----------
wavelength : numpy.ndarray
wavelength : np.ndarray
Wavelength points (um).
flux : numpy.ndarray
flux : np.ndarray
Flux (W m-2 um-1).
error : numpy.ndarray
error : np.ndarray
Uncertainty (W m-2 um-1). Not used if set to None.
threshold : float, None
Transmission threshold (value between 0 and 1). If the minimum transmission value is
Expand Down Expand Up @@ -208,24 +217,27 @@ def spectrum_to_flux(self,

return syn_flux, error_flux

@typechecked
def spectrum_to_magnitude(self,
wavelength,
flux,
error=None,
distance=None,
threshold=0.05):
wavelength: np.ndarray,
flux: np.ndarray,
error: Optional[Union[np.ndarray, List[np.ndarray]]] = None,
distance: Optional[Tuple[float, Optional[float]]] = None,
threshold: Optional[float] = 0.05) -> Tuple[
Tuple[float, Optional[float]],
Optional[Tuple[Optional[float], Optional[float]]]]:
"""
Function for calculating the apparent and absolute magnitude from a spectrum and a
filter profile. The error is propagated by sampling 200 random values from the error
distributions.
Parameters
----------
wavelength : numpy.ndarray
wavelength : np.ndarray
Wavelength points (um).
flux : numpy.ndarray
flux : np.ndarray
Flux (W m-2 um-1).
error : numpy.ndarray, list(numpy.ndarray), None
error : np.ndarray, list(np.ndarray), None
Uncertainty (W m-2 um-1).
distance : tuple(float, float), None
Distance and uncertainty (pc). No absolute magnitude is calculated if set to None.
Expand Down Expand Up @@ -289,10 +301,11 @@ def spectrum_to_magnitude(self,

return (app_mag, error_app_mag), (abs_mag, error_abs_mag)

@typechecked
def magnitude_to_flux(self,
magnitude,
error=None,
zp_flux=None):
magnitude: float,
error: Optional[float] = None,
zp_flux: Optional[float] = None) -> Tuple[np.float64, np.float64]:
"""
Function for converting a magnitude to a flux.
Expand All @@ -301,9 +314,9 @@ def magnitude_to_flux(self,
magnitude : float
Magnitude (mag).
error : float, None
Error (mag). Not used if set to None.
zp_flux : float
Zero-point flux (W m-2 um-1). The value is calculated if set to None.
Error (mag). Not used if set to ``None``.
zp_flux : float, None
Zero-point flux (W m-2 um-1). The value is calculated if set to ``None``.
Returns
-------
Expand All @@ -328,30 +341,36 @@ def magnitude_to_flux(self,

return flux, error_flux

@typechecked
def flux_to_magnitude(self,
flux,
error=None,
distance=None):
flux: float,
error: Optional[Union[float, np.ndarray]] = None,
distance: Optional[Union[Tuple[float, Optional[float]],
Tuple[np.ndarray, Optional[np.ndarray]]]] = None
) -> Tuple[Union[Tuple[float, Optional[float]],
Tuple[np.ndarray, Optional[np.ndarray]]],
Union[Tuple[float, Optional[float]],
Tuple[np.ndarray, Optional[np.ndarray]]]]:
"""
Function for converting a flux into a magnitude.
Parameters
----------
flux : float, numpy.ndarray
flux : float, np.ndarray
Flux (W m-2 um-1).
error : float, numpy.ndarray, None
error : float, np.ndarray, None
Uncertainty (W m-2 um-1). Not used if set to None.
distance : tuple(float, float), tuple(numpy.ndarray, numpy.ndarray)
distance : tuple(float, float), tuple(np.ndarray, np.ndarray)
Distance and uncertainty (pc). The returned absolute magnitude is set to None in case
``distance`` is set to None. The error is not propagated into the error on the absolute
magnitude in case the distance uncertainty is set to None, for example
``distance=(20., None)``
Returns
-------
tuple(float, float), tuple(numpy.ndarray, numpy.ndarray)
tuple(float, float), tuple(np.ndarray, np.ndarray)
Apparent magnitude and uncertainty (mag).
tuple(float, float), tuple(numpy.ndarray, numpy.ndarray)
tuple(float, float), tuple(np.ndarray, np.ndarray)
Absolute magnitude and uncertainty (mag).
"""

Expand Down
8 changes: 7 additions & 1 deletion species/data/companions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
Module for extracting data of directly imaged planets and brown dwarfs.
"""

from typing import Union, Dict, Tuple, List

def get_data():
from typeguard import typechecked


@typechecked
def get_data() -> Dict[str, Dict[str, Union[Tuple[float, float],
Dict[str, Union[Tuple[float, float], List[Tuple[float, float]]]]]]]:
"""
Function for extracting a dictionary with the distances (pc) and apparent magnitudes of
directly imaged planets and brown dwarfs.
Expand Down
7 changes: 4 additions & 3 deletions species/read/read_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import configparser

from typing import Tuple
from typing import Union, Tuple

import h5py
import numpy as np
Expand Down Expand Up @@ -94,7 +94,8 @@ def interpolate_filter(self) -> interpolate.interp1d:
fill_value=float('nan'))

@typechecked
def wavelength_range(self) -> Tuple[float, float]:
def wavelength_range(self) -> Tuple[Union[np.float32, np.float64],
Union[np.float32, np.float64]]:
"""
Extract the wavelength range of the filter profile.
Expand All @@ -108,7 +109,7 @@ def wavelength_range(self) -> Tuple[float, float]:

data = self.get_filter()

return np.amin(data[0, ]), np.amax(data[0, ])
return data[0, 0], data[0, -1]

@typechecked
def mean_wavelength(self) -> float:
Expand Down

0 comments on commit 9a4f9e5

Please sign in to comment.