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

Add option to fill all species when computing PowerSpectrum #214

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
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
26 changes: 21 additions & 5 deletions python/rascaline/rascaline/utils/power_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,18 @@ def compute(
]
assert spherical_expansion_1.keys.names == expected_key_names
assert spherical_expansion_1.property_names == ["n"]
spherical_expansion_1 = spherical_expansion_1.keys_to_properties(
"species_neighbor"

# Fill blocks with `species_neighbor` from ALL blocks. If we don't do this
# merging blocks along the ``sample`` direction might be not possible.
keys_to_move = Labels(
names="species_neighbor",
values=np.unique(spherical_expansion_1.keys["species_neighbor"]).reshape(
-1, 1
),
)

spherical_expansion_1 = spherical_expansion_1.keys_to_properties(keys_to_move)

if self.calculator_2 is None:
spherical_expansion_2 = spherical_expansion_1
else:
Expand All @@ -197,8 +205,16 @@ def compute(
)
assert spherical_expansion_2.keys.names == expected_key_names
assert spherical_expansion_2.property_names == ["n"]

keys_to_move = Labels(
names="species_neighbor",
values=np.unique(
spherical_expansion_2.keys["species_neighbor"]
).reshape(-1, 1),
)

spherical_expansion_2 = spherical_expansion_2.keys_to_properties(
"species_neighbor"
keys_to_move
)

blocks = []
Expand All @@ -212,7 +228,7 @@ def compute(
spherical_harmonics_l=ell, species_center=species_center
)
for block_2 in blocks_2:
# Makre sure that samples are the same. This should not happen.
# Make sure that samples are the same. This should not happen.
assert block_1.samples == block_2.samples

properties = Labels(
Expand Down Expand Up @@ -258,7 +274,7 @@ def _positions_gradients(new_block, block_1, block_2, factor):
gradient_2 = block_2.gradient("positions")

if len(gradient_1.samples) == 0 or len(gradient_2.samples) == 0:
gradients_samples = Labels.empty()
gradients_samples = Labels.empty(names=["sample", "structure", "atom"])
gradient_values = np.array([]).reshape(0, 1, len(new_block.properties))
else:
# The "sample" dimension in the power spectrum gradient samples do
Expand Down
18 changes: 18 additions & 0 deletions python/rascaline/tests/utils/power_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,21 @@ def test_power_spectrum_unknown_gradient() -> None:
msg = "PowerSpectrum currently only supports gradients w.r.t. to positions"
with pytest.raises(NotImplementedError, match=msg):
PowerSpectrum(calculator).compute(SystemForTests(), gradients=["cell"])


def test_fill_species_neighbor() -> None:
"""Test that ``species_center`` keys can be merged for different blocks."""

frames = [
ase.Atoms("H", positions=np.zeros([1, 3])),
ase.Atoms("O", positions=np.zeros([1, 3])),
]

calculator = PowerSpectrum(
calculator_1=rascaline.SphericalExpansion(**HYPERS),
calculator_2=rascaline.SphericalExpansion(**HYPERS),
)

descriptor = calculator.compute(frames)

descriptor.keys_to_samples("species_center")