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

Adding Sine and Cosine transform #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
66 changes: 64 additions & 2 deletions src/jimgw/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def transform(self, x: dict[str, Float]) -> tuple[dict[str, Float], Float]:
output_params = self.transform_func(transform_params)
jacobian = jax.jacfwd(self.transform_func)(transform_params)
jacobian = jnp.array(jax.tree.leaves(jacobian))
jacobian = jnp.log(jnp.absolute(jnp.linalg.det(jacobian.reshape(self.n_dim, self.n_dim))))
jacobian = jnp.log(
jnp.absolute(jnp.linalg.det(jacobian.reshape(self.n_dim, self.n_dim)))
)
jax.tree.map(
lambda key: x_copy.pop(key),
self.name_mapping[0],
Expand Down Expand Up @@ -124,7 +126,9 @@ def inverse(self, y: dict[str, Float]) -> tuple[dict[str, Float], Float]:
output_params = self.inverse_transform_func(transform_params)
jacobian = jax.jacfwd(self.inverse_transform_func)(transform_params)
jacobian = jnp.array(jax.tree.leaves(jacobian))
jacobian = jnp.log(jnp.absolute(jnp.linalg.det(jacobian.reshape(self.n_dim, self.n_dim))))
jacobian = jnp.log(
jnp.absolute(jnp.linalg.det(jacobian.reshape(self.n_dim, self.n_dim)))
)
jax.tree.map(
lambda key: y_copy.pop(key),
self.name_mapping[1],
Expand Down Expand Up @@ -298,6 +302,64 @@ def __init__(
}


@jaxtyped(typechecker=typechecker)
class SineTransform(BijectiveTransform):
"""
Sine transformation

The original parameter is expected to be in [-pi/2, pi/2]

Parameters
----------
name_mapping : tuple[list[str], list[str]]
The name mapping between the input and output dictionary.

"""

def __init__(
self,
name_mapping: tuple[list[str], list[str]],
):
super().__init__(name_mapping)
self.transform_func = lambda x: {
name_mapping[1][i]: jnp.sin(x[name_mapping[0][i]])
for i in range(len(name_mapping[0]))
}
self.inverse_transform_func = lambda x: {
name_mapping[0][i]: jnp.arcsin(x[name_mapping[1][i]])
for i in range(len(name_mapping[1]))
}


@jaxtyped(typechecker=typechecker)
class CosineTransform(BijectiveTransform):
"""
Cosine transformation

The original parameter is expected to be in [0, pi]

Parameters
----------
name_mapping : tuple[list[str], list[str]]
The name mapping between the input and output dictionary.

"""

def __init__(
self,
name_mapping: tuple[list[str], list[str]],
):
super().__init__(name_mapping)
self.transform_func = lambda x: {
name_mapping[1][i]: jnp.cos(x[name_mapping[0][i]])
for i in range(len(name_mapping[0]))
}
self.inverse_transform_func = lambda x: {
name_mapping[0][i]: jnp.arccos(x[name_mapping[1][i]])
for i in range(len(name_mapping[1]))
}


@jaxtyped(typechecker=typechecker)
class ArcSineTransform(BijectiveTransform):
"""
Expand Down
Loading