Skip to content

Commit

Permalink
Move HidraProjectFileMode into separate file refs #219
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Dec 19, 2019
1 parent c0e923d commit 8bebdbd
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 47 deletions.
3 changes: 2 additions & 1 deletion pyrs/core/powder_pattern.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pyrs.core import reduction_manager
from pyrs.utilities import checkdatatypes
from pyrs.core import mask_util
from pyrs.projectfile import HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from matplotlib import pyplot as plt


Expand Down
4 changes: 2 additions & 2 deletions pyrs/core/pyrscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from pyrs.core import instrument_geometry
from pyrs.utilities import file_util
from pyrs.peaks import PeakFitEngineFactory, SupportedPeakProfiles, SupportedBackgroundTypes
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from pyrs.core import strain_stress_calculator
from pyrs.core import reduction_manager
from pyrs.core import polefigurecalculator
Expand Down
4 changes: 2 additions & 2 deletions pyrs/core/reduction_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from pyrs.core import mask_util
from pyrs.core import reduce_hb2b_mtd
from pyrs.core import reduce_hb2b_pyrs
from pyrs.projectfile import HidraConstants
from pyrs.projectfile import HidraConstants, HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities import checkdatatypes
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile


class HB2BReductionManager(object):
Expand Down
4 changes: 3 additions & 1 deletion pyrs/projectfile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# flake8: noqa
# pylint: disable=undefined-variable
from .constants import HidraConstants
from .file_mode import HidraProjectFileMode

__all__ = ['HidraConstants']
__all__ = constants.__all__ + file_mode.__all__
3 changes: 3 additions & 0 deletions pyrs/projectfile/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__all__ = ['HidraConstants']


class HidraConstants(object):
"""
Constants used for Hidra project file, workspace and related dictionary
Expand Down
37 changes: 37 additions & 0 deletions pyrs/projectfile/file_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is rs_scan_io.DiffractionFile's 2.0 version
from enum import Enum

__all__ = ['HidraProjectFileMode']


class HidraProjectFileMode(Enum):
'''
Enumeration for file access mode
These values match the strings of :py:obj:`h5py.File`
'''
READONLY = 'r' # read-only
READWRITE = 'a' # read and write
OVERWRITE = 'w' # new file

def __str__(self):
return self.value

@staticmethod
def getMode(mode):
'''Private function to convert anything into :py:obj:`HidraProjectFileMode`'''
if mode in HidraProjectFileMode:
return mode # already a member of the enum
else:
# all other checks are for strings
# the first checks are against nominal values
# the last check is against the enum names
mode = str(mode).lower()
if mode in ['a', 'rw']:
return HidraProjectFileMode.READWRITE
elif mode == 'r':
return HidraProjectFileMode.READONLY
elif mode == 'w':
return HidraProjectFileMode.OVERWRITE
else:
return HidraProjectFileMode[mode.upper()]
26 changes: 2 additions & 24 deletions pyrs/utilities/rs_project_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,9 @@
from pyrs.core.instrument_geometry import AnglerCameraDetectorGeometry, HidraSetup
from pyrs.peaks import PeakCollection
from pyrs.dataobjects import SampleLogs
from pyrs.projectfile import HidraConstants
from pyrs.projectfile import HidraConstants, HidraProjectFileMode

__all__ = ['HidraProjectFileMode', 'DiffractionUnit', 'HidraProjectFile']


class HidraProjectFileMode(Enum):
'''
Enumeration for file access mode
These values match the strings of :py:obj:`h5py.File`
'''
READONLY = 'r' # read-only
READWRITE = 'a' # read and write
OVERWRITE = 'w' # new file

def __str__(self):
return self.value

@staticmethod
def getMode(mode):
'''Private function to convert anything into :py:obj:`HidraProjectFileMode`'''
if mode in HidraProjectFileMode:
return mode
else:
return HidraProjectFileMode[str(mode)]
__all__ = ['DiffractionUnit', 'HidraProjectFile']


class DiffractionUnit(Enum):
Expand Down
4 changes: 2 additions & 2 deletions scripts/preparetest/convert_hb2b_peaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
import numpy
from pyrs.utilities import rs_scan_io
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile


def main():
Expand All @@ -24,7 +24,7 @@ def main():

# Create a Hidra project
target_project_file_name = 'tests/testdata/Hydra_16-1_cor_log.h5'
target_file = HidraProjectFile(target_project_file_name, HidraProjectFileMode.OVERWRITE)
target_file = HidraProjectFile(target_project_file_name, 'w')

# Create sub runs
target_file.write_sub_runs(sorted(diff_data_dict.keys()))
Expand Down
3 changes: 2 additions & 1 deletion scripts/preparetest/convert_strain_stress_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# System (library) test for classes and methods that will be used to calculate strain and stress
import os
import sys
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile


def contain_strain_stress_main():
Expand Down
3 changes: 2 additions & 1 deletion scripts/pyrs_calibration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy
import time
from pyrs.projectfile import HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from pyrs.calibration import peakfit_calibration

# DEFAULT VALUES FOR DATA PROCESSING
Expand Down
9 changes: 4 additions & 5 deletions tests/data/convert_xray_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
Note: most of the methods to parse HZB data are copied from script pyrscalibration.py
"""
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from pyrs.core.instrument_geometry import HidraSetup
import numpy
from skimage import io
from PIL import Image
import numpy as np
Expand Down Expand Up @@ -96,8 +95,8 @@ def main():

# add sample log data & sub runs
# for log_name in ['sub-run', '2Theta']
project_file.append_experiment_log(HidraConstants.SUB_RUNS, numpy.array([1]))
project_file.append_experiment_log(HidraConstants.TWO_THETA, numpy.array([35.]))
project_file.append_experiment_log(HidraConstants.SUB_RUNS, np.array([1]))
project_file.append_experiment_log(HidraConstants.TWO_THETA, np.array([35.]))

# add instrument information
instrument_setup = generate_xray_instrument()
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/test_calibration_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import time
import os
import json
from pyrs.projectfile import HidraProjectFileMode
from pyrs.utilities import calibration_file_io
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
try:
from pyrs.calibration import peakfit_calibration
except ImportError as e:
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_project_file_rw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pyrs.core import workspaces
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from pyrs.core.instrument_geometry import HidraSetup
import os
import pytest
Expand All @@ -15,15 +15,15 @@ def test_rw_raw():
raw_project_name = os.path.join(os.getcwd(), 'data/HZB_Raw_Project.h5')

# Read to workspace
source_project = HidraProjectFile(raw_project_name, HidraProjectFileMode.READONLY)
source_project = HidraProjectFile(raw_project_name, 'r')

# To the workspace
source_workspace = workspaces.HidraWorkspace('Source HZB')
source_workspace.load_hidra_project(source_project, load_raw_counts=True,
load_reduced_diffraction=False)

# Export
target_project = HidraProjectFile('HZB_HiDra_Test.h5', HidraProjectFileMode.OVERWRITE)
target_project = HidraProjectFile('HZB_HiDra_Test.h5', 'w')
# Experiment data
source_workspace.save_experimental_data(target_project, sub_runs=range(1, 41))

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_reduction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
from pyrs.utilities import calibration_file_io
from pyrs.core import workspaces
import pytest
Expand Down Expand Up @@ -27,7 +27,7 @@ def test_calibration_json():
assert status == 3

# Import project file
project_file = HidraProjectFile(project_file_name, HidraProjectFileMode.READONLY)
project_file = HidraProjectFile(project_file_name, 'r')

# Reduce
test_workspace = workspaces.HidraWorkspace('test calibration')
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_hidra_project_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Test for reading and writing components to HiDRA project file
"""
from pyrs.projectfile import HidraConstants
from pyrs.utilities.rs_project_file import HidraProjectFile, HidraProjectFileMode
from pyrs.projectfile import HidraConstants, HidraProjectFileMode
from pyrs.utilities.rs_project_file import HidraProjectFile
import os
import numpy as np
import datetime
Expand Down

0 comments on commit 8bebdbd

Please sign in to comment.