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

Refactor and Ongoing Development in API Module #165

Merged
merged 4 commits into from
Apr 24, 2024
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
6 changes: 3 additions & 3 deletions brkraw/api/analyzer/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def __init__(self, infoobj: 'ScanInfo', fileobj: Union[BufferedReader, ZipExtFil
def _parse_info(self, infoobj: 'ScanInfo'):
if not hasattr(infoobj, 'dataarray'):
raise AttributeError
self.slope = infoobj.dataarray['2dseq_slope']
self.offset = infoobj.dataarray['2dseq_offset']
self.dtype = infoobj.dataarray['2dseq_dtype']
self.slope = infoobj.dataarray['slope']
self.offset = infoobj.dataarray['offset']
self.dtype = infoobj.dataarray['dtype']
self.shape = infoobj.image['shape'][:]
self.shape_desc = infoobj.image['dim_desc'][:]
if infoobj.frame_group and infoobj.frame_group['type']:
Expand Down
1 change: 1 addition & 0 deletions brkraw/api/analyzer/scaninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self,
self._set_pars(pvobj, reco_id)
if not debug:
self.info_protocol = helper.Protocol(self).get_info()
self.info_fid = helper.FID(self).get_info()
if self.visu_pars:
self._parse_info()

Expand Down
2 changes: 1 addition & 1 deletion brkraw/api/brkobj/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, pvscan: 'PvScan', reco_id: Optional[int] = None,
def set_info(self):
self.info = self.get_info(self.reco_id)

def get_info(self, reco_id:int, get_analyzer:bool = False):
def get_info(self, reco_id:Optional[int] = None, get_analyzer:bool = False):
infoobj = ScanInfo()
pvscan = self.retrieve_pvscan()
analysed = ScanInfoAnalyzer(pvscan, reco_id, self.is_debug)
Expand Down
3 changes: 2 additions & 1 deletion brkraw/api/helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .slicepack import SlicePack
from .cycle import Cycle
from .orientation import Orientation, to_matvec, from_matvec, rotate_affine
from .fid import FID

__all__ = [Protocol, FrameGroup, DataArray, Image, SlicePack, Cycle, Orientation,
__all__ = [Protocol, FID, FrameGroup, DataArray, Image, SlicePack, Cycle, Orientation,
to_matvec, from_matvec, rotate_affine]
11 changes: 11 additions & 0 deletions brkraw/api/helper/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import warnings
from functools import partial

WORDTYPE = \
dict(_32BIT_SGN_INT = 'i',
_16BIT_SGN_INT = 'h',
_8BIT_UNSGN_INT = 'B',
_32BIT_FLOAT = 'f')

BYTEORDER = \
dict(littleEndian = '<',
bigEndian = '>')


def is_all_element_same(listobj):
if listobj is None:
return True
Expand Down
32 changes: 6 additions & 26 deletions brkraw/api/helper/dataarray.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
from __future__ import annotations
import numpy as np
from typing import TYPE_CHECKING
from .base import BaseHelper, is_all_element_same
from .base import BaseHelper, is_all_element_same, BYTEORDER, WORDTYPE
if TYPE_CHECKING:
from ..analyzer import ScanInfoAnalyzer


WORDTYPE = \
dict(_32BIT_SGN_INT = 'i',
_16BIT_SGN_INT = 'h',
_8BIT_UNSGN_INT = 'B',
_32BIT_FLOAT = 'f')

BYTEORDER = \
dict(littleEndian = '<',
bigEndian = '>')


class DataArray(BaseHelper):
"""requires visu_pars and aqcp to pars parameter related to the dtype of binary files

Expand All @@ -29,17 +18,8 @@ class DataArray(BaseHelper):
"""
def __init__(self, analobj: 'ScanInfoAnalyzer'):
super().__init__()
acqp = analobj.acqp
visu_pars = analobj.visu_pars

if acqp:
fid_word_type = f'_{"".join(acqp["ACQ_word_size"].split("_"))}_SGN_INT'
fid_byte_order = f'{acqp["BYTORDA"]}Endian'
self.fid_dtype = np.dtype(f'{BYTEORDER[fid_byte_order]}{WORDTYPE[fid_word_type]}')
else:
self.fid_dtype = None
self._warn("Failed to fetch 'fid_dtype' information because the 'acqp' file is missing from 'analobj'.")

byte_order = visu_pars["VisuCoreByteOrder"]
word_type = visu_pars["VisuCoreWordType"]
self.data_dtype = np.dtype(f'{BYTEORDER[byte_order]}{WORDTYPE[word_type]}')
Expand All @@ -53,13 +33,13 @@ def __init__(self, analobj: 'ScanInfoAnalyzer'):

if isinstance(self.data_slope, list) or isinstance(self.data_offset, list):
self._warn("Data slope and data offset values are unusual. "
"They are expected to be either a list containing the same elements or a single float value.")
"They are expected to be either a list containing the same elements or a single float value.")


def get_info(self):
return {
'fid_dtype': self.fid_dtype,
'2dseq_dtype': self.data_dtype,
'2dseq_slope': self.data_slope,
'2dseq_offset': self.data_offset,
'dtype': self.data_dtype,
'slope': self.data_slope,
'offset': self.data_offset,
'warns': self.warns
}
36 changes: 36 additions & 0 deletions brkraw/api/helper/fid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations
import numpy as np
from typing import TYPE_CHECKING
from .base import BaseHelper, is_all_element_same, BYTEORDER, WORDTYPE
if TYPE_CHECKING:
from ..analyzer import ScanInfoAnalyzer


class FID(BaseHelper):
"""requires visu_pars and aqcp to pars parameter related to the dtype of binary files

Dependencies:
acqp
visu_pars

Args:
BaseHelper (_type_): _description_
"""
def __init__(self, analobj: 'ScanInfoAnalyzer'):
super().__init__()
acqp = analobj.acqp

if acqp:
word_type = f'_{"".join(acqp["ACQ_word_size"].split("_"))}_SGN_INT'
byte_order = f'{acqp["BYTORDA"]}Endian'
self.dtype = np.dtype(f'{BYTEORDER[byte_order]}{WORDTYPE[word_type]}')
else:
self.fid_dtype = None
self._warn("Failed to fetch 'fid_dtype' information because the 'acqp' file is missing from 'analobj'.")


def get_info(self):
return {
'dtype': self.dtype,
'warns': self.warns
}
1 change: 1 addition & 0 deletions brkraw/api/helper/orientation.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def _calc_eulerangle(matrix):
def _get_gradient_encoding_dir(cls, visu_pars):
if visu_pars["VisuVersion"] != 1:
return visu_pars["VisuAcqGradEncoding"]
# routine for PV version < 6
phase_enc = visu_pars["VisuAcqImagePhaseEncDir"]
phase_enc = phase_enc[0] if is_all_element_same(phase_enc) else phase_enc
return (
Expand Down
Empty file added brkraw/app/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ dependencies = [
'pillow>=7.1.1',
'tqdm>=4.45.0',
'openpyxl>=3.0.3',
'xlrd>=1.0.0'
'xlrd>=1.0.0',
'toml>=0.10.2'
]
description = "Bruker PvDataset Loader"
license = {text = "GNLv3"}
Expand Down