Skip to content

Commit 669e5b1

Browse files
author
martinkilbinger
committed
added neg_dash to plot; added treecorr_aux file
1 parent a8e5371 commit 669e5b1

2 files changed

Lines changed: 97 additions & 19 deletions

File tree

cs_util/plots.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def plot_data_1d(
229229
xlim=None,
230230
ylim=None,
231231
shift_x=False,
232+
neg_dash=False,
232233
close_fig=True,
233234
second_x_axis=None,
234235
second_x_label=None,
@@ -280,6 +281,25 @@ def plot_data_1d(
280281
plot only one in every `every` point on second x-axis; default is 1
281282
282283
"""
284+
# Add negative points with dashed lines
285+
if neg_dash:
286+
if not ylog:
287+
raise ValueError("neg_dash only valid if ylog is True")
288+
289+
n = len(x)
290+
291+
# Duplicate the following lists
292+
x = x * 2
293+
yerr = yerr * 2
294+
colors = colors * 2
295+
labels = labels + [""] * n
296+
297+
# Add negative y-values
298+
y = y + [-arr for arr in y]
299+
300+
# Add dashed lines
301+
linestyles = linestyles + ["--"] * n
302+
283303
if labels is None:
284304
labels = [""] * len(x)
285305
do_legend = False
@@ -302,25 +322,6 @@ def plot_data_1d(
302322
else:
303323
fig = ax.figure
304324

305-
# Add negative points with dashed lines
306-
if neg_dash:
307-
if not ylog:
308-
raise ValueError("neg_dash only valid if ylog is True")
309-
310-
n = len(x)
311-
312-
# Duplicate the following lists
313-
x = x * 2
314-
yerr = yerr * 2
315-
colors = colors * 2
316-
labels = labels + [""] * n
317-
318-
# Add negative y-values
319-
y = y + [-arr for arr in y]
320-
321-
# Add dashed lines
322-
linestyles = linestyles + ["--"] * n
323-
324325
for idx in range(len(x)):
325326
this_x = get_x_dx(x, shift_x, idx, log=xlog)
326327

cs_util/treecorr_aux.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""TREECORR_AUX.
2+
3+
:Name: treecorr_aux.py
4+
5+
:Description: This file contains methods for working with TreeCorr output files.
6+
7+
:Author: Martin Kilbinger <martin.kilbinger@cea.fr>
8+
9+
"""
10+
11+
import ast
12+
import numpy as np
13+
import treecorr
14+
15+
16+
def read_ascii_to_ngcorrelation(file_path):
17+
"""Read ASCII to NGCorrelation.
18+
19+
Read TreeCorr ASCII output file and create a NGCorrelation instance
20+
without needing a config file. All configuration parameters are
21+
extracted from the ASCII file metadata.
22+
23+
Parameters
24+
----------
25+
file_path : str
26+
path to TreeCorr ASCII output file
27+
28+
Returns
29+
-------
30+
treecorr.NGCorrelation
31+
TreeCorr NGCorrelation object with data loaded from ASCII file
32+
33+
Raises
34+
------
35+
IOError
36+
if input file is not found or cannot be read
37+
ValueError
38+
if file format is not recognized as TreeCorr ASCII output
39+
"""
40+
try:
41+
with open(file_path, 'r') as f:
42+
lines = f.readlines()
43+
except IOError as e:
44+
raise IOError(f"Cannot read file '{file_path}': {e}")
45+
46+
if not lines:
47+
raise ValueError(f"File '{file_path}' is empty")
48+
49+
# Parse the config from first line
50+
if not lines[0].startswith('##'):
51+
raise ValueError(f"File '{file_path}' does not appear to be TreeCorr ASCII output")
52+
53+
# Extract config dictionary from first line
54+
config_str = lines[0][2:].strip() # Remove '##' and whitespace
55+
try:
56+
config = ast.literal_eval(config_str)
57+
except (ValueError, SyntaxError) as e:
58+
raise ValueError(f"Cannot parse config from first line: {e}")
59+
60+
# Verify this is an NG (gamma-gamma) correlation
61+
if config.get('corr') != 'NG':
62+
raise ValueError(f"Expected 'NG' correlation type, got '{config.get('corr')}'")
63+
64+
# Create NGCorrelation object with config parameters
65+
ng = treecorr.NGCorrelation(
66+
min_sep=config['min_sep'],
67+
max_sep=config['max_sep'],
68+
sep_units=config['sep_units'],
69+
nbins=config['nbins'],
70+
var_method=config.get('var_method', 'shot'),
71+
metric=config.get('metric', 'Euclidean'),
72+
)
73+
74+
# Use TreeCorr's built-in read method to load the data
75+
ng.read(file_path)
76+
77+
return ng

0 commit comments

Comments
 (0)