Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert PDF spectrum to CDF by default #72

Merged
merged 5 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion appletree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from immutabledict import immutabledict
from jax import numpy as jnp
from warnings import warn

from appletree.share import _cached_configs
from appletree.utils import exporter, load_json, get_file_path
from appletree.utils import exporter, load_json, get_file_path, integrate_midpoint, cum_integrate_midpoint
zihaoxu98 marked this conversation as resolved.
Show resolved Hide resolved

export, __all__ = exporter()

Expand Down Expand Up @@ -125,6 +126,12 @@ def build(self):
def build_point(self, data):
"""Cache the map to jnp.array if bins_type is point"""

if data['coordinate_name'] == 'pdf':
warn(f'Convert {self.name} from (x, pdf) to (cdf, x).')
x, cdf = self.pdf_to_cdf(data['coordinate_system'], data['map'])
data['coordinate_name'] = 'cdf'
data['coordinate_system'] = cdf
data['map'] = x
self.coordinate_name = data['coordinate_name']
self.coordinate_system = jnp.asarray(data['coordinate_system'], dtype=float)
self.map = jnp.asarray(data['map'], dtype=float)
Expand All @@ -136,3 +143,10 @@ def build_regbin(self, data):
self.coordinate_lowers = jnp.asarray(data['coordinate_lowers'], dtype=float)
self.coordinate_uppers = jnp.asarray(data['coordinate_uppers'], dtype=float)
self.map = jnp.asarray(data['map'], dtype=float)

def pdf_to_cdf(self, x, pdf):
zihaoxu98 marked this conversation as resolved.
Show resolved Hide resolved
"""Convert pdf map to cdf map"""
norm = integrate_midpoint(x, pdf)
x, cdf = cum_integrate_midpoint(x, pdf)
cdf /= norm
return x, cdf
19 changes: 19 additions & 0 deletions appletree/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,22 @@ def _add_extension(module, subclass, base):
)
else:
setattr(module, subclass.__name__, subclass)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
W291 trailing whitespace


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
W291 trailing whitespace

def integrate_midpoint(x, y):
"""Calculate the integral using midpoint method.
zihaoxu98 marked this conversation as resolved.
Show resolved Hide resolved
:param x: 1D array-like
:param y: 1D array-like, with the same length as x.
"""
_, res = cum_integrate(x, y)
return res[-1]
zihaoxu98 marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
W291 trailing whitespace


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
W291 trailing whitespace

def cum_integrate_midpoint(x, y):
"""Calculate the cumulative integral using midpoint method.
:param x: 1D array-like
:param y: 1D array-like, with the same length as x."""
dx = x[1:] - x[:-1]
x_mid = 0.5 * (x[1:] + x[:-1])
y_mid = 0.5 * (y[1:] + y[:-1])
return x_mid, np.cumsum(dx * y_mid)