Skip to content

Commit 647c155

Browse files
author
Bert Vandenbroucke
committed
Applied more review suggestions. Properly deal with out-of-bounds values in spline interpolation by setting the correction to 1 outside the interpolation range.
1 parent d3ddc01 commit 647c155

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

velociraptor-compute-box-size-correction

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ if log_y:
104104
small_spline = interpol.InterpolatedUnivariateSpline(small_box_x, small_box_y)
105105
large_spline = interpol.InterpolatedUnivariateSpline(large_box_x, large_box_y)
106106

107-
xmin = min(small_box_x.min(), large_box_x.min())
108-
xmax = max(small_box_x.max(), large_box_x.max())
107+
xmin = max(small_box_x.min(), large_box_x.min())
108+
xmax = min(small_box_x.max(), large_box_x.max())
109109
x_range = np.linspace(xmin, xmax, 100)
110110
small_y_range = small_spline(x_range)
111111
large_y_range = large_spline(x_range)
@@ -120,6 +120,8 @@ correction_data = {}
120120
correction_data["plot_name"] = args.plotname
121121
correction_data["plot_type"] = args.plottype
122122
correction_data["is_log_x"] = True
123+
correction_data["x_units"] = small_box_plot_data["centers_units"]
124+
correction_data["x_limits"] = np.array([xmin, xmax]).tolist()
123125
correction_data["x"] = x_range.tolist()
124126
correction_data["y"] = correction.tolist()
125127
with open(output_file, "w") as handle:

velociraptor/autoplotter/box_size_correction.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import yaml
88
import os
99
import scipy.interpolate as interpol
10+
import unyt
11+
from typing import Tuple
1012

1113

1214
class VelociraptorBoxSizeCorrection:
@@ -17,18 +19,26 @@ def __init__(self, filename: str, correction_directory: str):
1719
with open(correction_file, "r") as handle:
1820
correction_data = yaml.safe_load(handle)
1921
self.is_log_x = correction_data["is_log_x"]
22+
self.x_min, self.x_max = correction_data["x_limits"]
2023
x = np.array(correction_data["x"])
2124
y = np.array(correction_data["y"])
2225
self.correction_spline = interpol.InterpolatedUnivariateSpline(x, y)
2326

24-
def apply_mass_function_correction(self, mass_function_output):
27+
def apply_mass_function_correction(
28+
self,
29+
mass_function_output: Tuple[unyt.unyt_array, unyt.unyt_array, unyt.unyt_array],
30+
) -> Tuple[unyt.unyt_array, unyt.unyt_array, unyt.unyt_array]:
2531

2632
bin_centers, mass_function, error = mass_function_output
2733

34+
x_vals = bin_centers
35+
correction = np.ones(x_vals.shape)
2836
if self.is_log_x:
29-
correction = self.correction_spline(np.log10(bin_centers))
30-
else:
31-
correction = self.correction_spline(bin_centers)
37+
x_vals = np.log10(x_vals)
38+
# only apply the correction to bins that are within the range for which
39+
# the correction is valid
40+
x_mask = (self.x_min <= x_vals) & (x_vals <= self.x_max)
41+
correction[x_mask] = self.correction_spline(x_vals[x_mask])
3242

3343
corrected_mass_function = mass_function * correction
3444
corrected_mass_function.name = mass_function.name

velociraptor/autoplotter/lines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ def create_line(
323323
box_volume=box_volume,
324324
return_bin_edges=True,
325325
)
326+
if self.box_size_correction is not None:
327+
mass_function_output = self.box_size_correction.apply_mass_function_correction(
328+
mass_function_output
329+
)
326330
self.output = (
327331
*mass_function_output,
328332
unyt_array([], units=mass_function_output[0].units),

velociraptor/autoplotter/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def _parse_massfunction(self) -> None:
534534
self.box_size_correction = VelociraptorBoxSizeCorrection(
535535
box_size_correction, self.correction_directory
536536
)
537-
except:
537+
except KeyError:
538538
self.box_size_correction = None
539539
# A bit of a hacky workaround - improve this in the future
540540
# by combining this functionality properly into the
@@ -569,7 +569,7 @@ def _parse_adaptivemassfunction(self) -> None:
569569
self.box_size_correction = VelociraptorBoxSizeCorrection(
570570
box_size_correction, self.correction_directory
571571
)
572-
except:
572+
except KeyError:
573573
self.box_size_correction = None
574574
# A bit of a hacky workaround - improve this in the future
575575
# by combining this functionality properly into the

0 commit comments

Comments
 (0)