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

Format docstrings in spectroscopy functions #356

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions spectroscopy/code_src/data_structures_spec.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
# setup to store the light curves in a data structure
# setup to store the spectra in a data structure
import pandas as pd


class MultiIndexDFObject:
"""
Pandas MultiIndex data frame to store & manipulate multiband light curves
Pandas MultiIndex data frame to store & manipulate spectra.

Examples
--------
# Initialize Pandas MultiIndex data frame for storing the light curve
df_lc = MultiIndexDFObject()
# Initialize Pandas MultiIndex data frame for storing the spectra
df_spec = MultiIndexDFObject()

#make a single multiindex dataframe
dfsingle = pd.DataFrame(dict(flux=[0.1], err=[0.1], time=[time_mjd], objectid=[ccount + 1], /
band=[mission], label=lab)).set_index(["objectid", "label", "band", "time"])
# Make a single multiindex dataframe
df_single = pd.DataFrame(dict(wave=[0.1], flux=[0.1], err=[0.1], instrument=[instrument_name],
objectid=[ccount + 1], filter=[filter_name],
mission=[mission_name], label=[lab]))
df_single = df_single.set_index(["objectid", "label", "filter", "mission"])

# Append to existing MultiIndex light curve object
df_lc.append(dfsingle)

#Show the contents
df_lc.data
# Append to existing MultiIndex object
df_spec.append(dfsingle)

# Show the contents
df_spec.data
"""

def __init__(self, data=None):
"""Create a MultiIndex DataFrame that is empty if data is None, else contains the data.
"""
Create a MultiIndex DataFrame that is empty if data is None, else contains the data.

Parameters
----------
data : pd.DataFrame, optional
Dataframe to store in the `data` attribute.
"""

index = ["objectid", "label", "filter", "mission"]
columns = ["wave", "flux", "err", "instrument"]
self.data = pd.DataFrame(columns=index + columns).set_index(index)
if data is not None:
self.append(data)

def append(self, x):
"""Add a new band of light curve data to the dataframe
"""
Add a new spectra data to the dataframe.

Parameters
----------
x : Pandas dataframe
contains columns [flux, fluxerr] and multi-index [objectid, label, band, time]
Contains columns ["wave", "flux", "err", "instrument"]
and multi-index ["objectid", "label", "filter", "mission"].
"""

if isinstance(x, self.__class__):
# x is a MultiIndexDFObject. extract the DataFrame
new_data = x.data
Expand All @@ -65,11 +71,13 @@ def append(self, x):
self.data = pd.concat([self.data, new_data])

def remove(self, x):
""" Drop a light curve from the dataframe
"""
Drop a row from the dataframe.

Parameters
----------
x : list of values
Index values identifying rows to be dropped.
Index values identifying rows to be dropped.
"""

self.data = self.data.drop(x)
19 changes: 9 additions & 10 deletions spectroscopy/code_src/desi_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@


def DESIBOSS_get_spec(sample_table, search_radius_arcsec):
'''
Retrieves DESI and BOSS spectra for a list of sources.
"""
Retrieve DESI and BOSS spectra for a list of sources.
Note, that we can also retrieve SDSS-DR16 spectra here, which
leads to similar results as SDSS_get_spec().

Parameters
----------
sample_table : `~astropy.table.Table`
Table with the coordinates and journal reference labels of the sources
search_radius_arcsec : `float`
sample_table : astropy.table.Table
Table with the coordinates and journal reference labels of the sources.
search_radius_arcsec : float
Search radius in arcseconds. Here its rather half a box size.

Returns
-------
df_lc : MultiIndexDFObject
The main data structure to store all spectra

'''
MultiIndexDFObject
The spectra returned from the archive.
"""

# Set up client
client = SparclClient()
Expand Down Expand Up @@ -98,4 +97,4 @@ def DESIBOSS_get_spec(sample_table, search_radius_arcsec):
)).set_index(["objectid", "label", "filter", "mission"])
df_spec.append(dfsingle)

return (df_spec)
return df_spec
38 changes: 20 additions & 18 deletions spectroscopy/code_src/herschel_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

def find_max_flux_column(df):
"""
Analyzes a DataFrame with flux columns and returns the column with the largest sum.
Analyze a DataFrame with flux columns and returns the column with the largest sum.
Args:
df (pandas.DataFrame): The DataFrame containing columns with "flux" in the name.
Parameters
----------
df : pandas.DataFrame
The DataFrame containing columns with "flux" in the name.
Returns:
str: The name of the column with the largest sum of values containing "flux".
Returns
-------
str
The name of the column with the largest sum of values containing "flux".
"""


# Filter column names containing "flux"
flux_cols = [col for col in df.columns if "flux" in col.lower()]

Expand All @@ -44,27 +47,26 @@ def find_max_flux_column(df):

def Herschel_get_spec(sample_table, search_radius_arcsec, datadir,
delete_downloaded_data=True):
'''
Retrieves Herschel spectra from a subset of modes for a list of sources.
"""
Retrieve Herschel spectra from a subset of modes for a list of sources.
Parameters
----------
sample_table : `~astropy.table.Table`
Table with the coordinates and journal reference labels of the sources
search_radius_arcsec : `float`
sample_table : astropy.table.Table
Table with the coordinates and journal reference labels of the sources.
search_radius_arcsec : float
Search radius in arcseconds.
datadir : `str`
datadir : str
Data directory where to store the data. Each function will create a
separate data directory (for example "[datadir]/HST/" for HST data).
delete_downloaded_data: `bool`, optional
Should the tarfiles be deteled after spectra are extracted?
delete_downloaded_data : bool, optional
Whether the tar files be deleted after spectra are extracted.
Returns
-------
df_spec : MultiIndexDFObject
The main data structure to store all spectra
'''
MultiIndexDFObject
The spectra returned from the archive.
"""

# Initialize multi-index object:
df_spec = MultiIndexDFObject()
Expand Down
19 changes: 9 additions & 10 deletions spectroscopy/code_src/keck_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@


def KeckDEIMOS_get_spec(sample_table, search_radius_arcsec):
'''
Retrieves Keck DEIMOS on COSMOS spectra for a list of sources.
"""
Retrieve Keck DEIMOS on COSMOS spectra for a list of sources.
Parameters
----------
sample_table : `~astropy.table.Table`
Table with the coordinates and journal reference labels of the sources
search_radius_arcsec : `float`
sample_table : astropy.table.Table
Table with the coordinates and journal reference labels of the sources.
search_radius_arcsec : float
Search radius in arcseconds.
Returns
-------
df_lc : MultiIndexDFObject
The main data structure to store all spectra
'''
MultiIndexDFObject
The spectra returned from the archive.
"""

# Initialize multi-index object:
df_spec = MultiIndexDFObject()
Expand Down Expand Up @@ -84,4 +83,4 @@ def KeckDEIMOS_get_spec(sample_table, search_radius_arcsec):
)).set_index(["objectid", "label", "filter", "mission"])
df_spec.append(dfsingle)

return (df_spec)
return df_spec
Loading