snc-sf 1.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- snc_sf-1.1.1/LICENSE.md +29 -0
- snc_sf-1.1.1/PKG-INFO +61 -0
- snc_sf-1.1.1/README.md +33 -0
- snc_sf-1.1.1/pyproject.toml +37 -0
- snc_sf-1.1.1/src/snc_sf/__init__.py +0 -0
- snc_sf-1.1.1/src/snc_sf/optimize.py +106 -0
- snc_sf-1.1.1/src/snc_sf/selection_function.py +720 -0
- snc_sf-1.1.1/src/snc_sf/sf_files/__init__.py +0 -0
- snc_sf-1.1.1/src/snc_sf/utils.py +557 -0
snc_sf-1.1.1/LICENSE.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Ilija Medan
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
snc_sf-1.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: snc-sf
|
|
3
|
+
Version: 1.1.1
|
|
4
|
+
Summary:
|
|
5
|
+
Author: imedan
|
|
6
|
+
Author-email: ilija.medan@gmail.com
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Requires-Dist: astropy (>=6.1.0)
|
|
13
|
+
Requires-Dist: astroquery (>=0.4.7)
|
|
14
|
+
Requires-Dist: gaiaunlimited (>=0.3.2,<0.4.0)
|
|
15
|
+
Requires-Dist: healpy (>=1.17.1)
|
|
16
|
+
Requires-Dist: jax (>=0.4.27)
|
|
17
|
+
Requires-Dist: jaxlib (>=0.4.27)
|
|
18
|
+
Requires-Dist: matplotlib (>=3.8.0)
|
|
19
|
+
Requires-Dist: notebook (>=7.0.0)
|
|
20
|
+
Requires-Dist: numpy (>=2.0.0)
|
|
21
|
+
Requires-Dist: numpyro (>=0.14.0)
|
|
22
|
+
Requires-Dist: polars (>=1.0.0)
|
|
23
|
+
Requires-Dist: pyarrow (>=14.0.0)
|
|
24
|
+
Requires-Dist: scipy (>=1.13.0)
|
|
25
|
+
Requires-Dist: sdss-semaphore (>=0.2.4,<0.3.0)
|
|
26
|
+
Requires-Dist: tqdm (>=4.60.0)
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# SDSS-V Solar Neighborhood Census Selection Function and Subpopulation Probabilities
|
|
30
|
+
|
|
31
|
+
This repository provides the code to calculate the selection function for the SDSS-V Solar Neighborhood Census (SNC) relative to the Gaia Catalog of Nearby Stars ([GCNS](https://ui.adsabs.harvard.edu/abs/2021A%26A...649A...6G/abstract)). With this selection function, the code allows for the forward modeling of subpopulation probabilities across the HR diagram. A use case would be selecting all stars in the SNC with [Fe/H] < -1 and the forward model would evaluate the likely probability of selecting [Fe/H] < -1 stars from the GCNS in discrete bins across the HR diagram.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
The code can be installed with `pip`:
|
|
36
|
+
```
|
|
37
|
+
git clone https://github.com/imedan/snc_sf
|
|
38
|
+
cd snc_sf
|
|
39
|
+
pip install .
|
|
40
|
+
```
|
|
41
|
+
or in a fresh virtual environment with `poetry` to fully replicate the development environment
|
|
42
|
+
```
|
|
43
|
+
git clone https://github.com/imedan/snc_sf
|
|
44
|
+
cd snc_sf
|
|
45
|
+
conda create -n "snc_sf" python=3.11
|
|
46
|
+
conda activate snc_sf
|
|
47
|
+
pip install poetry
|
|
48
|
+
poetry install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
To fully utilize the code, SDSS-V and GCNS data is need. The SDSS-V DR19 `astra` summary data can be accessed [here](https://data.sdss.org/sas/dr19/spectro/astra/0.6.0/summary/). The required GCNS data will automatically be downloaded when first initializing a `snc_sf.selection_function.SNCSelectionFunction()` object.
|
|
52
|
+
|
|
53
|
+
## Examples
|
|
54
|
+
|
|
55
|
+
A number of examples using the code are located in [`notebooks/`](notebooks/). These examples are fully explained in the paper accompanying this work. In summary, the examples included related to the following:
|
|
56
|
+
- [`dr19_obs.ipynb`](notebooks/dr19_obs.ipynb): Constructs the base SNC 100 pc dataset and creates the plots in Figure 1 of the paper.
|
|
57
|
+
- [`SF_example_plots.ipynb`](notebooks/SF_example_plots.ipynb): Example of the selecrtion function for different regions on the sky, as shown in Figure 2 of the paper.
|
|
58
|
+
- [`forward_model_GCNS_mock_proof.py`](notebooks/forward_model_GCNS_mock_proof.py): Script that replicates the example with mock data in Section 4.1 and Figure 3 in the paper.
|
|
59
|
+
- [`forward_model_halpha_ex.py`](notebooks/forward_model_halpha_ex.py): Script that replicates the example examing the distribution of H-alpha emitters across the HR diagram in Section 4.2 and Figure 4 in the paper.
|
|
60
|
+
- [`forward_model_mass_dens.py`](notebooks/forward_model_mass_dens.py): Script that replicates the example that calculates the stellar number desnity in bins of metallicity and mass in Section 4.3 in the paper.
|
|
61
|
+
- [`appendix_example.ipynb`](notebooks/appendix_example.ipynb): Example to demonstrate basic code usage as illustrated in the Appendix of the paper.
|
snc_sf-1.1.1/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# SDSS-V Solar Neighborhood Census Selection Function and Subpopulation Probabilities
|
|
2
|
+
|
|
3
|
+
This repository provides the code to calculate the selection function for the SDSS-V Solar Neighborhood Census (SNC) relative to the Gaia Catalog of Nearby Stars ([GCNS](https://ui.adsabs.harvard.edu/abs/2021A%26A...649A...6G/abstract)). With this selection function, the code allows for the forward modeling of subpopulation probabilities across the HR diagram. A use case would be selecting all stars in the SNC with [Fe/H] < -1 and the forward model would evaluate the likely probability of selecting [Fe/H] < -1 stars from the GCNS in discrete bins across the HR diagram.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
The code can be installed with `pip`:
|
|
8
|
+
```
|
|
9
|
+
git clone https://github.com/imedan/snc_sf
|
|
10
|
+
cd snc_sf
|
|
11
|
+
pip install .
|
|
12
|
+
```
|
|
13
|
+
or in a fresh virtual environment with `poetry` to fully replicate the development environment
|
|
14
|
+
```
|
|
15
|
+
git clone https://github.com/imedan/snc_sf
|
|
16
|
+
cd snc_sf
|
|
17
|
+
conda create -n "snc_sf" python=3.11
|
|
18
|
+
conda activate snc_sf
|
|
19
|
+
pip install poetry
|
|
20
|
+
poetry install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To fully utilize the code, SDSS-V and GCNS data is need. The SDSS-V DR19 `astra` summary data can be accessed [here](https://data.sdss.org/sas/dr19/spectro/astra/0.6.0/summary/). The required GCNS data will automatically be downloaded when first initializing a `snc_sf.selection_function.SNCSelectionFunction()` object.
|
|
24
|
+
|
|
25
|
+
## Examples
|
|
26
|
+
|
|
27
|
+
A number of examples using the code are located in [`notebooks/`](notebooks/). These examples are fully explained in the paper accompanying this work. In summary, the examples included related to the following:
|
|
28
|
+
- [`dr19_obs.ipynb`](notebooks/dr19_obs.ipynb): Constructs the base SNC 100 pc dataset and creates the plots in Figure 1 of the paper.
|
|
29
|
+
- [`SF_example_plots.ipynb`](notebooks/SF_example_plots.ipynb): Example of the selecrtion function for different regions on the sky, as shown in Figure 2 of the paper.
|
|
30
|
+
- [`forward_model_GCNS_mock_proof.py`](notebooks/forward_model_GCNS_mock_proof.py): Script that replicates the example with mock data in Section 4.1 and Figure 3 in the paper.
|
|
31
|
+
- [`forward_model_halpha_ex.py`](notebooks/forward_model_halpha_ex.py): Script that replicates the example examing the distribution of H-alpha emitters across the HR diagram in Section 4.2 and Figure 4 in the paper.
|
|
32
|
+
- [`forward_model_mass_dens.py`](notebooks/forward_model_mass_dens.py): Script that replicates the example that calculates the stellar number desnity in bins of metallicity and mass in Section 4.3 in the paper.
|
|
33
|
+
- [`appendix_example.ipynb`](notebooks/appendix_example.ipynb): Example to demonstrate basic code usage as illustrated in the Appendix of the paper.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "snc-sf"
|
|
3
|
+
version = "1.1.1"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "imedan",email = "ilija.medan@gmail.com"}
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"astropy (>=6.1.0)",
|
|
12
|
+
"numpy (>=2.0.0)",
|
|
13
|
+
"matplotlib (>=3.8.0)",
|
|
14
|
+
"healpy (>=1.17.1)",
|
|
15
|
+
"tqdm (>=4.60.0)",
|
|
16
|
+
"sdss-semaphore (>=0.2.4,<0.3.0)",
|
|
17
|
+
"gaiaunlimited (>=0.3.2,<0.4.0)",
|
|
18
|
+
"polars (>=1.0.0)",
|
|
19
|
+
"jax (>=0.4.27)",
|
|
20
|
+
"jaxlib (>=0.4.27)",
|
|
21
|
+
"notebook (>=7.0.0)",
|
|
22
|
+
"numpyro (>=0.14.0)",
|
|
23
|
+
"pyarrow (>=14.0.0)",
|
|
24
|
+
"astroquery (>=0.4.7)",
|
|
25
|
+
"scipy (>=1.13.0)",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[tool.poetry]
|
|
29
|
+
packages = [{include = "snc_sf", from = "src"}]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
34
|
+
build-backend = "poetry.core.masonry.api"
|
|
35
|
+
|
|
36
|
+
[tool.poetry.group.dev.dependencies]
|
|
37
|
+
pytest = ">=8.0.0"
|
|
File without changes
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import jax.numpy as jnp
|
|
3
|
+
from jax.scipy.special import logsumexp
|
|
4
|
+
from jax.nn import sigmoid
|
|
5
|
+
from jax.experimental.sparse import BCOO
|
|
6
|
+
try:
|
|
7
|
+
from jaxlib._jax import ArrayImpl
|
|
8
|
+
except ModuleNotFoundError:
|
|
9
|
+
from jaxlib.xla_extension import ArrayImpl
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@jax.jit
|
|
13
|
+
def compute_single_loglike(p: ArrayImpl, A_jk_bcoo: BCOO,
|
|
14
|
+
S: ArrayImpl, idx_mod: ArrayImpl) -> float:
|
|
15
|
+
"""
|
|
16
|
+
Compute the log likelihood for a single posterior draw
|
|
17
|
+
of the selection function
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
p: jaxlib._jax.ArrayImpl
|
|
22
|
+
Probability of a subpopulation of GCNS stars being in bins
|
|
23
|
+
across the HR diagram. This is a 1D array raveled
|
|
24
|
+
from a 2D array of G - RP vs M_G
|
|
25
|
+
|
|
26
|
+
A_jk_bcoo: jax.experimental.sparse.BCOO
|
|
27
|
+
Sparse matrix of the sum of the selection function values
|
|
28
|
+
for the intersection of each selection function bin with
|
|
29
|
+
each HR diagram bin. Should be of shape
|
|
30
|
+
(1D ravel of selection bins, 1D ravel of HR diagram bins)
|
|
31
|
+
|
|
32
|
+
S: jaxlib._jax.ArrayImpl
|
|
33
|
+
Selection function values for the obserbed data in a subpopulation.
|
|
34
|
+
|
|
35
|
+
idx_mod: jaxlib._jax.ArrayImpl
|
|
36
|
+
The HR diagram 1D raveled indexes on the HR diagram of the observed
|
|
37
|
+
data in the subpopulation
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
--------
|
|
41
|
+
log_like: float
|
|
42
|
+
The poisson point process log likelihood
|
|
43
|
+
"""
|
|
44
|
+
Z = A_jk_bcoo @ p
|
|
45
|
+
mask = (S > 0)
|
|
46
|
+
p_obs = p[idx_mod] * S
|
|
47
|
+
safe_p_obs = jnp.where(mask, jnp.maximum(p_obs, 1e-300), 1.0)
|
|
48
|
+
log_like_obs = jnp.sum(jnp.log(safe_p_obs))
|
|
49
|
+
log_like_norm = jnp.sum(Z)
|
|
50
|
+
log_like = log_like_obs - log_like_norm
|
|
51
|
+
return log_like
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def objective_jax_multi(theta: ArrayImpl, S_data: ArrayImpl, A_jks_bcoo: tuple,
|
|
55
|
+
idx_mod_data: ArrayImpl) -> float:
|
|
56
|
+
"""
|
|
57
|
+
Compute the total log likelihood
|
|
58
|
+
|
|
59
|
+
Parameters
|
|
60
|
+
----------
|
|
61
|
+
theta: jaxlib._jax.ArrayImpl
|
|
62
|
+
The sigmoid of theta gives the probability of a subpopulation
|
|
63
|
+
of GCNS stars being in bins
|
|
64
|
+
across the HR diagram. This is a 1D array raveled
|
|
65
|
+
from a 2D array of G - RP vs M_G.
|
|
66
|
+
|
|
67
|
+
S_data: jaxlib._jax.ArrayImpl
|
|
68
|
+
Selection function values for the obserbed data in a subpopulation.
|
|
69
|
+
|
|
70
|
+
A_jks_bcoo: tuple
|
|
71
|
+
Sparse matrix of the sum of the selection function values
|
|
72
|
+
for the intersection of each selection function bin with
|
|
73
|
+
each HR diagram bin. Should be of shape
|
|
74
|
+
(1D ravel of selection bins, 1D ravel of HR diagram bins). This should
|
|
75
|
+
be a tuple where each index is for a different posterior draw
|
|
76
|
+
|
|
77
|
+
idx_mod_data: jaxlib._jax.ArrayImpl
|
|
78
|
+
The HR diagram 1D raveled indexes on the HR diagram of the observed
|
|
79
|
+
data in the subpopulation
|
|
80
|
+
|
|
81
|
+
Returns
|
|
82
|
+
--------
|
|
83
|
+
neg_log_like: float
|
|
84
|
+
The total poisson point process negative log likelihood
|
|
85
|
+
"""
|
|
86
|
+
p = sigmoid(theta)
|
|
87
|
+
p = jnp.nan_to_num(p, nan=0, posinf=1, neginf=0)
|
|
88
|
+
|
|
89
|
+
log_like_samples = []
|
|
90
|
+
for i in range(len(A_jks_bcoo)):
|
|
91
|
+
log_like_samples.append(compute_single_loglike(p, A_jks_bcoo[i],
|
|
92
|
+
S_data[:, i], idx_mod_data))
|
|
93
|
+
log_like_samples = jnp.stack(log_like_samples)
|
|
94
|
+
|
|
95
|
+
log_like_samples = jnp.where(jnp.isfinite(log_like_samples), log_like_samples, -1e10)
|
|
96
|
+
|
|
97
|
+
neg_log_like = -1.0 * (logsumexp(log_like_samples) - jnp.log(len(A_jks_bcoo)))
|
|
98
|
+
return neg_log_like
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@jax.jit
|
|
102
|
+
def sigmoid_inv(y: ArrayImpl):
|
|
103
|
+
"""
|
|
104
|
+
Inverse of the sigmoid
|
|
105
|
+
"""
|
|
106
|
+
return jnp.log(y / (1 - y))
|