Skip to content

Commit

Permalink
adds script to import cctbx data to rs (#264)
Browse files Browse the repository at this point in the history
Co-authored-by: kmdalton <[email protected]>
  • Loading branch information
dermen and kmdalton committed Sep 20, 2024
1 parent 7882bee commit 5fcad3a
Show file tree
Hide file tree
Showing 7 changed files with 659 additions and 23 deletions.
2 changes: 1 addition & 1 deletion reciprocalspaceship/dtypes/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def round(self: T, decimals: int = 0, *args, **kwargs) -> T:

@wraps(libmissing.is_numeric_na)
def is_numeric_na(values):
allowed_dtypes = ("float32", "int32")
allowed_dtypes = ("float64", "float32", "int32")
if isinstance(values, np.ndarray) and values.dtype in allowed_dtypes:
return np.isnan(values)
return libmissing.is_numeric_na(values)
1 change: 1 addition & 0 deletions reciprocalspaceship/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from reciprocalspaceship.io.ccp4map import write_ccp4_map
from reciprocalspaceship.io.crystfel import read_crystfel
from reciprocalspaceship.io.csv import read_csv
from reciprocalspaceship.io.dials import print_refl_info, read_dials_stills
from reciprocalspaceship.io.mtz import (
from_gemmi,
read_cif,
Expand Down
48 changes: 48 additions & 0 deletions reciprocalspaceship/io/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
import warnings
from contextlib import contextmanager
from importlib.util import find_spec


def set_ray_loglevel(level):
logger = logging.getLogger("ray")
logger.setLevel(level)
for handler in logger.handlers:
handler.setLevel(level)


def check_for_ray():
has_ray = True
if find_spec("ray") is None:
has_ray = False

message = (
"ray (https://www.ray.io/) is not available..." "Falling back to serial."
)
warnings.warn(message, ImportWarning)
return has_ray


def check_for_mpi():
try:
from mpi4py import MPI

return True
except Exception as err:
message = (
f"Failed `from mpi4py import MPI` with {err}. Falling back to serial mode."
)
warnings.warn(message, ImportWarning)
return False


@contextmanager
def ray_context(log_level="DEBUG", **ray_kwargs):
import ray

set_ray_loglevel(log_level)
ray.init(**ray_kwargs)
try:
yield ray
finally:
ray.shutdown()
24 changes: 2 additions & 22 deletions reciprocalspaceship/io/crystfel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import mmap
import re
from contextlib import contextmanager
from importlib.util import find_spec
from typing import Union

import gemmi
import numpy as np

from reciprocalspaceship import DataSet, concat
from reciprocalspaceship.io.common import check_for_ray, ray_context
from reciprocalspaceship.utils import angle_between, eV2Angstroms

# See Rupp Table 5-2
Expand Down Expand Up @@ -60,17 +59,6 @@
}


@contextmanager
def ray_context(**ray_kwargs):
import ray

ray.init(**ray_kwargs)
try:
yield ray
finally:
ray.shutdown()


class StreamLoader(object):
"""
An object that loads stream files into rs.DataSet objects in parallel.
Expand Down Expand Up @@ -304,15 +292,7 @@ def read_crystfel(

# Check whether ray is available
if use_ray:
if find_spec("ray") is None:
use_ray = False
import warnings

message = (
"ray (https://www.ray.io/) is not available..."
"Falling back to serial stream file parser."
)
warnings.warn(message, ImportWarning)
use_ray = check_for_ray()

with open(self.filename, "r") as f:
memfile = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
Expand Down
Loading

0 comments on commit 5fcad3a

Please sign in to comment.