Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Use ones_like
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoCentauri committed Feb 7, 2023
1 parent a99fd2a commit 48f0790
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 39 deletions.
11 changes: 5 additions & 6 deletions examples/linear-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ase.io
import numpy as np
from equistore import Labels
from equistore.operations import slice, sum_over_samples
from equistore.operations import slice, sum_over_samples, ones_like
from rascaline import SoapPowerSpectrum

from equisolve.numpy.models.linear_model import Ridge
Expand Down Expand Up @@ -132,9 +132,8 @@
#
# For this we create a TensorMap containing with the desired regulerizer


alpha_dict = {"values": 1e-5}
alpha = dictionary_to_tensormap(alpha_dict, X)
alpha = ones_like(X)
alpha.block().values[:] *= 1e-5

# %%
#
Expand Down Expand Up @@ -173,8 +172,8 @@
# Next we create a sample weighting :class:`equistiore.TensorMap` that weights energies
# five times more then the forces.

sw_dict = {"values": 5, "positions": 1}
sw = dictionary_to_tensormap(sw_dict, y)
sw = ones_like(y)
sw.block().values[:] *= 5

# %%
#
Expand Down
3 changes: 1 addition & 2 deletions src/equisolve/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
# Released under the BSD 3-Clause "New" or "Revised" License
# SPDX-License-Identifier: BSD-3-Clause

from .convert import ase_to_tensormap, dictionary_to_tensormap, properties_to_tensormap
from .convert import ase_to_tensormap, properties_to_tensormap
from .metrics import rmse


__all__ = [
"ase_to_tensormap",
"dictionary_to_tensormap",
"properties_to_tensormap",
"rmse",
]
31 changes: 0 additions & 31 deletions src/equisolve/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,34 +141,3 @@ def properties_to_tensormap(
)

return TensorMap(Labels.single(), [block])


def dictionary_to_tensormap(d: dict, ts: TensorMap) -> TensorMap:
"""TensorMap with data provided by a dictionary and meta data from a TensorMap.
Parameters which are no keys in ``d`` but present in ``block`` will be copied
into the new block.
Should be moved to equistore!
:paramas d: Keys of ``d`` have to be the 'parameters' of provided TensorMap i.e.
``'values'``, ``'positions'``, ``'cell'``. Values of ``d`` can either
be scalars or must have the same shape as the data associated with
each 'parameter'.
:params block: Reference :class:`equistore.TensorMap` for gathering the
meta data
:returns ts_d: block containing the data of ``d`` with the metadata of ``block``.
"""
blocks = []
for block in ts.blocks():
new_block = block.copy()

for parameter, data in d.items():
if parameter == "values":
new_block.values[:] = data
else:
new_block.gradient(parameter).data[:] = data

blocks.append(new_block)

return TensorMap(ts.keys, blocks)

0 comments on commit 48f0790

Please sign in to comment.