Skip to content

Commit 7ff1259

Browse files
Merge pull request #23 from CosmoStat/develop
Release v.0.0.4
2 parents 312518b + c16b09a commit 7ff1259

22 files changed

Lines changed: 485 additions & 491 deletions

.github/pull_request_template.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Summary
2+
3+
> Developers should provide a summary of the proposed changes here and include "closes #<ISSUE NUMBER>" if this addresses an open issue.
4+
5+
## Reviewer Checklist
6+
7+
> Reviewers should tick the following boxes before approving and merging the PR.
8+
9+
- [ ] The PR is assigned to the developer
10+
- [ ] The PR has appropriate labels
11+
- [ ] The PR is included in appropriate projects and/or milestones
12+
- [ ] The PR includes a clear description of the proposed changes
13+
- [ ] If the PR addresses an open issue the description includes "closes #<ISSUE NUMBER>"
14+
- [ ] The code and documentation style match the current standards
15+
- [ ] Documentation has been added/updated consistently with the code
16+
- [ ] All CI tests are passing
17+
- [ ] All changed files have been checked and comments provided to the developer
18+
- [ ] All of the reviewer's comments have been satisfactorily addressed by the developer

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ Utility library for CosmoStat
2222

2323
### Library
2424

25-
### Scripts
25+
- Galaxy catalogue handling
26+
- Weak-lensing related cosmological quantities (e.g. surface mass density)
27+
- VOS (Virtual Observatory Software)
28+
- Command line logging
29+
- Plotting
30+
- Basic statistic calculations
31+
- UNIONS/CFIS weak-lensing survey handling

cs_util/__init__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""cs_util PACKAGE.
22
3-
Provide a basic description of what your package contains.
3+
CosmoStat utilities for weak lensing and cosmology.
44
55
References
66
----------
@@ -23,14 +23,4 @@
2323

2424
from warnings import warn
2525

26-
from importlib_metadata import version
27-
28-
try:
29-
_version = version('cs_util')
30-
except Exception: # pragma: no cover
31-
_version = 'Unkown'
32-
warn(
33-
'Could not extract package metadata. Make sure the package is '
34-
+ 'correctly installed.',
35-
)
36-
__version__ = _version
26+
__version__ = "0.0.4"

cs_util/calc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"""CALC.
2-
3-
:Name: calc.py
4-
1+
"""CALC.
2+
3+
:Name: calc.py
4+
55
:Description: This file contains methods for general calculations
66
and statistics.
7-
7+
88
:Author: Martin Kilbinger <martin.kilbinger@cea.fr>
99
1010
"""
@@ -36,10 +36,10 @@ def weighted_avg_and_std(values, weights, corrected=False):
3636
average = np.average(values, weights=weights)
3737

3838
# Fast and numerically precise:
39-
variance = np.average((values-average)**2, weights=weights)
40-
39+
variance = np.average((values - average) ** 2, weights=weights)
40+
4141
if corrected:
4242
n = len(values)
43-
variance = variance * n / (n - 1)
43+
variance = variance * n / (n - 1)
4444

4545
return average, np.sqrt(variance)

cs_util/canfar.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class vosHandler:
3838
"""
3939

4040
def __init__(self, command):
41-
4241
self._avail_commands = tuple(vosc.__all__)
4342
self.command = command
4443

@@ -58,10 +57,9 @@ def command(self):
5857

5958
@command.setter
6059
def command(self, value):
61-
6260
if value not in self._avail_commands:
6361
raise ValueError(
64-
f'vos command must be one of {self._avail_commands}'
62+
f"vos command must be one of {self._avail_commands}"
6563
)
6664

6765
self._command = getattr(vosc, value)
@@ -81,9 +79,7 @@ def __call__(self, *args, **kwargs):
8179
self._command()
8280

8381
except Exception:
84-
raise vosError(
85-
f'Error in VOs command: {self._command.__name__}'
86-
)
82+
raise vosError(f"Error in VOs command: {self._command.__name__}")
8783

8884

8985
def download(source, target, verbose=False):
@@ -106,20 +102,20 @@ def download(source, target, verbose=False):
106102
status, True/False or success/failure
107103
108104
"""
109-
cmd = 'vcp'
105+
cmd = "vcp"
110106

111107
if not os.path.exists(target):
112108
sys.argv = [cmd, source, target]
113109
if verbose:
114-
print(f'Downloading file {source} to {target}...')
110+
print(f"Downloading file {source} to {target}...")
115111
vcp = vosHandler(cmd)
116112

117113
vcp()
118114
if verbose:
119-
print('Download finished.')
115+
print("Download finished.")
120116
else:
121117
if verbose:
122-
print(f'Target file {target} exists, skipping download.')
118+
print(f"Target file {target} exists, skipping download.")
123119

124120

125121
def dir_list(path, verbose=False):
@@ -145,22 +141,22 @@ def dir_list(path, verbose=False):
145141
file or directory at path, type is str
146142
147143
"""
148-
cmd = 'vls'
144+
cmd = "vls"
149145
sys.argv = [cmd, path]
150146
vls = vosHandler(cmd)
151147

152148
if verbose:
153-
print('Getting vos directory content from vls...')
149+
print("Getting vos directory content from vls...")
154150

155151
f = StringIO()
156152

157153
try:
158154
with redirect_stdout(f):
159155
vls()
160156
except Exception:
161-
print('Error during vls command')
157+
print("Error during vls command")
162158
raise
163159

164160
vls_out = f.getvalue()
165161

166-
return vls_out.split('\n')
162+
return vls_out.split("\n")

cs_util/cat.py

Lines changed: 88 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"""
1010

1111
import os
12-
from datetime import datetime
12+
from datetime import datetime
1313
from astropy.io import fits
14+
from astropy.io import ascii
1415

1516

16-
def write_header_info_sp(primary_header, name='unknown', version='unknown'):
17+
def write_header_info_sp(primary_header, name="unknown", version="unknown"):
1718
"""Write Header Info sp_validation.
1819
1920
Write information about software and run to FITS header
@@ -33,16 +34,16 @@ def write_header_info_sp(primary_header, name='unknown', version='unknown'):
3334
updated FITS header information
3435
3536
"""
36-
if 'USER' in os.environ:
37-
author = os.environ['USER']
37+
if "USER" in os.environ:
38+
author = os.environ["USER"]
3839
else:
39-
author = 'unknown'
40-
primary_header['AUTHOR'] = (author, 'Who ran the software')
41-
primary_header['SOFTNAME'] = (name, 'Name of the software')
42-
primary_header['SOFTVERS'] = (version, 'Version of the software')
43-
primary_header['DATE'] = (
44-
datetime.now().strftime('%Y-%m-%d_%H-%M-%S'),
45-
'When it was started',
40+
author = "unknown"
41+
primary_header["AUTHOR"] = (author, "Who ran the software")
42+
primary_header["SOFTNAME"] = (name, "Name of the software")
43+
primary_header["SOFTVERS"] = (version, "Version of the software")
44+
primary_header["DATE"] = (
45+
datetime.now().strftime("%Y-%m-%d_%H-%M-%S"),
46+
"When it was started",
4647
)
4748

4849
return primary_header
@@ -68,56 +69,41 @@ def add_shear_bias_to_header(primary_header, R, R_shear, R_select, c):
6869
additive bias
6970
7071
"""
71-
primary_header['R'] = (
72-
r'<R>',
73-
r'Mean full response <R_shear> + <R_select>'
72+
primary_header["R"] = (r"<R>", r"Mean full response <R_shear> + <R_select>")
73+
primary_header["R_11"] = (R[0, 0], "Full response matrix comp 1 1")
74+
primary_header["R_12"] = (R[0, 1], "Full response matrix comp 1 2")
75+
primary_header["R_21"] = (R[1, 0], "Full response matrix comp 2 1")
76+
primary_header["R_22"] = (R[1, 1], "Full response matrix comp 2 2")
77+
78+
primary_header["R_g"] = (r"<R_g>", r"Mean shear response matrix <R_shear>")
79+
primary_header["R_g11"] = (R_shear[0, 0], "Mean shear resp matrix comp 1 1")
80+
primary_header["R_g12"] = (R_shear[0, 1], "Mean shear resp matrix comp 1 2")
81+
primary_header["R_g21"] = (R_shear[1, 0], "Mean shear resp matrix comp 2 1")
82+
primary_header["R_g22"] = (R_shear[1, 1], "Mean shear resp matrix comp 2 2")
83+
84+
primary_header["R_S"] = (
85+
r"<R_S>",
86+
r"Global selection response matrix <R_select>",
7487
)
75-
primary_header['R_11'] = (R[0, 0], 'Full response matrix comp 1 1')
76-
primary_header['R_12'] = (R[0, 1], 'Full response matrix comp 1 2')
77-
primary_header['R_21'] = (R[1, 0], 'Full response matrix comp 2 1')
78-
primary_header['R_22'] = (R[1, 1], 'Full response matrix comp 2 2')
79-
80-
primary_header['R_g'] = (r'<R_g>', r'Mean shear response matrix <R_shear>')
81-
primary_header['R_g11'] = (
82-
R_shear[0, 0],
83-
'Mean shear resp matrix comp 1 1'
84-
)
85-
primary_header['R_g12'] = (
86-
R_shear[0, 1],
87-
'Mean shear resp matrix comp 1 2'
88-
)
89-
primary_header['R_g21'] = (
90-
R_shear[1, 0],
91-
'Mean shear resp matrix comp 2 1'
92-
)
93-
primary_header['R_g22'] = (
94-
R_shear[1, 1],
95-
'Mean shear resp matrix comp 2 2'
96-
)
97-
98-
primary_header['R_S'] = (
99-
r'<R_S>',
100-
r'Global selection response matrix <R_select>'
101-
)
102-
primary_header['R_S11'] = (
88+
primary_header["R_S11"] = (
10389
R_select[0, 0],
104-
'Global selection resp matrix comp 1 1'
90+
"Global selection resp matrix comp 1 1",
10591
)
106-
primary_header['R_S12'] = (
92+
primary_header["R_S12"] = (
10793
R_select[0, 1],
108-
'Global selection resp matrix comp 1 2'
94+
"Global selection resp matrix comp 1 2",
10995
)
110-
primary_header['R_S21'] = (
96+
primary_header["R_S21"] = (
11197
R_select[1, 0],
112-
'Global selection resp matrix comp 2 1'
98+
"Global selection resp matrix comp 2 1",
11399
)
114-
primary_header['R_S22'] = (
100+
primary_header["R_S22"] = (
115101
R_select[1, 1],
116-
'Global selection resp matrix comp 2 2'
102+
"Global selection resp matrix comp 2 2",
117103
)
118104

119-
primary_header['c_1'] = (c[0], 'Additive bias 1st comp')
120-
primary_header['c_2'] = (c[1], 'Additive bias 2nd comp')
105+
primary_header["c_1"] = (c[0], "Additive bias 1st comp")
106+
primary_header["c_2"] = (c[1], "Additive bias 2nd comp")
121107

122108

123109
def write_fits_BinTable_file(
@@ -159,3 +145,54 @@ def write_fits_BinTable_file(
159145

160146
hdu_list = fits.HDUList([primary_hdu, table_hdu])
161147
hdu_list.writeto(output_path, overwrite=True)
148+
149+
150+
def bin_edges2centers(bin_edges):
151+
"""Bin Edges To Centers.
152+
153+
Transform bin edge values to central values.
154+
155+
Parameters
156+
----------
157+
bin_edges : list
158+
bin edge values
159+
160+
Returns
161+
-------
162+
list
163+
bin central values
164+
165+
"""
166+
bin_means = 0.5 * (bin_edges[1:] + bin_edges[:-1])
167+
168+
return bin_means
169+
170+
171+
def read_dndz(file_path):
172+
"""Read Dndz.
173+
174+
Read redshift histogram from file.
175+
176+
Parameters
177+
----------
178+
file_path : str
179+
input file path
180+
181+
Returns
182+
-------
183+
list :
184+
redshift bin centers
185+
list :
186+
number densities
187+
list :
188+
redshift bin edges
189+
190+
"""
191+
dat = ascii.read(file_path, format="commented_header")
192+
193+
# Remove last n(z) value which is zero, to match bin centers
194+
nz = dat["dn_dz"][:-1]
195+
z_edges = dat["z"]
196+
z_centers = bin_edges2centers(z_edges)
197+
198+
return z_centers, nz, z_edges

0 commit comments

Comments
 (0)