From 6ccb97ad1d7efc073bb4ac3dcd48c138a5241eac Mon Sep 17 00:00:00 2001 From: Kyle Oman Date: Fri, 30 Jun 2023 12:16:42 +0100 Subject: [PATCH] Convert from setup.py to pyproject.toml --- pyproject.toml | 38 +++++ setup.py | 27 ---- tests/__init__.py | 0 tests/test_load_catalogue.py | 6 +- velociraptor-compute-box-size-correction | 128 ----------------- ...elociraptor_compute_box_size_correction.py | 134 ++++++++++++++++++ .../velociraptor_plot.py | 6 +- 7 files changed, 181 insertions(+), 158 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.py create mode 100644 tests/__init__.py delete mode 100755 velociraptor-compute-box-size-correction create mode 100644 velociraptor/velociraptor_compute_box_size_correction.py rename velociraptor-plot => velociraptor/velociraptor_plot.py (98%) mode change 100755 => 100644 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d95e06e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,38 @@ +[build-system] +requires = ["setuptools>=61.0"] +build-backend = "setuptools.build_meta" + +[tool.setuptools] +packages = ["velociraptor"] + +[project] +name = "velociraptor-python" +version="0.16.1" +authors = [ + { name="Josh Borrow", email="joshua.borrow@durham.ac.uk" }, +] +description="Velociraptor catalogue reading routines." +readme = "README.md" +requires-python = ">3.6.0" +classifiers = [ + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)", + "Operating System :: OS Independent", +] +dependencies = [ + "numpy", + "h5py", + "unyt>=2.6.0", + "astropy", +] + +[project.urls] +"Homepage" = "https://github.com/SWIFTSIM/velociraptor-python" +"Bug Tracker" = "https://github.com/SWIFTSIM/velociraptor-python/issues" +"Documentation" = "https://velociraptor-python.readthedocs.io/en/latest" + +[project.scripts] +velociraptor-plot = "velociraptor.velociraptor_plot:velociraptor_plot" +velociraptor-compute-box-size-correction = "velociraptor.velociraptor_compute_box_size_correction:velociraptor_compute_box_size_correction" \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100644 index 8c28f45..0000000 --- a/setup.py +++ /dev/null @@ -1,27 +0,0 @@ -import setuptools -from velociraptor import __version__ - -with open("README.md", "r") as fh: - long_description = fh.read() - -setuptools.setup( - name="velociraptor", - version=__version__, - description="Velociraptor catalogue reading routines.", - url="https://github.com/swiftsim/velociraptor-python", - author="Josh Borrow", - author_email="joshua.borrow@durham.ac.uk", - packages=setuptools.find_packages(), - scripts=["velociraptor-plot"], - long_description=long_description, - long_description_content_type="text/markdown", - zip_safe=False, - classifiers=[ - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)", - "Operating System :: OS Independent", - ], - install_requires=["numpy", "unyt>=2.6.0", "h5py", "astropy"], -) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_load_catalogue.py b/tests/test_load_catalogue.py index ae3ac33..3130709 100644 --- a/tests/test_load_catalogue.py +++ b/tests/test_load_catalogue.py @@ -3,11 +3,13 @@ """ from velociraptor import load -from helper import requires +from .helper import requires @requires("cosmo_0000.properties") -def test_basic_load_catalogue_no_crash(filename="test_data/cosmo_0000.properties",): +def test_basic_load_catalogue_no_crash( + filename="test_data/cosmo_0000.properties", +): catalogue = load(filename) return diff --git a/velociraptor-compute-box-size-correction b/velociraptor-compute-box-size-correction deleted file mode 100755 index 769d2c0..0000000 --- a/velociraptor-compute-box-size-correction +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/env python - -""" -Compute a box size correction file that can be used as the 'box_size_correction' -argument for an autoplotter plot. - -Usage: - velociraptor-compute-box-size-correction \ - smallbox largebox plotname plottype output - -with: - - smallbox/largebox: data*.yml output file from a pipeline run - - plotname: Name of a particular plot in the data*.yml files - - plottype: Type of plot (currently supported: mass_function) - - output: Name of an output .yml file. If the .yml extension is missing, it is - added. -""" - -import argparse -import os -import yaml -import numpy as np -import scipy.interpolate as interpol - -argparser = argparse.ArgumentParser("Compute the box size correction for a plot.") -argparser.add_argument( - "smallbox", help="Pipeline output for the small box that needs to be corrected." -) -argparser.add_argument( - "largebox", help="Pipeline output for the large box that we want to correct to." -) -argparser.add_argument("plotname", help="Name of the plot that we want to correct.") -argparser.add_argument("plottype", help="Type of the plot we want to correct.") -argparser.add_argument( - "output", help="Name of the output file that will store the correction." -) -args = argparser.parse_args() - -if not args.plottype in ["mass_function"]: - raise AttributeError( - f"Cannot compute box size correction for plot type {args.plottype}!" - ) - -log_x = False -log_y = False -if args.plottype in ["mass_function"]: - log_x = True - log_y = True - -small_box = args.smallbox -large_box = args.largebox -for file in [args.smallbox, args.largebox]: - if not os.path.exists(file): - raise AttributeError(f"File {file} could not be found!") - -output_file = args.output -if not output_file.endswith(".yml"): - output_file += ".yml" -try: - open(output_file, "w").close() -except: - raise AttributeError(f"Can not write to {output_file}!") - -with open(args.smallbox, "r") as handle: - small_box = yaml.safe_load(handle) -with open(args.largebox, "r") as handle: - large_box = yaml.safe_load(handle) - -try: - small_box_data = small_box[args.plotname]["lines"] -except: - raise AttributeError(f"Could not find {args.plotname} in {args.smallbox}!") -try: - large_box_data = large_box[args.plotname]["lines"] -except: - raise AttributeError(f"Could not find {args.plotname} in {args.largebox}!") - -try: - small_box_plot_data = small_box_data[args.plottype] -except: - raise AttributeError( - f"{args.plottype} not found in plot {args.plotname} in {args.smallbox}!" - ) -try: - large_box_plot_data = large_box_data[args.plottype] -except: - raise AttributeError( - f"{args.plottype} not found in plot {args.plotname} in {args.largebox}!" - ) - -small_box_x = small_box_plot_data["centers"] -small_box_y = small_box_plot_data["values"] -large_box_x = large_box_plot_data["centers"] -large_box_y = large_box_plot_data["values"] - -if log_x: - small_box_x = np.log10(small_box_x) - large_box_x = np.log10(large_box_x) - -if log_y: - small_box_y = np.log10(small_box_y) - large_box_y = np.log10(large_box_y) - -small_spline = interpol.InterpolatedUnivariateSpline(small_box_x, small_box_y) -large_spline = interpol.InterpolatedUnivariateSpline(large_box_x, large_box_y) - -xmin = max(small_box_x.min(), large_box_x.min()) -xmax = min(small_box_x.max(), large_box_x.max()) -x_range = np.linspace(xmin, xmax, 100) -small_y_range = small_spline(x_range) -large_y_range = large_spline(x_range) - -if log_y: - small_y_range = 10.0**small_y_range - large_y_range = 10.0**large_y_range - -correction = large_y_range / small_y_range - -correction_data = {} -correction_data["plot_name"] = args.plotname -correction_data["plot_type"] = args.plottype -correction_data["is_log_x"] = True -correction_data["x_units"] = small_box_plot_data["centers_units"] -correction_data["x_limits"] = np.array([xmin, xmax]).tolist() -correction_data["x"] = x_range.tolist() -correction_data["y"] = correction.tolist() -with open(output_file, "w") as handle: - yaml.safe_dump(correction_data, handle) diff --git a/velociraptor/velociraptor_compute_box_size_correction.py b/velociraptor/velociraptor_compute_box_size_correction.py new file mode 100644 index 0000000..d6dbe79 --- /dev/null +++ b/velociraptor/velociraptor_compute_box_size_correction.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python + +""" +Compute a box size correction file that can be used as the 'box_size_correction' +argument for an autoplotter plot. + +Usage: + velociraptor-compute-box-size-correction \ + smallbox largebox plotname plottype output + +with: + - smallbox/largebox: data*.yml output file from a pipeline run + - plotname: Name of a particular plot in the data*.yml files + - plottype: Type of plot (currently supported: mass_function) + - output: Name of an output .yml file. If the .yml extension is missing, it is + added. +""" + +import argparse +import os +import yaml +import numpy as np +import scipy.interpolate as interpol + + +def velociraptor_compute_box_size_correction(): + argparser = argparse.ArgumentParser("Compute the box size correction for a plot.") + argparser.add_argument( + "smallbox", help="Pipeline output for the small box that needs to be corrected." + ) + argparser.add_argument( + "largebox", help="Pipeline output for the large box that we want to correct to." + ) + argparser.add_argument("plotname", help="Name of the plot that we want to correct.") + argparser.add_argument("plottype", help="Type of the plot we want to correct.") + argparser.add_argument( + "output", help="Name of the output file that will store the correction." + ) + args = argparser.parse_args() + + if not args.plottype in ["mass_function"]: + raise AttributeError( + f"Cannot compute box size correction for plot type {args.plottype}!" + ) + + log_x = False + log_y = False + if args.plottype in ["mass_function"]: + log_x = True + log_y = True + + small_box = args.smallbox + large_box = args.largebox + for file in [args.smallbox, args.largebox]: + if not os.path.exists(file): + raise AttributeError(f"File {file} could not be found!") + + output_file = args.output + if not output_file.endswith(".yml"): + output_file += ".yml" + try: + open(output_file, "w").close() + except: + raise AttributeError(f"Can not write to {output_file}!") + + with open(args.smallbox, "r") as handle: + small_box = yaml.safe_load(handle) + with open(args.largebox, "r") as handle: + large_box = yaml.safe_load(handle) + + try: + small_box_data = small_box[args.plotname]["lines"] + except: + raise AttributeError(f"Could not find {args.plotname} in {args.smallbox}!") + try: + large_box_data = large_box[args.plotname]["lines"] + except: + raise AttributeError(f"Could not find {args.plotname} in {args.largebox}!") + + try: + small_box_plot_data = small_box_data[args.plottype] + except: + raise AttributeError( + f"{args.plottype} not found in plot {args.plotname} in {args.smallbox}!" + ) + try: + large_box_plot_data = large_box_data[args.plottype] + except: + raise AttributeError( + f"{args.plottype} not found in plot {args.plotname} in {args.largebox}!" + ) + + small_box_x = small_box_plot_data["centers"] + small_box_y = small_box_plot_data["values"] + large_box_x = large_box_plot_data["centers"] + large_box_y = large_box_plot_data["values"] + + if log_x: + small_box_x = np.log10(small_box_x) + large_box_x = np.log10(large_box_x) + + if log_y: + small_box_y = np.log10(small_box_y) + large_box_y = np.log10(large_box_y) + + small_spline = interpol.InterpolatedUnivariateSpline(small_box_x, small_box_y) + large_spline = interpol.InterpolatedUnivariateSpline(large_box_x, large_box_y) + + xmin = max(small_box_x.min(), large_box_x.min()) + xmax = min(small_box_x.max(), large_box_x.max()) + x_range = np.linspace(xmin, xmax, 100) + small_y_range = small_spline(x_range) + large_y_range = large_spline(x_range) + + if log_y: + small_y_range = 10.0**small_y_range + large_y_range = 10.0**large_y_range + + correction = large_y_range / small_y_range + + correction_data = {} + correction_data["plot_name"] = args.plotname + correction_data["plot_type"] = args.plottype + correction_data["is_log_x"] = True + correction_data["x_units"] = small_box_plot_data["centers_units"] + correction_data["x_limits"] = np.array([xmin, xmax]).tolist() + correction_data["x"] = x_range.tolist() + correction_data["y"] = correction.tolist() + with open(output_file, "w") as handle: + yaml.safe_dump(correction_data, handle) + + +if __name__ == "__main__": + velociraptor_compute_box_size_correction() diff --git a/velociraptor-plot b/velociraptor/velociraptor_plot.py old mode 100755 new mode 100644 similarity index 98% rename from velociraptor-plot rename to velociraptor/velociraptor_plot.py index ec99072..62bd2e9 --- a/velociraptor-plot +++ b/velociraptor/velociraptor_plot.py @@ -109,7 +109,7 @@ ) -if __name__ == "__main__": +def velociraptor_plot(): # Parse our lovely arguments and pass them to the velociraptor library from velociraptor.autoplotter.objects import AutoPlotter from velociraptor.autoplotter.metadata import AutoPlotterMetadata @@ -169,3 +169,7 @@ def print_if_debug(string: str): auto_plotter_metadata.write_metadata(args.metadata) print_if_debug("Done.") + + +if __name__ == "__main__": + velociraptor_plot()