Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test suite code review #17

Open
wants to merge 11 commits into
base: tdd_develop
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ env/
# Sphinx documentation
docs/build/
docs/_build/

# Test coverage
.coverage
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
"python.testing.unittestEnabled": true,
"esbonio.server.enabled": true,
"python.formatting.provider": "black"
}
Binary file modified requirements.txt
Binary file not shown.
44 changes: 44 additions & 0 deletions superbol/FlxuCurve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import matplotlib.pyplot as plt

from superbol import extinction, read_osc

"""
TODO Need to create a class that contains the fluxes, magnitudes and etc. that are calculated from
an osc collection
This class should also contain a function that opens a graph to view the flux and its details
"""
class FluxCurve:
"""constructor for file path,
constructor for photometry given
constructor for url given
"""
photometries = []
luminosities = []
times = []

def __init__(self, photometries):
self.photometries = photometries
self.luminosities
# fluxes = []
# for photometry_dict in photometries:
# try:
# observed_magnitude = read_osc.get_observed_magnitude(photometry_dict)
# extinction_value = extinction.get_extinction_by_name(extinction_table, observed_magnitude.band)
# observed_magnitude.magnitude = extinction.correct_observed_magnitude(observed_magnitude, extinction_value)
# fluxes.append(observed_magnitude.convert_to_flux())
# except:
# pass

# distance = lum.Distance(3.0E7 * 3.086E18, 7.0E6 * 3.086E18)
# self.luminosities = lightcurve.calculate_lightcurve(fluxes, distance, lqbol.calculate_qbol_flux)


def draw_curve(self):
x=[1,3,5,7]
y=[2,4,6,1]
print('drawing curve')
plt.plot(x,y)
plt.show()

def sum_fn(self):
print('yeah im working. what ab it')
103 changes: 27 additions & 76 deletions superbol/blackbody.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from astropy import constants as const
from .planck import *


def bb_flux(wavelength, temperature, angular_radius):
"""Observed flux at `wavelength` from a blackbody of `temperature` and `angular_radius` in cgs units

Expand All @@ -15,87 +16,28 @@ def bb_flux(wavelength, temperature, angular_radius):
Returns:
Astropy Quantity: blackbody flux in :math:`erg \\; s^{-1} cm^{-2} Anstrom^{-1}`
"""
bb_flux = (np.pi * u.sr) * planck_function(wavelength, temperature) * (angular_radius)**2
bb_flux = (
(np.pi * u.sr)
* planck_function(wavelength, temperature)
* (angular_radius) ** 2
)

return bb_flux


def bb_flux_nounits(wavelength, temperature, angular_radius):
"""Identical to bb_flux, but returns the value rather than the Astropy Quantity.

This function is needed because curve_fit() does not play nice with functions that return Astropy quantities.
"""
return bb_flux(wavelength, temperature, angular_radius).value

def bb_total_flux(self):
"""Integrate the planck function from :math:`\\lambda = 0` to
:math:`\\lambda = \\infty`, then convert result to observed flux.

The calculation is done using the Stefan-Boltxmann law, rather than
performing an actual integral using numerical integration. The
`angular_radius` is included to convert the result to an observed flux.

Args:
temperature (float): Temperature in Kelvin
angular_radius (float): Angular radius :math:`(\\theta = \\frac{R}{D})`

Returns:
float: The value of :math:`\\sigma \\frac{R^2}{D^2} T^4`
"""
bb_total_flux = (const.sigma_sb * self.angular_radius**2
* self.temperature**4)
bb_total_flux = bb_total_flux.to(u.erg / (u.s * u.cm**2))
return bb_total_flux.value


def bb_flux_integrated(wavelength, temperature, angular_radius):
"""Integrate the planck function from :math:`\\lambda = 0` to :math:`\\lambda =` `wavelength`, then convert result to observed flux

Args:
wavelength (float): Wavelength in Angstroms
temperature (float): Temperature in Kelvin
angular_radius (float): Angular radius :math:`(\\theta = \\frac{R}{D})`

Returns:
float: Value of the integrated flux in :math:`erg \\; s^{-1} cm^{-2}`
"""
bb_flux_integrated = (np.pi * u.sr) * planck_integral(wavelength, temperature) * (angular_radius)**2

return bb_flux_integrated.value

def dbb_flux_integrated_dT(wavelength, temperature, angular_radius):
"""Take the derivative of the integrated planck function, then convert result to observed flux. This is used in error propagation calculations.

Args:
wavelength (float): Wavelength in Angstroms
temperature (float): Temperature in Kelvin
angular_radius (float): Angular radius :math:`(\\theta = \\frac{R}{D})`

Returns:
float: Derivative of the integrated blackbody flux at `wavelength` with respect to `temperature`
"""
dbb_flux_integrated_dT = (np.pi * u.sr) * d_planck_integral_dT(wavelength, temperature) * (angular_radius)**2

return dbb_flux_integrated_dT.value

def dbb_total_flux_dT(temperature, angular_radius):
"""Take the derivative of the total blackbody flux with respect to temperature

The calculation takes a derivative of the Stefan-Boltzmann law with respect to temperature. The `angular_radius` is present to convert the result to an observed flux.

Args:
temperature (float): Temperature in Kelvin
angular_radius (float): Angular radius :math:`(\\theta = \\frac{R}{D})`

Returns:
float: The value of :math:`4 \\sigma \\frac{R^2}{D^2} T^3`
"""
temperature = u.Quantity(temperature, u.K)
bb_total_flux = 4.0 * const.sigma_sb * (angular_radius**2) * temperature**3
bb_total_flux = bb_total_flux.to(u.erg / (u.s * u.cm**2 * u.K))
return bb_total_flux.value

class BlackbodyFit(object):

def __init__(self):
self.temperature = None
self.angular_radius = None
Expand All @@ -105,23 +47,28 @@ def __init__(self):

def fit_to_SED(self, SED):
"""Fits the temperature and angular_radius of a blackbody to the SED

The initial guesses for the `temperature` and `angular_radius` are
:math:`T = 5000` K and :math:`\\theta = 1.0 \\times 10^{-10}`.
These are typical for an extragalactic supernovae, but should be used
with caution for any other objects.

Args:
SED (list): List of observed MonochromaticFlux objects.
"""
wavelengths = [f.wavelength for f in SED]
fluxes = [f.flux for f in SED]
flux_uncertainties = [f.flux_uncertainty for f in SED]

popt, pcov = curve_fit(bb_flux_nounits, wavelengths, fluxes,
p0=[5000, 1.0e-10], sigma=flux_uncertainties,
absolute_sigma=True)

popt, pcov = curve_fit(
bb_flux_nounits,
wavelengths,
fluxes,
p0=[5000, 1.0e-10],
sigma=flux_uncertainties,
absolute_sigma=True,
)

self.SED = SED
self.temperature = popt[0]
self.angular_radius = popt[1]
Expand All @@ -130,18 +77,22 @@ def fit_to_SED(self, SED):
self.temperature_err = perr[0]
self.angular_radius_err = perr[1]


def __call__(self, wavelength):
"""Flux of the blackbody at a given wavelength in angstroms

Args:
wavelength (float): Wavelength in Angstroms

Returns:
bb_flux (float): blackbody flux at given wavelength in cgs units"""
if self.temperature == None:
# TODO No test written
return None
else:
bb_flux = (np.pi * u.sr * planck_function(wavelength, self.temperature)
* self.angular_radius**2)
bb_flux = (
np.pi
* u.sr
* planck_function(wavelength, self.temperature)
* self.angular_radius**2
)
return bb_flux.value
Loading