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

Spherical harmonic class addition #59

Closed
wants to merge 17 commits into from

Conversation

xefonon
Copy link
Collaborator

@xefonon xefonon commented Nov 16, 2023

Hello all,

I've drafted the spherical harmonics class "Sphharm" with some features already included and potential for extension.
Feedback is appreciated.

For discussion

  • Add option for Condon-Shortley phase convention (difference between Ahrens - Rafaely) as a property. Check also if it available for real valued sph harmonics. (scipy includes this by default) - add not implemented in gradient
  • Do we want to remove the transforms from the SH class? (done)
  • Do we want to allow multideimensional coordinates or force cdim==1 - raise error: has to be 1D (possible feature)
  • Does the caching automatically recompute the bases, gradients, inverse, etc. if for example the normalization changes
  • --private parameter getter functions check recompute and recompute if required.
  • How do we want the converters between normalizations and channel orderings to work? Should they consider AmbisonicsSignals?
  • Flake8 should be checked (done)

Major:

  • Check CircleCI - dev branch not up to date with requirements (i.e., ModuleNotFoundError: No module named 'spharpy._deprecation')

ToDo

Comments Fabian

A few thoughts on the converters n3d_to_maxn and n3d_to_sn3d_norm

  • change n_sh back to n_max and make n_max docstring more explicit

  • For discussion: The names are inconsistent and should be changed. Were using '2' instead of 'to' in pyfar. Do we want to be consistent across packages here and use '2' everywhere?

  • A few notes on the docstrings:

    • They are not converting SH but returning weights for the conversion instead. I think this is fine, since conversion os handeled in the SH class. But we should maybe start the dosctrings with ' Compute order dependent weights for converting between ...' or something similar.
    • We could add a note that conversion of SH should usually be handeled by the SH class.
    • The docs of the return parameters should mention that this is a conversion factor, how it should be applied and how it can be applied to convert in the inverse direction.

Meeting 08/11/24:

  • Example notebook with different conventions etc.
  • change n_sh back to n_max and enhance docstring (regarding n_max)

@xefonon xefonon requested a review from mberz November 16, 2023 09:26
@xefonon xefonon linked an issue Nov 16, 2023 that may be closed by this pull request
@xefonon xefonon requested a review from ahms5 November 16, 2023 09:28
@ahms5 ahms5 changed the base branch from main to develop November 16, 2023 09:35
Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

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

Thanks for the effort. I mainly left concpetual questions for discussing in the group. And I wondered if we have functions for returning the index of a specific order n and degree m inside a SH matrix? That might come in handy in some cases.

Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

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

Thanks for the effort. I mainly left concpetual questions for discussing in the group. And I wondered if we have functions for returning the index of a specific order n and degree m inside a SH matrix? That might come in handy in some cases.

Copy link
Member

@ahms5 ahms5 left a comment

Choose a reason for hiding this comment

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

Thanks for implementing, maybe we should also discuss to move this to shparpy/classes/SphericalHarmonics.py, and bring it to toplevl. see pyfar.

@ahms5 ahms5 added the v1.0.0 label Mar 7, 2024
@xefonon xefonon marked this pull request as draft May 13, 2024 07:39
xefonon added a commit to xefonon/spharpy that referenced this pull request May 13, 2024
Changed SphericalHarmonics class after initial review
Add unit tests for SphericalHarmonics class in spharpy
mberz and others added 2 commits May 17, 2024 16:49
…calculation

Added SphericalHarmonics class with methods for computing spherical harmonic basis matrices, their gradients, and handling various spherical harmonic operations. Also included helper functions for spherical harmonic transformations and calculations
Copy link

@tluebeck tluebeck left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for the effort. I would only remove the transform from the SH class. Or did we decide to include here?

Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

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

Thanks for the effort, it is way better already. In addition to my comments, I marked some points for discussion in the description of the pull requetst

Calculate the linear index coefficient for a order n and degree m,
Calculate the spherical harmonic order n and degree m for a linear
coefficient index, according to the FuMa (Furse-Malham)
Channel Ordering Convention [2]_.
Copy link
Member

Choose a reason for hiding this comment

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

This will automatically number the references, which is safer

Suggested change
Channel Ordering Convention [2]_.
Channel Ordering Convention [#]_.

@xefonon
Copy link
Collaborator Author

xefonon commented Sep 30, 2024

Hello, thanks for the review so far. I've tried to incorporate most of the comments in the revised code. Let me know what you think, and I can fix the CircleCI tests also.

@pyfar pyfar deleted a comment from mberz Oct 11, 2024
@pyfar pyfar deleted a comment from ahms5 Oct 11, 2024
@pyfar pyfar deleted a comment from xefonon Oct 11, 2024
@pyfar pyfar deleted a comment from xefonon Oct 11, 2024
Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

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

I think we should generlly discuss the docstring and beahvior of the *_to_* functions.

@tluebeck tluebeck self-requested a review October 13, 2024 14:49
Copy link
Member

@mberz mberz left a comment

Choose a reason for hiding this comment

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

My comments are only on the SH class implementation.

They're mostly on slimming down the class and making it a bit more accessible by removing redundancy.
One thing I'm not sure is the calculation of the inverse of the gradient. I'd argue that it's too specific and should not be part of the class, but implemented as part of code which does rely on it. I wasn't sure for example if a valid orthogonal basis relying on quadrature does also results in orthogonal transforms when considering the respective gradients.

The diff of this file is still quite messy, which makes it hard to track the changes. I've tried to clean up the diff a bit in #76.
I think it would be useful to merge #76 into this branch and re-write the commit history before merging into main once this PR is finalised.
Please turn off auto-formatting or similar when editing the file.

Comment on lines 380 to 384
def compute_basis_gradient(self):
try:
return self._compute_basis_gradient()
except Exception as e:
raise ValueError("Error computing basis gradient:", e) from e
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def compute_basis_gradient(self):
try:
return self._compute_basis_gradient()
except Exception as e:
raise ValueError("Error computing basis gradient:", e) from e

As with the other public calculation methods, I'd remove this and only keep the private ones.

Comment on lines 173 to 181
def condon_shortley(self, value):
if not isinstance(value, bool):
raise TypeError("condon_shortley must be a boolean value")
if value != self._condon_shortley:
self._recompute_basis = True
self._recompute_basis_gradient = True
self._recompute_inverse = True
self._recompute_inverse_gradient = True
self._condon_shortley = value
Copy link
Member

Choose a reason for hiding this comment

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

As far as I remember the Condon Shortley phase is only used for complex valued SHs

Comment on lines 158 to 164
self._prev_n_max = self.n_max
self._prev_coordinates = self.coordinates
self._prev_basis_type = self.basis_type
self._prev_inverse_transform = self.inverse_transform
self._prev_channel_convention = self.channel_convention
self._prev_normalization = self.normalization
self._prev_condon_shortley = self.condon_shortley
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self._prev_n_max = self.n_max
self._prev_coordinates = self.coordinates
self._prev_basis_type = self.basis_type
self._prev_inverse_transform = self.inverse_transform
self._prev_channel_convention = self.channel_convention
self._prev_normalization = self.normalization
self._prev_condon_shortley = self.condon_shortley

channel_convention="acn",
inverse_transform=None,
weights=None,
condon_shortley=True
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
condon_shortley=True
condon_shortley=None

I think this is conditional wheter the SHs are real or complex valued. I'm not aware of a real valued SH implementation using the CS phase.

@xefonon xefonon self-assigned this Nov 29, 2024
@xefonon xefonon closed this Nov 29, 2024
@xefonon xefonon deleted the dev_spherical_harmonic_class branch November 29, 2024 12:06
mberz pushed a commit that referenced this pull request Feb 13, 2025
mberz pushed a commit that referenced this pull request Feb 13, 2025
…eights (currently 2 ways of setting weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New feature: SphericalHarmonic class
5 participants