Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-p committed Jun 25, 2024
2 parents d0077fa + 8414dcd commit 09204a9
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 534 deletions.
84 changes: 55 additions & 29 deletions asteca/cluster.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import warnings
from dataclasses import dataclass
import numpy as np
import pandas as pd
from .modules import cluster_priv as cp
from .modules import nmembers as nm


@dataclass
class Cluster:
"""Define a :py:class:`Cluster` object.
This object contains the basic data required to load a group of observed stars
that could represent a cluster or an entire field.
:param obs_df: `pandas DataFrame <https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html>`__
with the observed loaded data.
:type obs_df: pd.DataFrame
Expand Down Expand Up @@ -42,7 +43,7 @@ class Cluster:
:type plx: str | None
:param e_plx: Name of the DataFrame column that contains the parallax uncertainty,
defaults to ``None``
:type plx: str | None
:type e_plx: str | None
:param pmra: Name of the DataFrame column that contains the RA proper motion,
defaults to ``None``
:type pmra: str | None
Expand All @@ -59,43 +60,71 @@ class Cluster:
:type N_clust_min: int
:param N_clust_max: Maximum number of cluster members, defaults to ``5000``
:type N_clust_max: int
:raises ValueError: If there are missing required attributes to generate the
:py:class:`Cluster <asteca.cluster.Cluster>` object
"""

obs_df: pd.DataFrame
ra: str | None = None
dec: str | None = None
magnitude: str | None = None
e_mag: str | None = None
color: str | None = None
e_color: str | None = None
color2: str | None = None
e_color2: str | None = None
plx: str | None = None
pmra: str | None = None
pmde: str | None = None
e_plx: str | None = None
e_pmra: str | None = None
e_pmde: str | None = None
N_clust_min: int = 25
N_clust_max: int = 5000

def __post_init__(self):
"""The photometry is stored with a '_p' to differentiate from the
self.magnitude, self.color, etc that are defined with the class is called.
"""
def __init__(
self,
obs_df: pd.DataFrame,
ra: str | None = None,
dec: str | None = None,
magnitude: str | None = None,
e_mag: str | None = None,
color: str | None = None,
e_color: str | None = None,
color2: str | None = None,
e_color2: str | None = None,
plx: str | None = None,
e_plx: str | None = None,
pmra: str | None = None,
e_pmra: str | None = None,
pmde: str | None = None,
e_pmde: str | None = None,
N_clust_min: int = 25,
N_clust_max: int = 5000,
) -> None:
self.obs_df = obs_df
self.ra = ra
self.dec = dec
self.magnitude = magnitude
self.e_mag = e_mag
self.color = color
self.e_color = e_color
self.color2 = color2
self.e_color2 = e_color2
self.plx = plx
self.pmra = pmra
self.pmde = pmde
self.e_plx = e_plx
self.e_pmra = e_pmra
self.e_pmde = e_pmde
self.N_clust_min = N_clust_min
self.N_clust_max = N_clust_max

self.N_stars = len(self.obs_df)
print("\nInstantiating cluster...")
print(f"N_stars : {self.N_stars}")
print(f"N_clust_min : {self.N_clust_min}")
print(f"N_clust_max : {self.N_clust_max}")

dim_count = self._load()
if dim_count > 0:
print("Cluster object generated")
else:
raise ValueError("No column names defined for cluster")

def _load(self):
dim_count = 0

if self.ra is not None:
self.ra_v = self.obs_df[self.ra].values
self.dec_v = self.obs_df[self.dec].values
print(f"(RA, DEC) : ({self.ra}, {self.dec})")
dim_count += 1

# TODO: change _p to _v
if self.magnitude is not None:
self.mag_p = self.obs_df[self.magnitude].values
if self.e_mag is not None:
Expand Down Expand Up @@ -148,10 +177,7 @@ def __post_init__(self):
print(f"pmDE : {self.pmra} [{self.e_pmde}]")
dim_count += 1

if dim_count > 0:
print("Cluster object generated")
else:
raise ValueError("No column names defined for cluster")
return dim_count

def get_center(
self,
Expand Down
61 changes: 38 additions & 23 deletions asteca/isochrones.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import numpy as np
from dataclasses import dataclass
from .modules import isochrones_priv


@dataclass
class Isochrones:
"""Define an :py:class:`Isochrones` object.
Expand All @@ -16,7 +14,7 @@ class Isochrones:
`PARSEC <http://stev.oapd.inaf.it/cgi-bin/cmd_3.7>`__,
`MIST <https://waps.cfa.harvard.edu/MIST/>`__, or
`BASTI <http://basti-iac.oa-abruzzo.inaf.it/isocs.html>`__.
:type model: str: ``PARSEC, MIST, BASTI``
:type model: str
:param isochs_path: Path to the folder that contains the files for the theoretical
isochrones
:type isochs_path: str
Expand Down Expand Up @@ -52,13 +50,6 @@ class Isochrones:
:py:meth:`Synthetic.generate() <asteca.synthetic.Synthetic.generate>` method,
defaults to ``None``
:type z_to_FeH: float | None
:param column_names: Column names for the initial mass, metallicity, and age for
the photometric system's isochrones files. Example:
``{"mass_col": "Mini", "met_col": "Zini", "age_col": "logAge"}``.
This dictionary is defined internally in **ASteCA** and should only be given
by the user if the isochrone service changes its format and the `isochrones`
class fails to load the files, defaults to ``None``
:type column_names: dict, optional
:param N_interp: Number of interpolation points used to ensure that all isochrones
are the same shape, defaults to ``2500``
:type N_interp: int
Expand All @@ -67,22 +58,46 @@ class fails to load the files, defaults to ``None``
"`in preparation <http://stev.oapd.inaf.it/cmd_3.7/faq.html>`__", defaults
to ``True``
:type parsec_rm_stage_9: bool
:param column_names: Column names for the initial mass, metallicity, and age for
the photometric system's isochrones files. Example:
``{"mass_col": "Mini", "met_col": "Zini", "age_col": "logAge"}``.
This dictionary is defined internally in **ASteCA** and should only be given
by the user if the isochrone service changes its format and the `isochrones`
class fails to load the files, defaults to ``None``
:type column_names: dict | None
:raises ValueError: If any of the attributes is not recognized as a valid option,
or there are missing required attributes
"""

model: str
isochs_path: str
magnitude: str
color: tuple
color2: tuple | None = None
magnitude_effl: float | None = None
color_effl: tuple | None = None
color2_effl: tuple | None = None
z_to_FeH: float | None = None
column_names: dict | None = None
N_interp: int = 2500
parsec_rm_stage_9: bool = True
def __init__(
self,
model: str,
isochs_path: str,
magnitude: str,
color: tuple,
color2: tuple | None = None,
magnitude_effl: float | None = None,
color_effl: tuple | None = None,
color2_effl: tuple | None = None,
z_to_FeH: float | None = None,
N_interp: int = 2500,
parsec_rm_stage_9: bool = True,
column_names: dict | None = None,
) -> None:
self.model = model
self.isochs_path = isochs_path
self.magnitude = magnitude
self.color = color
self.color2 = color2
self.magnitude_effl = magnitude_effl
self.color_effl = color_effl
self.color2_effl = color2_effl
self.z_to_FeH = z_to_FeH
self.column_names = column_names
self.N_interp = N_interp
self.parsec_rm_stage_9 = parsec_rm_stage_9

def __post_init__(self):
# Check that the number of colors match
if self.color2 is not None and self.color2_effl is None:
raise ValueError(
Expand Down
34 changes: 19 additions & 15 deletions asteca/likelihood.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from dataclasses import dataclass
import numpy as np
from .cluster import Cluster
from .modules import likelihood_priv as lpriv


@dataclass
class Likelihood:
"""Define a :py:class:`Likelihood` object.
Expand All @@ -15,24 +13,30 @@ class Likelihood:
:param my_cluster: :py:class:`Cluster <asteca.cluster.Cluster>` object with the
loaded data for the observed cluster
:type my_cluster: :py:class:`Cluster <asteca.cluster.Cluster>`
:type my_cluster: Cluster
:param lkl_name: Currently only the Poisson likelihood ratio defined in
`Tremmel et al. (2013) <https://ui.adsabs.harvard.edu/abs/2013ApJ...766...19T/abstract)>`__
`Tremmel et al. (2013)
<https://ui.adsabs.harvard.edu/abs/2013ApJ...766...19T/abstract)>`__
is accepted, defaults to ``plr``
:type lkl_name: str
:param bin_method: Bin method used to split the color-magnitude diagram into cells
(`Hess diagram <https://en.wikipedia.org/wiki/Hess_diagram>`__). If ``manual``
(`Hess diagram <https://en.wikipedia.org/wiki/Hess_diagram>`__); one of:
``knuth, fixed, bayes_blocks, manual``. If ``manual``
is selected, a list containing an array of edge values for the magnitude,
followed by one or two arrays (depending on the number of colors defined) for
the color(s), also with edge values, defaults to ``knuth``
:type bin_method: str: ``knuth, fixed, bayes_blocks, manual``
:type bin_method: str
:raises ValueError: If any of the attributes is not recognized as a valid option
"""

my_cluster: Cluster
lkl_name: str = "plr"
bin_method: str = "knuth"
def __init__(
self, my_cluster: Cluster, lkl_name: str = "plr", bin_method: str = "knuth"
) -> None:
self.my_cluster = my_cluster
self.lkl_name = lkl_name
self.bin_method = bin_method

def __post_init__(self):
likelihoods = ("plr", "bins_distance")
if self.lkl_name not in likelihoods:
raise ValueError(
Expand All @@ -54,7 +58,7 @@ def __post_init__(self):
np.array([self.my_cluster.mag_p, *self.my_cluster.colors_p])
)

print("Likelihood object generated")
print("\nLikelihood object generated")

def get(self, synth_clust: np.array) -> float:
"""Evaluate the selected likelihood function.
Expand All @@ -70,9 +74,9 @@ def get(self, synth_clust: np.array) -> float:
"""
if self.lkl_name == "plr":
return lpriv.tremmel(self, synth_clust)
if self.lkl_name == "visual":
return lpriv.visual(self, synth_clust)
if self.lkl_name == "mean_dist":
return lpriv.mean_dist(self, synth_clust)
# if self.lkl_name == "visual":
# return lpriv.visual(self, synth_clust)
# if self.lkl_name == "mean_dist":
# return lpriv.mean_dist(self, synth_clust)
if self.lkl_name == "bins_distance":
return lpriv.bins_distance(self, synth_clust)
13 changes: 7 additions & 6 deletions asteca/membership.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from dataclasses import dataclass
import numpy as np
from .cluster import Cluster
from .modules import cluster_priv as cp
from .modules.fastmp import fastMP
from .modules.bayesian_da import bayesian_mp


@dataclass
class Membership:
"""Define a :py:class:`Membership` object.
Expand All @@ -27,14 +25,17 @@ class Membership:
:py:class:`Cluster <asteca.cluster.Cluster>` object. Photometry data is not
employed.
:param cluster: :py:class:`Cluster <asteca.cluster.Cluster>` object with the
:param my_field: :py:class:`Cluster <asteca.cluster.Cluster>` object with the
loaded data for the observed field
:type cluster: :py:class:`Cluster <asteca.cluster.Cluster>`
:type my_field: Cluster
:raises ValueError: If there are missing required attributes in the
:py:class:`Cluster <asteca.cluster.Cluster>` object
"""

my_field: Cluster
def __init__(self, my_field: Cluster) -> None:
self.my_field = my_field

def __post_init__(self):
noradeg_flag = False
try:
self.my_field.ra
Expand Down
Loading

0 comments on commit 09204a9

Please sign in to comment.