Skip to content

Commit d8ab50d

Browse files
authored
Fix ImgConverter type hints and FastADT negative osc_angle (#146)
* Improve type hints, convert paths to Path at input * Instead of allowing `AnyPath`, correctly hint all `Path`s * Correctly handle negative osc_angle at convertion * Everything works fine if only the oscillation angle must be positive
1 parent e4d83ca commit d8ab50d

2 files changed

Lines changed: 20 additions & 28 deletions

File tree

src/instamatic/experiments/fast_adt/experiment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def finalize(self) -> None:
436436

437437
img_conv = ImgConversion(
438438
buffer=self.run.buffer,
439-
osc_angle=self.run.osc_angle,
439+
osc_angle=abs(self.run.osc_angle),
440440
start_angle=self.run.table['alpha'].iloc[0],
441441
end_angle=self.run.table['alpha'].iloc[-1],
442442
rotation_axis=rotation_axis,

src/instamatic/processing/ImgConversion.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
from datetime import datetime
77
from pathlib import Path
8+
from typing import Optional
89

910
import numpy as np
1011

@@ -324,7 +325,7 @@ def write_geometric_correction_files(self, path) -> None:
324325
write_cbf(path / 'XCORR.cbf', np.int32(xcorr * 100))
325326
write_cbf(path / 'YCORR.cbf', np.int32(ycorr * 100))
326327

327-
def tiff_writer(self, path: str) -> None:
328+
def tiff_writer(self, path: Path) -> None:
328329
"""Write all data as tiff files to given `path`"""
329330
print('\033[k', 'Writing TIFF files......', end='\r')
330331

@@ -335,7 +336,7 @@ def tiff_writer(self, path: str) -> None:
335336

336337
logger.debug(f'Tiff files saved in folder: {path}')
337338

338-
def smv_writer(self, path: str) -> None:
339+
def smv_writer(self, path: Path) -> None:
339340
"""Write all data as SMV files compatible with XDS/DIALS to `path`"""
340341
print('\033[k', 'Writing SMV files......', end='\r')
341342

@@ -347,7 +348,7 @@ def smv_writer(self, path: str) -> None:
347348

348349
logger.debug(f'SMV files saved in folder: {path}')
349350

350-
def mrc_writer(self, path: str) -> None:
351+
def mrc_writer(self, path: Path) -> None:
351352
"""Write all data as mrc files to `path`"""
352353
print('\033[k', 'Writing MRC files......', end='\r')
353354

@@ -360,9 +361,9 @@ def mrc_writer(self, path: str) -> None:
360361

361362
def threadpoolwriter(
362363
self,
363-
tiff_path: str = None,
364-
smv_path: str = None,
365-
mrc_path: str = None,
364+
tiff_path: Optional[Path] = None,
365+
smv_path: Optional[Path] = None,
366+
mrc_path: Optional[Path] = None,
366367
workers: int = 8,
367368
) -> None:
368369
"""Efficiently write all data to the specified formats using a
@@ -403,7 +404,7 @@ def threadpoolwriter(
403404
for future in futures:
404405
ret = future.result()
405406

406-
def to_dials(self, smv_path: str) -> None:
407+
def to_dials(self, smv_path: Path) -> None:
407408
"""Convert the buffer to output compatible with DIALS.
408409
409410
Files are written to the path given by `smv_path`.
@@ -444,7 +445,7 @@ def to_dials(self, smv_path: str) -> None:
444445
del self.data[n]
445446
del self.headers[n]
446447

447-
def write_tiff(self, path: str, i: int) -> str:
448+
def write_tiff(self, path: Path, i: int) -> Path:
448449
"""Write the image+header with sequence number `i` to the directory
449450
`path` in TIFF format.
450451
@@ -454,10 +455,10 @@ def write_tiff(self, path: str, i: int) -> str:
454455
h = self.headers[i]
455456

456457
fn = path / f'{i:05d}.tiff'
457-
write_tiff(fn, img, header=h)
458+
write_tiff(str(fn), img, header=h)
458459
return fn
459460

460-
def write_smv(self, path: str, i: int) -> str:
461+
def write_smv(self, path: Path, i: int) -> Path:
461462
"""Write the image+header with sequence number `i` to the directory
462463
`path` in SMV format.
463464
@@ -508,10 +509,10 @@ def write_smv(self, path: str, i: int) -> str:
508509
header['DENZO_X_BEAM'] = f'{mean_beam_center[0] * self.physical_pixelsize:.4f}'
509510
header['DENZO_Y_BEAM'] = f'{mean_beam_center[1] * self.physical_pixelsize:.4f}'
510511
fn = path / f'{i:05d}.img'
511-
write_adsc(fn, img, header=header)
512+
write_adsc(str(fn), img, header=header)
512513
return fn
513514

514-
def write_mrc(self, path: str, i: int) -> str:
515+
def write_mrc(self, path: Path, i: int) -> Path:
515516
"""Write the image+header with sequence number `i` to the directory
516517
`path` in TIFF format.
517518
@@ -538,22 +539,13 @@ def write_mrc(self, path: str, i: int) -> str:
538539

539540
return fn
540541

541-
def write_ed3d(self, path: str) -> None:
542+
def write_ed3d(self, path: Path) -> None:
542543
"""Write .ed3d input file for REDp in directory `path`"""
543544
path.mkdir(exist_ok=True)
544545

545546
omega = np.degrees(self.rotation_axis)
546-
547-
# for red, -180 <= omega <= 180
548-
if omega < -180:
549-
omega += 360
550-
elif omega > 180:
551-
omega -= 360
552-
553-
if self.start_angle > self.end_angle:
554-
sign = -1
555-
else:
556-
sign = 1
547+
omega = ((omega + 180) % 360) - 180 # for red, -180 <= omega <= 180
548+
sign = -1 if self.start_angle > self.end_angle else +1
557549

558550
with open(path / '1.ed3d', 'w') as f:
559551
print(f'WAVELENGTH {self.wavelength}', file=f)
@@ -576,7 +568,7 @@ def write_ed3d(self, path: str) -> None:
576568

577569
logger.debug(f'ED3D file created in path: {path}')
578570

579-
def write_xds_inp(self, path: str) -> None:
571+
def write_xds_inp(self, path: Path) -> None:
580572
"""Write XDS.INP input file for XDS in directory `path`"""
581573

582574
path.mkdir(exist_ok=True)
@@ -638,7 +630,7 @@ def write_xds_inp(self, path: str) -> None:
638630

639631
logger.info('XDS INP file created.')
640632

641-
def write_beam_centers(self, path: str) -> None:
633+
def write_beam_centers(self, path: Path) -> None:
642634
"""Write list of beam centers to file `beam_centers.txt` in `path`"""
643635
centers = np.zeros((max(self.observed_range), 2), dtype=float)
644636
for i, h in self.headers.items():
@@ -679,7 +671,7 @@ def write_pets_inp(self, path: AnyPath, tiff_path: str = 'tiff') -> None:
679671
with open(Path(path) / 'pets.pts', 'w') as f:
680672
f.write(str(p.compile(self.__dict__)))
681673

682-
def write_REDp_shiftcorrection(self, path: str) -> None:
674+
def write_REDp_shiftcorrection(self, path: Path) -> None:
683675
"""Write .sc (shift correction) file for REDp in directory `path`"""
684676
path.mkdir(exist_ok=True)
685677

0 commit comments

Comments
 (0)