Skip to content

Commit

Permalink
Adjusting linear triangular function
Browse files Browse the repository at this point in the history
  • Loading branch information
achiefa authored and RoyStegeman committed Feb 14, 2025
1 parent ab92347 commit 9f286c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
7 changes: 5 additions & 2 deletions validphys2/src/validphys/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,12 +1792,15 @@ def produce_power_corr_dict(self, pc_parameters=None, pc_func_type=None):
# Loop over the parameterization for the power corrections in the runcard
for par in pc_parameters:
# Check that the length of shifts is one less than the length of nodes.
if (len(par['yshift']) != len(par['nodes']) - 1) and pc_func_type != 'cubic':
if (len(par['yshift']) != len(par['nodes']) - 1) and pc_func_type not in [
'cubic',
'linear',
]:
raise ValueError(
f"The length of nodes does not match that of the list in {par['ht']}."
f"Check the runcard. Got {len(par['yshift'])} != {len(par['nodes'])}"
)
elif (len(par['yshift']) != len(par['nodes'])) and pc_func_type == 'cubic':
elif (len(par['yshift']) != len(par['nodes'])) and pc_func_type in ['cubic', 'linear']:
raise ValueError(
f"The length of nodes does not match that of the list in {par['ht']}."
f"Check the runcard. Got {len(par['yshift'])} != {len(par['nodes'])}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,31 @@ def linear_bin_function(
"""
res = np.zeros_like(a)
for shift_pos, shift in enumerate(y_shift):
bin_low = bin_edges[shift_pos]
bin_high = bin_edges[shift_pos + 1]
bin_mid = 0.5 * (bin_low + bin_high)
if shift_pos > 0 and shift_pos < len(y_shift) - 1:
bin_low = bin_edges[shift_pos - 1]
bin_high = bin_edges[shift_pos + 1]
bin_mid = bin_edges[shift_pos]
m1 = shift / (bin_mid - bin_low)
m2 = shift / (bin_high - bin_mid)
elif shift_pos == 0: # Left-most bin
bin_high = bin_edges[shift_pos + 1]
bin_mid = bin_edges[shift_pos]
bin_low = bin_mid
m1 = 0.0
m2 = shift / (bin_high - bin_mid)
else: # Right-most bin
bin_low = bin_edges[shift_pos - 1]
bin_mid = bin_edges[shift_pos]
bin_high = bin_mid
m1 = shift / (bin_mid - bin_low)
m2 = 0.0
cond_low = np.multiply(a >= bin_low, a < bin_mid)
cond_high = np.multiply(
a >= bin_mid, a < bin_high if shift_pos != len(y_shift) - 1 else a <= bin_high
)
m = 2 * shift / (bin_high - bin_low)
res = np.add(res, [m * (val - bin_low) if cond else 0.0 for val, cond in zip(a, cond_low)])
res = np.add(res, [m1 * (val - bin_low) if cond else 0.0 for val, cond in zip(a, cond_low)])
res = np.add(
res, [-m * (val - bin_high) if cond else 0.0 for val, cond in zip(a, cond_high)]
res, [-m2 * (val - bin_high) if cond else 0.0 for val, cond in zip(a, cond_high)]
)
return res

Expand Down

0 comments on commit 9f286c6

Please sign in to comment.