Skip to content

Commit 69e488b

Browse files
authored
Merge pull request #100 from kyleaoman/pyproject_toml
Pyproject toml
2 parents 940b8f8 + 70b492a commit 69e488b

File tree

7 files changed

+179
-130
lines changed

7 files changed

+179
-130
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ dist/*
1414

1515
# MacOS
1616
*.DS_Store
17+
18+
# autoformatter virtualenv
19+
black_formatting_env/*

pyproject.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[tool.setuptools]
6+
packages = ["velociraptor"]
7+
8+
[project]
9+
name = "velociraptor-python"
10+
version="0.16.1"
11+
authors = [
12+
{ name="Josh Borrow", email="[email protected]" },
13+
{ name="Kyle Oman", email="[email protected]" },
14+
]
15+
description="Velociraptor catalogue reading routines."
16+
readme = "README.md"
17+
requires-python = ">3.6.0"
18+
classifiers = [
19+
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
20+
"Operating System :: OS Independent",
21+
]
22+
dependencies = [
23+
"numpy",
24+
"h5py",
25+
"unyt>=2.6.0",
26+
"astropy",
27+
]
28+
29+
[project.urls]
30+
"Homepage" = "https://github.com/SWIFTSIM/velociraptor-python"
31+
"Bug Tracker" = "https://github.com/SWIFTSIM/velociraptor-python/issues"
32+
"Documentation" = "https://velociraptor-python.readthedocs.io/en/latest"
33+
34+
[project.scripts]
35+
velociraptor-plot = "velociraptor.velociraptor_plot:velociraptor_plot"
36+
velociraptor-compute-box-size-correction = "velociraptor.velociraptor_compute_box_size_correction:velociraptor_compute_box_size_correction"

tests/__init__.py

Whitespace-only changes.

tests/test_load_catalogue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from velociraptor import load
6-
from helper import requires
6+
from .helper import requires
77

88

99
@requires("cosmo_0000.properties")

velociraptor-compute-box-size-correction

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Compute a box size correction file that can be used as the 'box_size_correction'
5+
argument for an autoplotter plot.
6+
7+
Usage:
8+
velociraptor-compute-box-size-correction \
9+
smallbox largebox plotname plottype output
10+
11+
with:
12+
- smallbox/largebox: data*.yml output file from a pipeline run
13+
- plotname: Name of a particular plot in the data*.yml files
14+
- plottype: Type of plot (currently supported: mass_function)
15+
- output: Name of an output .yml file. If the .yml extension is missing, it is
16+
added.
17+
"""
18+
19+
import argparse
20+
import os
21+
import yaml
22+
import numpy as np
23+
import scipy.interpolate as interpol
24+
25+
26+
def velociraptor_compute_box_size_correction():
27+
argparser = argparse.ArgumentParser("Compute the box size correction for a plot.")
28+
argparser.add_argument(
29+
"smallbox", help="Pipeline output for the small box that needs to be corrected."
30+
)
31+
argparser.add_argument(
32+
"largebox", help="Pipeline output for the large box that we want to correct to."
33+
)
34+
argparser.add_argument("plotname", help="Name of the plot that we want to correct.")
35+
argparser.add_argument("plottype", help="Type of the plot we want to correct.")
36+
argparser.add_argument(
37+
"output", help="Name of the output file that will store the correction."
38+
)
39+
args = argparser.parse_args()
40+
41+
if not args.plottype in ["mass_function"]:
42+
raise AttributeError(
43+
f"Cannot compute box size correction for plot type {args.plottype}!"
44+
)
45+
46+
log_x = False
47+
log_y = False
48+
if args.plottype in ["mass_function"]:
49+
log_x = True
50+
log_y = True
51+
52+
small_box = args.smallbox
53+
large_box = args.largebox
54+
for file in [args.smallbox, args.largebox]:
55+
if not os.path.exists(file):
56+
raise AttributeError(f"File {file} could not be found!")
57+
58+
output_file = args.output
59+
if not output_file.endswith(".yml"):
60+
output_file += ".yml"
61+
try:
62+
open(output_file, "w").close()
63+
except:
64+
raise AttributeError(f"Can not write to {output_file}!")
65+
66+
with open(args.smallbox, "r") as handle:
67+
small_box = yaml.safe_load(handle)
68+
with open(args.largebox, "r") as handle:
69+
large_box = yaml.safe_load(handle)
70+
71+
try:
72+
small_box_data = small_box[args.plotname]["lines"]
73+
except:
74+
raise AttributeError(f"Could not find {args.plotname} in {args.smallbox}!")
75+
try:
76+
large_box_data = large_box[args.plotname]["lines"]
77+
except:
78+
raise AttributeError(f"Could not find {args.plotname} in {args.largebox}!")
79+
80+
try:
81+
small_box_plot_data = small_box_data[args.plottype]
82+
except:
83+
raise AttributeError(
84+
f"{args.plottype} not found in plot {args.plotname} in {args.smallbox}!"
85+
)
86+
try:
87+
large_box_plot_data = large_box_data[args.plottype]
88+
except:
89+
raise AttributeError(
90+
f"{args.plottype} not found in plot {args.plotname} in {args.largebox}!"
91+
)
92+
93+
small_box_x = small_box_plot_data["centers"]
94+
small_box_y = small_box_plot_data["values"]
95+
large_box_x = large_box_plot_data["centers"]
96+
large_box_y = large_box_plot_data["values"]
97+
98+
if log_x:
99+
small_box_x = np.log10(small_box_x)
100+
large_box_x = np.log10(large_box_x)
101+
102+
if log_y:
103+
small_box_y = np.log10(small_box_y)
104+
large_box_y = np.log10(large_box_y)
105+
106+
small_spline = interpol.InterpolatedUnivariateSpline(small_box_x, small_box_y)
107+
large_spline = interpol.InterpolatedUnivariateSpline(large_box_x, large_box_y)
108+
109+
xmin = max(small_box_x.min(), large_box_x.min())
110+
xmax = min(small_box_x.max(), large_box_x.max())
111+
x_range = np.linspace(xmin, xmax, 100)
112+
small_y_range = small_spline(x_range)
113+
large_y_range = large_spline(x_range)
114+
115+
if log_y:
116+
small_y_range = 10.0 ** small_y_range
117+
large_y_range = 10.0 ** large_y_range
118+
119+
correction = large_y_range / small_y_range
120+
121+
correction_data = {}
122+
correction_data["plot_name"] = args.plotname
123+
correction_data["plot_type"] = args.plottype
124+
correction_data["is_log_x"] = True
125+
correction_data["x_units"] = small_box_plot_data["centers_units"]
126+
correction_data["x_limits"] = np.array([xmin, xmax]).tolist()
127+
correction_data["x"] = x_range.tolist()
128+
correction_data["y"] = correction.tolist()
129+
with open(output_file, "w") as handle:
130+
yaml.safe_dump(correction_data, handle)
131+
132+
133+
if __name__ == "__main__":
134+
velociraptor_compute_box_size_correction()
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
)
110110

111111

112-
if __name__ == "__main__":
112+
def velociraptor_plot():
113113
# Parse our lovely arguments and pass them to the velociraptor library
114114
from velociraptor.autoplotter.objects import AutoPlotter
115115
from velociraptor.autoplotter.metadata import AutoPlotterMetadata
@@ -169,3 +169,7 @@ def print_if_debug(string: str):
169169
auto_plotter_metadata.write_metadata(args.metadata)
170170

171171
print_if_debug("Done.")
172+
173+
174+
if __name__ == "__main__":
175+
velociraptor_plot()

0 commit comments

Comments
 (0)