Skip to content

Commit

Permalink
use rs decorators for @cellify and @spacegroupify (#62)
Browse files Browse the repository at this point in the history
* use rs decorators

* ya so decorator order matters

* lint
  • Loading branch information
tjlane authored Oct 31, 2024
1 parent bc84ac0 commit 27606e5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
17 changes: 14 additions & 3 deletions meteor/rsmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import numpy as np
import pandas as pd
import reciprocalspaceship as rs
from reciprocalspaceship.decorators import cellify, spacegroupify

from .settings import GEMMI_HIGH_RESOLUTION_BUFFER
from .utils import (
CellType,
SpacegroupType,
canonicalize_amplitudes,
complex_array_to_rs_dataseries,
numpy_array_to_map,
Expand Down Expand Up @@ -63,6 +66,8 @@ class Map(rs.DataSet):
# in addition, __init__ specifies 3 columns special that can be named dynamically to support:
# amplitudes, phases, uncertainties; all other columns are forbidden

@cellify
@spacegroupify
def __init__(
self,
data: dict | pd.DataFrame | rs.DataSet,
Expand Down Expand Up @@ -208,6 +213,9 @@ def get_hkls(self) -> np.ndarray:
def compute_dHKL(self) -> rs.DataSeries: # noqa: N802, caps from reciprocalspaceship
# rs adds a "dHKL" column to the DataFrame
# that could be enabled by adding "dHKL" to _allowed_columns - @tjlane
if not hasattr(self, "cell"):
msg = "no `cell` attribute set, cannot compute resolution (d-values)"
raise AttributeError(msg)
d_hkl = self.cell.calculate_d_array(self.get_hkls())
return rs.DataSeries(d_hkl, dtype="R", index=self.index)

Expand Down Expand Up @@ -288,13 +296,15 @@ def to_structurefactor(self) -> rs.DataSeries:
return super().to_structurefactor(self._amplitude_column, self._phase_column)

@classmethod
@cellify("cell")
@spacegroupify("spacegroup")
def from_structurefactor(
cls,
complex_structurefactor: np.ndarray | rs.DataSeries,
*,
index: pd.Index,
cell: Any = None,
spacegroup: Any = None,
cell: CellType | None = None,
spacegroup: SpacegroupType | None = None,
) -> Map:
# 1. `rs.DataSet.from_structurefactor` exists, but it operates on a column that's already
# part of the dataset; having such a (redundant) column is forbidden by `Map`
Expand Down Expand Up @@ -326,8 +336,9 @@ def from_gemmi(
)

@classmethod
@cellify("cell")
def from_3d_numpy_map(
cls, map_grid: np.ndarray, *, spacegroup: Any, cell: Any, high_resolution_limit: float
cls, map_grid: np.ndarray, *, spacegroup: Any, cell: CellType, high_resolution_limit: float
) -> Map:
"""
Create a `Map` from a 3d grid of voxel values stored in a numpy array.
Expand Down
11 changes: 9 additions & 2 deletions meteor/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import Literal, overload

import gemmi
import numpy as np
import reciprocalspaceship as rs
from pandas import Index
from reciprocalspaceship import DataSet
from reciprocalspaceship.decorators import cellify, spacegroupify
from reciprocalspaceship.utils import canonicalize_phases

CellType = Sequence[float] | np.ndarray | gemmi.UnitCell
SpacegroupType = str | int | gemmi.SpaceGroup


class ShapeMismatchError(Exception): ...

Expand Down Expand Up @@ -147,11 +152,13 @@ def complex_array_to_rs_dataseries(
return amplitudes, phases


@cellify("cell")
@spacegroupify("spacegroup")
def numpy_array_to_map(
array: np.ndarray,
*,
spacegroup: str | int | gemmi.SpaceGroup,
cell: tuple[float, float, float, float, float, float] | gemmi.UnitCell,
spacegroup: SpacegroupType,
cell: CellType,
) -> gemmi.Ccp4Map:
ccp4_map = gemmi.Ccp4Map()
ccp4_map.grid = gemmi.FloatGrid(array.astype(np.float32))
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_rsmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def test_compute_dhkl(noise_free_map: Map) -> None:
assert np.min(d_hkl) == 1.0
assert d_hkl.shape == noise_free_map.amplitudes.shape

noise_free_map.cell = None
with pytest.raises(AttributeError):
_ = noise_free_map.compute_dHKL()


def test_resolution_limits(random_difference_map: Map) -> None:
dmax, dmin = random_difference_map.resolution_limits
Expand Down

0 comments on commit 27606e5

Please sign in to comment.