GitLab will be upgraded on June 23rd evening. During the upgrade the service will be unavailable, sorry for the inconvenience.

Commit 8a977557 authored by payno's avatar payno

[esrf][utils] add utils functions for groups (as zseries)

parent 1fe384e4
......@@ -31,8 +31,13 @@ __date__ = "10/10/2019"
import os
import fabio
from silx.io.url import DataUrl
from tomoscan.scanbase import TomoScanBase
from typing import Union
from typing import Iterable
import numpy
import logging
_logger = logging.getLogger(__name__)
def get_parameters_frm_par_or_info(file_: str) -> dict:
......@@ -172,3 +177,71 @@ def get_compacted_dataslices(urls):
)
)
return res
def from_sequences_to_grps(scans: Iterable) -> tuple:
"""
create group with the same sample name
:param Iterable scans:
:return: tuple of group of scans
"""
grps = {}
for scan in scans:
if not isinstance(scan, TomoScanBase):
raise TypeError("Elements are expected to be instance of " "TomoScanBase")
if scan.sample_name in grps:
grps[scan.sample_name].append(scan)
else:
grps[scan.sample_name] = [
scan,
]
return tuple(grps.values())
def check_grp_is_valid(scans: Iterable):
"""
Insure the provided group of scan is valid. Otherwise raise an error
:param Iterable scans: group of TomoScanBAse to check
"""
l_scans = set()
for scan in scans:
if not isinstance(scan, TomoScanBase):
raise TypeError("Elements are expected to be instance of " "TomoScanBase")
if scan in l_scans:
raise ValueError("{} is present at least twice")
elif len(l_scans) > 0:
if list(l_scans)[0].sample_name != scan.sample_name:
raise ValueError(
"{} and {} are from two different sample".format(scan, l_scans)
)
l_scans.add(scan)
def grp_is_complete(scans: Iterable) -> bool:
"""
Insure the provided group of scan is valid. Otherwise raise an error
:param Iterable scans: group of TomoScanBAse to check
:return: True if the group is complete
:rtype: bool
"""
if len(scans) == 0:
return True
try:
check_grp_is_valid(scans=scans)
except Exception as e:
_logger.error("provided group is invalid. {}".format(e))
else:
group_size = list(scans)[0].group_size
if group_size is None:
_logger.warning("No information found regarding group size")
return True
elif group_size == len(scans):
return True
elif group_size < len(scans):
_logger.warning("more scans found than group_size")
return True
else:
return False
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment