1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
# coding: utf-8
from __future__ import annotations
import datetime
import os
import numpy
import h5py
from tomoscan.io import HDF5File
class MockBlissAcquisition:
"""
:param n_sequence: number of sequence to create
:param n_scan_per_sequence: number of scans (projection series) per sequence
:param n_projections_per_scan: number of projection frame in a scan
:param n_darks: number of dark frame in the series. Only one series at the
beginning
:param n_flats: number of flats to create. In this case will only
create one series of n flats after dark if any
:param output_dir: will contain the proposal file and one folder per
sequence.
:param str acqui_type: acquisition type. Can be "basic", "zseries-v1", "zseries-v3
:param Iterable z_values: if acqui_type is zseries then users should
provide the serie of values for z (one per stage)
:param nb_loop: number of pcotomo loop for v1 of bliss pcotomo
:param nb_tomo: number of tomo per loop for v1 of bliss pcotomo
:param nb_turns: number of turns for v2 of bliss pcotomo ( <=> nb NXtomo to generate)
:param file_name_prefix: bliss file prefix name
:param file_name_z_fill: optional z fill for the file name index. If None then file index will not be 'z filled'
:param create_tomo_config: if True create the 'tomo_config' group under instrument which contains
metadata describing the acquisition (which dataset to read for rotation, translation ...)
"""
def __init__(
self,
n_sample,
n_sequence,
n_scan_per_sequence,
n_darks,
n_flats,
output_dir,
with_nx_detector_attr=True,
detector_name="pcolinux",
acqui_type="basic",
z_values=None,
nb_loop=None,
nb_tomo=None,
nb_turns=None,
with_rotation_motor_info=True,
frame_data_type=numpy.uint16,
file_name_prefix="sample",
file_name_z_fill=None,
create_tomo_config: bool = True,
ebs_tomo_version: str | None = None,
z_series_v_3_options=None,
):
self._n_darks = n_darks
self._n_flats = n_flats
self._n_scan_per_sequence = n_scan_per_sequence
self.__folder = output_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
self.__proposal_file = os.path.join(self.__folder, "ihproposal_file.h5")
if acqui_type not in ("pcotomo"):
if nb_loop is not None or nb_tomo is not None:
raise ValueError(
"nb_loop and nb_tomo are only handled by acqui_type: `pcotomo`"
)
else:
if not (
nb_turns is not None or (nb_loop is not None and nb_tomo is not None)
):
raise ValueError(
"nb_turns should be provided or nb_loop and nb_tomo must be provided"
)
# create sample
self.__samples = []
for sample_i in range(n_sample):
if file_name_z_fill is None:
dir_name = f"{file_name_prefix}_{sample_i}"
else:
dir_name = f"{file_name_prefix}_{str(sample_i).zfill(file_name_z_fill)}"
sample_dir = os.path.join(self.path, dir_name)
os.mkdir(sample_dir)
sample_file = os.path.join(sample_dir, dir_name + ".h5")
if acqui_type == "basic":
acqui_tomo = _BlissBasicTomo(
sample_dir=sample_dir,
sample_file=sample_file,
n_sequence=n_sequence,
n_scan_per_sequence=n_scan_per_sequence,
n_darks=n_darks,
n_flats=n_flats,
with_nx_detector_attr=with_nx_detector_attr,
detector_name=detector_name,
with_rotation_motor_info=with_rotation_motor_info,
frame_data_type=frame_data_type,
create_tomo_config=create_tomo_config,
ebs_tomo_version=ebs_tomo_version,
)
elif acqui_type == "pcotomo":
acqui_tomo = _BlissPCOTomo(
sample_dir=sample_dir,
sample_file=sample_file,
n_sequence=n_sequence,
n_scan_per_sequence=n_scan_per_sequence,
n_darks=n_darks,
n_flats=n_flats,
with_nx_detector_attr=with_nx_detector_attr,
detector_name=detector_name,
with_rotation_motor_info=with_rotation_motor_info,
nb_loop=nb_loop,
nb_tomo=nb_tomo,
nb_turns=nb_turns,
frame_data_type=frame_data_type,
create_tomo_config=create_tomo_config,
ebs_tomo_version=ebs_tomo_version,
)
elif acqui_type in ("z-series-v1", "z-series-v3"):
if z_values is None:
raise ValueError("for z-series z_values should be provided")
acqui_tomo = _BlissZseriesTomo(
sample_dir=sample_dir,
sample_file=sample_file,
n_sequence=n_sequence,
n_scan_per_sequence=n_scan_per_sequence,
n_darks=n_darks,
n_flats=n_flats,
with_nx_detector_attr=with_nx_detector_attr,
detector_name=detector_name,
z_values=z_values,
with_rotation_motor_info=with_rotation_motor_info,
frame_data_type=frame_data_type,
create_tomo_config=create_tomo_config,
ebs_tomo_version=ebs_tomo_version,
z_series_version=acqui_type.split("-")[-1],
z_series_v_3_options=z_series_v_3_options,
)
else:
raise NotImplementedError("")
self.__samples.append(acqui_tomo)
@property
def samples(self):
return self.__samples
@property
def proposal_file(self):
# for now a simple file
return self.__proposal_file
@property
def path(self):
return self.__folder
class _BlissSample:
"""
Simple mock of a bliss sample. For now we only create the hierarchy of
files.
"""
def __init__(
self,
sample_dir,
sample_file,
n_sequence,
n_scan_per_sequence,
n_darks,
n_flats,
detector_name,
with_nx_detector_attr=True,
with_rotation_motor_info=True,
frame_data_type=numpy.uint16,
create_tomo_config: bool = True,
ebs_tomo_version: str | None = None,
):
self._with_nx_detector_attr = with_nx_detector_attr
self._sample_dir = sample_dir
self._sample_file = sample_file
self._n_sequence = n_sequence
self._n_scan_per_seq = n_scan_per_sequence
self._n_darks = n_darks
self._n_flats = n_flats
self._scan_folders = []
self._index = 1
self._detector_name = detector_name
self._det_width = 64
self._det_height = 64
self._tomo_n = 10
self._energy = 19.0
self._sample_detector_distance = 100.0 # in mm
self._source_sample_distance = 52000.0 # in mm
self._pixel_size = (0.0065, 0.0066)
self._with_rotation_motor_info = with_rotation_motor_info
self._frame_data_type = frame_data_type
self._create_tomo_config = create_tomo_config
self._ebs_tomo_version = ebs_tomo_version
for _ in range(n_sequence):
self.add_sequence()
@property
def frame_data_type(self):
return self._frame_data_type
def get_next_free_index(self):
idx = self._index
self._index += 1
return idx
@property
def current_scan_index(self) -> int:
return self._index - 1
def get_main_entry_title(self):
raise NotImplementedError("Base class")
@staticmethod
def get_title(scan_type):
if scan_type == "dark":
return "dark images"
elif scan_type == "flat":
return "reference images 1"
elif scan_type == "projection":
return "projections 1 - 2000"
else:
raise ValueError("Not implemented")
def create_entry_and_technique(self, seq_ini_index):
# add sequence init information
with HDF5File(self.sample_file, mode="a") as h5f:
seq_node = h5f.require_group(str(seq_ini_index) + ".1")
seq_node.attrs["NX_class"] = "NXentry"
seq_node["title"] = self.get_main_entry_title()
seq_node.require_group("instrument/positioners")
# write energy
seq_node["technique/scan/energy"] = self._energy
seq_node["technique/scan/tomo_n"] = self._tomo_n * self._n_scan_per_seq
seq_node["technique/scan/sample_detector_distance"] = (
self._sample_detector_distance
)
seq_node["technique/scan/sample_detector_distance"].attrs["units"] = "mm"
seq_node["technique/scan/source_sample_distance"] = (
self._source_sample_distance
)
seq_node["technique/scan/source_sample_distance"].attrs["units"] = "mm"
seq_node["technique/detector/pixel_size"] = numpy.asarray(self._pixel_size)
seq_node["start_time"] = str(datetime.datetime.now())
seq_node["end_time"] = str(
datetime.datetime.now() + datetime.timedelta(minutes=10)
)
if self._create_tomo_config:
self._add_tomo_config(seq_node)
@staticmethod
def get_next_group_name(seq_ini_index, scan_idx):
return str(scan_idx) + ".1"
def add_scan(
self,
scan_type,
seq_ini_index,
z_value,
skip_title=False,
nb_loop=None,
nb_tomo=None,
nb_turns=1,
):
"""
:param nb_loop: number of loop in pcotomo use case. Else must be 1
:param nb_tomo: number of tomography done in pcotomo 'per iteration' use case. Else must be 1
"""
scan_idx = self.get_next_free_index()
scan_name = str(scan_idx).zfill(4)
scan_path = os.path.join(self.path, scan_name)
self._scan_folders.append(_BlissScan(folder=scan_path, scan_type=scan_type))
if nb_turns is not None:
nb_nxtomo = nb_turns
if nb_tomo is not None or nb_loop is not None:
raise ValueError(
"nb_tomo and nb_loop should be provided or nb_turns. Not both"
)
elif nb_loop is not None and nb_tomo is not None:
nb_nxtomo = nb_loop * nb_tomo
if nb_turns is not None:
raise ValueError(
"nb_tomo and nb_loop should be provided or nb_turns. Not both"
)
else:
raise ValueError(
"nb_tomo and nb_loop should be provided or nb_turns. None provided"
)
# register the scan information
with HDF5File(self.sample_file, mode="a") as h5f:
seq_node = h5f.require_group(str(scan_idx) + ".1")
if "start_time" not in seq_node:
seq_node["start_time"] = str(datetime.datetime.now())
# write title
title = self.get_title(scan_type=scan_type)
if not skip_title:
seq_node["title"] = title
# write data
data = (
numpy.random.random(
self._det_height * self._det_width * self._tomo_n * nb_nxtomo
)
* 256
)
n_frames = self._tomo_n * nb_nxtomo
data = data.reshape(n_frames, self._det_height, self._det_width)
data = data.astype(self.frame_data_type)
det_path_1 = "/".join(("instrument", self._detector_name))
det_grp = seq_node.require_group(det_path_1)
det_grp["data"] = data
if self._with_nx_detector_attr:
det_grp.attrs["NX_class"] = "NXdetector"
acq_grp = det_grp.require_group("acq_parameters")
acq_grp["acq_expo_time"] = 4
det_path_2 = "/".join(("technique", "scan", self._detector_name))
seq_node[det_path_2] = data
seq_node.attrs["NX_class"] = "NXentry"
# write rotation angle value and translations
instrument_group = seq_node.require_group("instrument")
positioners_grp = instrument_group.require_group("positioners")
positioners_grp["hrsrot"] = numpy.linspace(
start=0.0, stop=360, num=n_frames
)
positioners_grp["sx"] = numpy.array(numpy.random.random(size=n_frames))
positioners_grp["sy"] = numpy.random.random(size=n_frames)
positioners_grp["sz"] = numpy.asarray([z_value] * n_frames)
positioners_grp["yrot"] = numpy.random.random(size=n_frames)
if self._with_rotation_motor_info:
scan_node = seq_node.require_group("technique/scan")
scan_node["motor"] = ("rotation", "hrsrot", "srot")
if self._ebs_tomo_version is not None:
technique_group = seq_node.require_group("technique")
technique_group.attrs["tomo_version"] = self._ebs_tomo_version
def _add_tomo_config(self, group: h5py.Group):
technique_group = group.require_group("technique")
tomo_config_group = technique_group.require_group("tomoconfig")
tomo_config_group["rotation"] = ["hrsrot"]
tomo_config_group["detector"] = [
self._detector_name,
]
tomo_config_group["sample_x"] = ["sx"]
tomo_config_group["sample_y"] = ["sy"]
tomo_config_group["translation_z"] = ["sz"]
tomo_config_group["translation_y"] = ["yrot"]
def add_sequence(self):
"""Add a sequence to the bliss file"""
raise NotImplementedError("Base class")
@property
def path(self):
return self._sample_dir
@property
def sample_directory(self):
return self._sample_dir
@property
def sample_file(self):
return self._sample_file
def scans_folders(self):
return self._scan_folders
@property
def n_darks(self):
return self._n_darks
@property
def with_rotation_motor_info(self):
return self._with_rotation_motor_info
class _BlissScan:
"""
mock of a bliss scan
"""
def __init__(self, folder, scan_type: str):
assert scan_type in ("dark", "flat", "projection")
self.__path = folder
def path(self):
return self.__path
class _BlissBasicTomo(_BlissSample):
def get_main_entry_title(self):
return "tomo:fullturn"
def add_sequence(self):
# reserve the index for the 'initialization' sequence. No scan folder
# will be created for this one.
seq_ini_index = self.get_next_free_index()
self.create_entry_and_technique(seq_ini_index=seq_ini_index)
if self.n_darks > 0:
self.add_scan(scan_type="dark", seq_ini_index=seq_ini_index, z_value=1)
if self._n_flats > 0:
self.add_scan(scan_type="flat", seq_ini_index=seq_ini_index, z_value=1)
for _ in range(self._n_scan_per_seq):
self.add_scan(
scan_type="projection", seq_ini_index=seq_ini_index, z_value=1
)
class _BlissPCOTomo(_BlissSample):
def __init__(
self,
sample_dir,
sample_file,
n_sequence,
n_scan_per_sequence,
n_darks,
n_flats,
detector_name,
with_nx_detector_attr=True,
with_rotation_motor_info=True,
nb_loop=None,
nb_tomo=None,
nb_turns=1,
frame_data_type=numpy.uint16,
create_tomo_config: bool = True,
ebs_tomo_version=None,
):
self.nb_loop = nb_loop
self.nb_tomo = nb_tomo
self.nb_turns = nb_turns
super().__init__(
sample_dir,
sample_file,
n_sequence,
n_scan_per_sequence,
n_darks,
n_flats,
detector_name,
with_nx_detector_attr,
with_rotation_motor_info=with_rotation_motor_info,
frame_data_type=frame_data_type,
create_tomo_config=create_tomo_config,
ebs_tomo_version=ebs_tomo_version,
)
if nb_loop is not None and nb_tomo is not None:
if nb_turns is not None:
raise ValueError(
"All of nb_loop, nb_tomo and nb_turns provided. Unable to deduce the pcotomo version"
)
pcotomo_version = 1
elif nb_turns is not None:
pcotomo_version = 2
else:
pcotomo_version = None
if pcotomo_version is not None:
# write Bliss version in attrs
with HDF5File(self.sample_file, mode="a") as h5f:
if "creator_version" not in h5f.attrs:
if pcotomo_version == 1:
h5f.attrs["creator_version"] = "1.2.3"
if pcotomo_version == 2:
h5f.attrs["creator_version"] = "1.10.0"
def get_main_entry_title(self):
return "tomo:pcotomo"
def add_sequence(self):
# reserve the index for the 'initialization' sequence. No scan folder
# will be created for this one.
seq_ini_index = self.get_next_free_index()
self.create_entry_and_technique(seq_ini_index=seq_ini_index)
# start dark
if self.n_darks > 0:
self.add_scan(scan_type="dark", seq_ini_index=seq_ini_index, z_value=1)
# start flat
if self._n_flats > 0:
self.add_scan(scan_type="flat", seq_ini_index=seq_ini_index, z_value=1)
for _ in range(self._n_scan_per_seq):
self.add_scan(
scan_type="projection",
seq_ini_index=seq_ini_index,
z_value=1,
nb_loop=self.nb_loop,
nb_tomo=self.nb_tomo,
nb_turns=self.nb_turns,
)
# end flat
if self._n_flats > 0:
self.add_scan(scan_type="flat", seq_ini_index=seq_ini_index, z_value=1)
def add_scan(
self,
scan_type,
seq_ini_index,
z_value,
skip_title=False,
nb_loop=None,
nb_tomo=None,
nb_turns=1,
):
super().add_scan(
scan_type, seq_ini_index, z_value, skip_title, nb_loop, nb_tomo, nb_turns
)
if scan_type == "projection":
# register pcotomo specific informations (only for projections)
with HDF5File(self.sample_file, mode="a") as h5f:
seq_node = h5f.require_group(str(self._index - 1) + ".1")
scan_grp = seq_node.require_group("technique/proj")
if nb_loop is not None and "nb_loop" not in scan_grp:
scan_grp["nb_loop"] = nb_loop
if nb_tomo is not None and "nb_tomo" not in scan_grp:
scan_grp["nb_tomo"] = nb_tomo
if nb_turns is not None and "nb_turns" not in scan_grp:
scan_grp["nb_turns"] = nb_turns
if "tomo_n" not in scan_grp:
scan_grp["tomo_n"] = self._tomo_n
class _BlissZseriesTomo(_BlissSample):
def __init__(
self,
sample_dir,
sample_file,
n_sequence,
n_scan_per_sequence,
n_darks,
n_flats,
detector_name,
z_values,
z_series_version: str,
with_nx_detector_attr=True,
with_rotation_motor_info=True,
frame_data_type=numpy.uint16,
create_tomo_config: bool = True,
ebs_tomo_version: str = None,
z_series_v_3_options=None,
):
assert z_series_version in ("v1", "v3")
self.z_series_version = z_series_version
self._z_values = z_values
self._z_series_v_3_options = z_series_v_3_options
super().__init__(
sample_dir=sample_dir,
sample_file=sample_file,
n_sequence=n_sequence,
n_scan_per_sequence=n_scan_per_sequence,
n_darks=n_darks,
n_flats=n_flats,
detector_name=detector_name,
with_nx_detector_attr=with_nx_detector_attr,
with_rotation_motor_info=with_rotation_motor_info,
frame_data_type=frame_data_type,
create_tomo_config=create_tomo_config,
ebs_tomo_version=ebs_tomo_version,
)
def get_main_entry_title(self):
return "tomo:zseries"
def create_dark_at_start(self) -> bool:
if self.z_series_version == "v1":
return True
else:
return self._z_series_v_3_options["dark_at_start"]
def create_flat_at_start(self) -> bool:
if self.z_series_version == "v1":
return True
else:
return self._z_series_v_3_options["flat_at_start"]
def create_dark_at_end(self) -> bool:
if self.z_series_version == "v1":
return True
else:
return self._z_series_v_3_options["dark_at_end"]
def create_flat_at_end(self) -> bool:
if self.z_series_version == "v1":
return True
else:
return self._z_series_v_3_options["flat_at_end"]
def create_intermediary_flat(self) -> bool:
return self.z_series_version == "v1"
def create_intermediary_dark(self) -> bool:
return self.z_series_version == "v1"
def add_sequence(self):
# reserve the index for the 'initialization' sequence. No scan folder
# will be created for this one.
if self.z_series_version == "v1":
seq_ini_index = self.get_next_free_index()
self.create_entry_and_technique(seq_ini_index=seq_ini_index)
for z_value in self._z_values:
if self.z_series_version == "v3":
seq_ini_index = self.get_next_free_index()
self.create_entry_and_technique(seq_ini_index=seq_ini_index)
if z_value == self._z_values[0]:
create_dark = self.create_dark_at_start()
create_flat = self.create_flat_at_start()
elif z_value == self._z_values[-1]:
create_dark = self.create_dark_at_end()
create_flat = self.create_flat_at_end()
else:
create_dark = self.create_intermediary_dark()
create_flat = self.create_intermediary_flat()
if create_dark and self.n_darks > 0:
self.add_scan(
scan_type="dark", seq_ini_index=seq_ini_index, z_value=z_value
)
if create_flat and self._n_flats > 0:
self.add_scan(
scan_type="flat", seq_ini_index=seq_ini_index, z_value=z_value
)
for _ in range(self._n_scan_per_seq):
self.add_scan(
scan_type="projection", seq_ini_index=seq_ini_index, z_value=z_value
)
def create_entry_and_technique(self, seq_ini_index):
super().create_entry_and_technique(seq_ini_index=seq_ini_index)
# add sequence init information
if self.z_series_version == "v3":
with HDF5File(self.sample_file, mode="a") as h5f:
seq_node = h5f.require_group(str(seq_ini_index) + ".1")
seq_node.attrs["NX_class"] = "NXentry"
# write scab flags
seq_node["technique/scan_flags/dark_images_at_start"] = (
self._z_series_v_3_options["dark_at_start"]
)
seq_node["technique/scan_flags/dark_images_at_end"] = (
self._z_series_v_3_options["dark_at_end"]
)
seq_node["technique/scan_flags/ref_images_at_start"] = (
self._z_series_v_3_options["flat_at_start"]
)
seq_node["technique/scan_flags/ref_images_at_end"] = (
self._z_series_v_3_options["flat_at_end"]
)
def add_scan(
self,
scan_type,
seq_ini_index,
z_value,
skip_title=False,
nb_loop=None,
nb_tomo=None,
nb_turns=1,
):
super().add_scan(
scan_type, seq_ini_index, z_value, skip_title, nb_loop, nb_tomo, nb_turns
)
with HDF5File(self.sample_file, mode="a") as h5f:
seq_node = h5f.require_group(str(self.current_scan_index) + ".1")
seq_node["sample/name"] = "mysample_0000"
|