pz-rail-astro-tools 0.0.1__py3-none-any.whl

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.

Potentially problematic release.


This version of pz-rail-astro-tools might be problematic. Click here for more details.

@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 LSST Dark Energy Science Collaboration (DESC)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.1
2
+ Name: pz-rail-astro-tools
3
+ Version: 0.0.1
4
+ Author-email: "LSST Dark Energy Science Collaboration (DESC)" <later@later.com>
5
+ License: MIT License
6
+
7
+ Copyright (c) 2023 LSST Dark Energy Science Collaboration (DESC)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+
27
+ Classifier: Development Status :: 4 - Beta
28
+ Classifier: License :: OSI Approved :: MIT License
29
+ Classifier: Intended Audience :: Developers
30
+ Classifier: Intended Audience :: Science/Research
31
+ Classifier: Operating System :: OS Independent
32
+ Classifier: Programming Language :: Python
33
+ Description-Content-Type: text/markdown
34
+ License-File: LICENSE
35
+ Requires-Dist: deprecated
36
+ Requires-Dist: pz-rail-base
37
+ Requires-Dist: astropy
38
+ Requires-Dist: healpy
39
+ Requires-Dist: photerr
40
+ Requires-Dist: dustmaps
41
+ Requires-Dist: pz-hyperbolic-temp
42
+ Provides-Extra: dev
43
+ Requires-Dist: tables-io[full] ; extra == 'dev'
44
+ Requires-Dist: pytest ; extra == 'dev'
45
+ Requires-Dist: pytest-cov ; extra == 'dev'
46
+ Requires-Dist: pre-commit ; extra == 'dev'
47
+ Requires-Dist: pylint ; extra == 'dev'
48
+
49
+ # pz-rail-astro-tools
50
+
51
+ [![Template](https://img.shields.io/badge/Template-LINCC%20Frameworks%20Python%20Project%20Template-brightgreen)](https://lincc-ppt.readthedocs.io/en/latest/)
52
+ [![codecov](https://codecov.io/gh/LSSTDESC/pz-rail-astro-tools/branch/main/graph/badge.svg)](https://codecov.io/gh/LSSTDESC/pz-rail-astro-tools)
53
+ [![PyPI](https://img.shields.io/pypi/v/astro_tools?color=blue&logo=pypi&logoColor=white)](https://pypi.org/project/astro_tools/)
54
+
55
+ TODO - add more about your project here.
56
+
57
+ ## RAIL: Redshift Assessment Infrastructure Layers
58
+
59
+ This package is part of the larger ecosystem of Photometric Redshifts
60
+ in [RAIL](https://github.com/LSSTDESC/RAIL).
61
+
62
+ ### Citing RAIL
63
+
64
+ This code, while public on GitHub, has not yet been released by DESC and is
65
+ still under active development. Our release of v1.0 will be accompanied by a
66
+ journal paper describing the development and validation of RAIL.
67
+
68
+ If you make use of the ideas or software in RAIL, please cite the repository
69
+ <https://github.com/LSSTDESC/RAIL>. You are welcome to re-use the code, which
70
+ is open source and available under terms consistent with the MIT license.
71
+
72
+ External contributors and DESC members wishing to use RAIL for non-DESC projects
73
+ should consult with the Photometric Redshifts (PZ) Working Group conveners,
74
+ ideally before the work has started, but definitely before any publication or
75
+ posting of the work to the arXiv.
76
+
77
+ ### Citing this package
78
+
79
+ If you use this package, you should also cite the appropriate papers for each
80
+ code used. A list of such codes is included in the
81
+ [Citing RAIL](https://lsstdescrail.readthedocs.io/en/stable/source/citing.html)
82
+ section of the main RAIL Read The Docs page.
83
+
@@ -0,0 +1,17 @@
1
+ rail/astro_tools/__init__.py,sha256=BTXEytC6N25I1t9oH2g0qUAYKcXEhLKGSPaVcL58_Ew,328
2
+ rail/astro_tools/_version.py,sha256=0E2q3bbXSYHV7qQaXVj0To-J2XdlP2Roo0TsM8oQtR4,160
3
+ rail/creation/degradation/grid_selection.py,sha256=aJ8tJsR9DiqgEhDEUKTo-O0T1uQXlN119yji8tT9dYU,11974
4
+ rail/creation/degradation/observing_condition_degrader.py,sha256=iacZu54A4y4wSFy7n3nz3IZEMmtak9OADrvtWZOnRKE,15697
5
+ rail/creation/degradation/spectroscopic_degraders.py,sha256=-Qko0oFVjPSnC5RKIJ7InMsVJkPAUnt3lRnuhTFWTzI,4935
6
+ rail/creation/degradation/spectroscopic_selections.py,sha256=f-87GGSDIORaupuU-7TZG_IgNjTIWQS1hx6BbTz-7Yc,22251
7
+ rail/examples_data/creation_data/data/hsc_ratios_and_specz.hdf5,sha256=liH54wuuuHxToqjoAwQ_fFmEefk3haNweVThMwdDKN4,13823712
8
+ rail/examples_data/creation_data/data/survey_conditions/DC2-dr6-galcounts-i20-i25.3-nside-128.fits,sha256=jHv3dwoLQSKcGUx29cnN9t9AWef4JlGsqSbITJfZI0o,1581120
9
+ rail/examples_data/creation_data/data/survey_conditions/DC2-mask-neg-nside-128.fits,sha256=79Kg4xRP4WHlOCcS-3SKqf9aP_LZRAq-izIM0DFL6bw,1581120
10
+ rail/examples_data/creation_data/data/survey_conditions/minion_1016_dc2_Median_airmass_i_and_nightlt1825_HEAL.fits,sha256=twvyce70G3h1MQnGGFQJG9wZc8UbuNqXc6mjtiuaL1s,1581120
11
+ rail/examples_data/creation_data/data/survey_conditions/minion_1016_dc2_Median_fiveSigmaDepth_i_and_nightlt1825_HEAL.fits,sha256=RbMMFSVJQxGhZnL4BnISE9VTNljfTZwV6Bax6NxverY,1581120
12
+ rail/tools/utilPhotometry.py,sha256=oRxBSNyBaiRuaSbf52w16W_vXLA8cAkQYLmdRRt_Ro8,17806
13
+ pz_rail_astro_tools-0.0.1.dist-info/LICENSE,sha256=tLMEN21HbzvT-7umOVVvPjaJZbQxCa-2bAeYLwG5Q04,1102
14
+ pz_rail_astro_tools-0.0.1.dist-info/METADATA,sha256=YeeWdosykH3Ia79tfn5IQzn6W22hb04QpxJiwKiG2I8,3863
15
+ pz_rail_astro_tools-0.0.1.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
16
+ pz_rail_astro_tools-0.0.1.dist-info/top_level.txt,sha256=6R6sqn-85I8YZTzmB9gv7sqckB2QaOTHbu-QLGTWBRE,5
17
+ pz_rail_astro_tools-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.40.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ rail
@@ -0,0 +1,7 @@
1
+ from ._version import __version__
2
+
3
+ from rail.creation.degradation.grid_selection import *
4
+ from rail.creation.degradation.observing_condition_degrader import *
5
+ from rail.creation.degradation.spectroscopic_degraders import *
6
+ from rail.creation.degradation.spectroscopic_selections import *
7
+ from rail.tools.utilPhotometry import *
@@ -0,0 +1,4 @@
1
+ # file generated by setuptools_scm
2
+ # don't change, don't track in version control
3
+ __version__ = version = '0.0.1'
4
+ __version_tuple__ = version_tuple = (0, 0, 1)
@@ -0,0 +1,212 @@
1
+ """Degraders that emulate spectroscopic effects on photometry"""
2
+
3
+ import os
4
+ import numpy as np
5
+ import pandas as pd
6
+ import pickle
7
+ import tables_io
8
+ from rail.creation.degrader import Degrader
9
+ from rail.core.utils import find_rail_file
10
+ from ceci.config import StageParameter as Param
11
+
12
+
13
+ class GridSelection(Degrader):
14
+ """
15
+ Uses the ratio of HSC spectroscpic galaxies to photometric galaxies to portion a sample
16
+ into training and application samples. Option to implement a color-based redshift cut off in each pixel.
17
+ Option of further degrading the training sample by limiting it to galaxies less than a redshift cutoff by specifying redshift_cut.
18
+
19
+ Parameters
20
+ ----------
21
+ color_redshift_cut: True or false, implements color-based redshift cut. Default is True.
22
+ If True, ratio_file must include second key called 'data' with magnitudes, colors and spec-z from the spectroscopic sample.
23
+ percentile_cut: If using color-based redshift cut, percentile in spec-z above which redshifts will be cut from training sample. Default is 99.0
24
+ scaling_factor: Enables the user to adjust the ratios by this factor to change the overall number of galaxies kept. For example, if you wish
25
+ to generate 100,00 galaxies but only 50,000 are selected by default, then you can adjust factor up by a factor of 2 to return more galaixes.
26
+ redshift_cut: redshift above which all galaxies will be removed from training sample. Default is 100
27
+ ratio_file: hdf5 file containing an array of spectroscpic vs. photometric galaxies in each pixel. Default is hsc_ratios.hdf5 for an HSC based selection
28
+ settings_file: pickled dictionary containing information about colors and magnitudes used in defining the pixels. Dictionary must include the following keys:
29
+ 'x_band_1': string, this is the band used for the magnitude in the color magnitude diagram. Default for HSC is 'i'.
30
+ 'x_band_2': string, this is the redder band used for the color in the color magnitude diagram.
31
+ if x_band_2 string is not set to '' then the grid is assumed to be over color and x axis color is set to x_band_1 - x_band_2, default is ''.
32
+ 'y_band_1': string, this is the bluer band used for the color in the color magnitude grid. Default for HSC is 'g'.
33
+ 'y_band_2': string, this is the redder band used for the color in the color magnitude diagram.
34
+ if y_band_2 is not set to '' then the y-band is assumed to be over color and is set to y_band_1 - y_band 2.
35
+ 'x_limits': 2-element list, this is a list of the lower and upper limits of the magnitude. Default for HSC is [13, 16],
36
+ 'y_limits': 2-element list, this is a list of the lower and upper limits of the color. Default for HSC is [-2, 6]}
37
+
38
+ NOTE: the default 'HSC' grid file, located in rail/examples_data/creation_data/data/hsc_ratios_and_specz.hdf5, is based on data from the
39
+ Second HSC Data Release, details of which can be found here:
40
+ Aihara, H., AlSayyad, Y., Ando, M., et al. 2019, PASJ, 71, 114
41
+ doi: 10.1093/pasj/psz103
42
+ """
43
+ def_ratio_file = find_rail_file("examples_data/creation_data/data/hsc_ratios_and_specz.hdf5")
44
+ def_set_file = find_rail_file("examples_data/creation_data/data/HSC_grid_settings.pkl" )
45
+
46
+ name = 'GridSelection'
47
+ config_options = Degrader.config_options.copy()
48
+ config_options.update(color_redshift_cut=Param(bool, True, msg='using color-based redshift cut'),
49
+ percentile_cut=Param(float, 99.0, msg='percentile cut-off for each pixel in color-based redshift cut off'),
50
+ redshift_cut=Param(float, 100.0, msg="cut redshifts above this value"),
51
+ ratio_file=Param(str, def_ratio_file,
52
+ msg="path to ratio file"),
53
+ settings_file=Param(str, def_set_file,
54
+ msg='path to pickled parameters file'),
55
+ random_seed=Param(int, 12345, msg="random seed for reproducibility"),
56
+ scaling_factor=Param(float, 1.588, msg="multiplicative factor for ratios to adjust number of galaxies kept"))
57
+
58
+ def __init__(self, args, comm=None):
59
+
60
+ Degrader.__init__(self, args, comm=comm)
61
+
62
+ if self.config.redshift_cut < 0:
63
+ raise ValueError("redshift cut must be positive")
64
+ if (self.config.percentile_cut < 0) | (self.config.percentile_cut > 100):
65
+ raise ValueError('percentile cut off must be between 0 and 100')
66
+
67
+ def run(self):
68
+ """
69
+ HSC galaxies were binned in color magnitude space with i-band mag from -2 to 6 and g-z color from 13 to 26
70
+ 200 bins in each direction. The ratio of of galaxies with spectroscopic redshifts (training galaxies) to
71
+ galaxies with only photometry in HSC wide field (application galaxies) was computed for each pixel. We divide
72
+ the data into the same pixels and randomly select galaxies into the training sample based on the HSC ratios.
73
+ If using a color-based redshift cut, galaxies with redshifts > the percentile cut are removed from the sample
74
+ before making the random selection.
75
+ """
76
+ rng = np.random.default_rng(seed=self.config.random_seed)
77
+
78
+ data = self.get_data('input')
79
+ with open(self.config.settings_file, 'rb') as handle:
80
+ settings = pickle.load(handle)
81
+ # check settings values
82
+ check_vals = ['x_band_1', 'x_band_2', 'y_band_1', 'y_band_2', 'x_limits', 'y_limits']
83
+ for val in check_vals:
84
+ if val not in settings.keys(): # pragma: no cover
85
+ raise KeyError(f"required key {val} not present in {self.config.settings_file}!")
86
+ for val in check_vals[:-2]:
87
+ if settings[val] != '' and settings[val] not in data.keys(): # pragma: no cover
88
+ raise KeyError(f"column {settings[val]} not present in data file!")
89
+
90
+ if settings['x_band_2'] == '':
91
+ data['x_vals'] = data[settings['x_band_1']].to_numpy()
92
+ else: # pragma: no cover
93
+ data['x_vals'] = (data[settings['x_band_1']] - data[settings['x_band_2]']]).to_numpy()
94
+
95
+ if settings['y_band_2'] == '': # pragma: no cover
96
+ data['y_vals'] = data[settings['y_band_1']].to_numpy()
97
+ else:
98
+ data['y_vals'] = (data[settings['y_band_1']] - data[settings['y_band_2']]).to_numpy()
99
+
100
+ # now remove galaxies that don't fall in i-mag and g-z color range of HSC data. These will always be application sample galaxies
101
+ x_lims = settings['x_limits']
102
+ y_lims = settings['y_limits']
103
+ if len(x_lims) != 2: # pragma: no cover
104
+ raise ValueError("x limits should have two elements!")
105
+ if len(y_lims) != 2: # pragma: no cover
106
+ raise ValueError("y limits should have two elements!")
107
+ data_hsc_like = data[(data['x_vals'] >= x_lims[0]) & (data['x_vals'] <= x_lims[1]) & (data['y_vals'] >= y_lims[0]) & (data['y_vals'] <= y_lims[1])]
108
+
109
+ x_vals = data_hsc_like['x_vals'].to_numpy()
110
+ y_vals = data_hsc_like['y_vals'].to_numpy()
111
+
112
+ # Opens file with HSC ratios
113
+ ratios = tables_io.read(self.config.ratio_file)['ratios']
114
+
115
+ # Sets pixel edges to be the same as were used to calculate HSC ratios
116
+ num_edges_y = ratios.shape[1] + 1
117
+ num_edges_x = ratios.shape[0] + 1
118
+ y_edges = np.linspace(y_lims[0], y_lims[1], num_edges_y)
119
+ x_edges = np.linspace(x_lims[0], x_lims[1], num_edges_x)
120
+
121
+ # Calculate the max spec-z for each pixel from percentile_cut
122
+ # If not using color-based redshift cut, max spec-z set to 100
123
+ # percentiles are determined from the HSC data, not the input
124
+ # data, which gives smoother distributions in many cases.
125
+ if not self.config.color_redshift_cut: # pragma: no cover
126
+ max_specz = np.ones_like(ratios) * 100.
127
+ else:
128
+ percentile_cut = self.config['percentile_cut']
129
+ hsc_spec_galaxies = pd.read_hdf(self.config['ratio_file'], 'data', 'r')
130
+
131
+ hsc_spec_mags = hsc_spec_galaxies['mag'].to_numpy()
132
+ hsc_spec_colors = hsc_spec_galaxies['color'].to_numpy()
133
+
134
+ pixels_hsc_colors = np.searchsorted(y_edges, hsc_spec_colors) - 1
135
+ pixels_hsc_mags = np.searchsorted(x_edges, hsc_spec_mags) - 1
136
+
137
+ pixel_tot = (len(x_edges) - 1) * pixels_hsc_mags + pixels_hsc_colors
138
+ hsc_spec_galaxies['total_pixel'] = pixel_tot # tags each galaxy with a single pixel number instead of one color and one magnitude
139
+
140
+ unique_pixels = hsc_spec_galaxies['total_pixel'].unique()
141
+
142
+ max_specz = np.zeros((len(x_edges) - 1, len(y_edges) - 1))
143
+ for i in range(len(x_edges) - 1):
144
+ for j in range(len(y_edges) - 1):
145
+ pixel = i * int(len(x_edges) - 1) + j
146
+ if np.isin(pixel, unique_pixels):
147
+ temp_dict = hsc_spec_galaxies[hsc_spec_galaxies['total_pixel'] == pixel]
148
+ spec_zs = temp_dict['specz'].to_numpy()
149
+ percentile = np.percentile(spec_zs, percentile_cut)
150
+ max_specz[i][j] = percentile
151
+
152
+ # For each galaxy in data, identifies the pixel it belongs in, and adds the ratio for that pixel to a new column in data called 'ratios'
153
+ pixels_y = np.searchsorted(y_edges, y_vals) - 1
154
+ pixels_x = np.searchsorted(x_edges, x_vals) - 1
155
+
156
+ ratio_list = []
157
+ max_specz_list = []
158
+ for pix_x, pix_y in zip(pixels_x, pixels_y):
159
+ ratio_list.append(ratios[pix_y][pix_x])
160
+ max_specz_list.append(max_specz[pix_y][pix_x])
161
+
162
+ data_hsc_like['ratios'] = ratio_list
163
+ data_hsc_like['max_specz'] = max_specz_list
164
+
165
+ # remove galaxies with redshifts higher than the color-based cutoff
166
+ data_hsc_like_redshift_cut = data_hsc_like[data_hsc_like['redshift'] <= data_hsc_like['max_specz']]
167
+
168
+ # If making a redshift cut, do that now
169
+ if self.config['redshift_cut'] != 100: # pragma: no cover
170
+ data_hsc_like_redshift_cut = data_hsc_like_redshift_cut[data_hsc_like_redshift_cut['redshift'] <= self.config['redshift_cut']]
171
+
172
+ # This picks galaxies for the training set
173
+ unique_ratios = data_hsc_like['ratios'].unique()
174
+
175
+ keep_inds = []
176
+
177
+ if self.config.color_redshift_cut:
178
+ # multiplicative factor that can account for the fact that we select fewer galaxies
179
+ # with the color-based redshift cut after percentile cut is applied
180
+ factor = self.config.scaling_factor
181
+ else: # pragma: no cover
182
+ factor = 1
183
+ for xratio in unique_ratios:
184
+ temp_data = data_hsc_like_redshift_cut[data_hsc_like_redshift_cut['ratios'] == xratio]
185
+ number_to_keep = len(temp_data) * xratio
186
+ if number_to_keep * factor <= len(temp_data):
187
+ number_to_keep = number_to_keep * factor
188
+ else: # pragma: no cover
189
+ number_to_keep = len(temp_data)
190
+
191
+ if int(number_to_keep) != number_to_keep:
192
+ random_num = rng.uniform()
193
+ else:
194
+ random_num = 2
195
+
196
+ number_to_keep = np.floor(number_to_keep)
197
+ indices_to_list = list(temp_data.index.values)
198
+ rng.shuffle(indices_to_list)
199
+
200
+ if random_num > xratio: # pragma: no cover
201
+ for j in range(int(number_to_keep)):
202
+ keep_inds.append(indices_to_list[j])
203
+
204
+ else: # pragma: no cover
205
+ for j in range(int(number_to_keep) + 1):
206
+ keep_inds.append(indices_to_list[j])
207
+
208
+ training_data = data_hsc_like_redshift_cut.loc[keep_inds, :]
209
+ training_data = training_data[training_data['redshift'] > 0]
210
+ training_data = training_data.drop(['x_vals', 'y_vals', 'ratios'], axis=1)
211
+
212
+ self.add_data('output', training_data)