Skip to content

Commit

Permalink
Add all .asd frames as images to img_dict in io.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SylviaWhittle committed Sep 24, 2023
1 parent e76cbc5 commit 038b1b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions asd_file_format.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@
" y = read_int32(f)\n",
" coords_blue.append((x, y))\n",
"\n",
" return header_dict\n",
"\n",
"\n",
"class VoltageLevelConverter:\n",
" def __init__(self, analogue_digital_range, max_voltage, scaling_factor, resolution):\n",
Expand Down Expand Up @@ -556,9 +558,7 @@
"metadata": {},
"outputs": [],
"source": [
"def load_asd():\n",
" channel = \"TP\"\n",
"\n",
"def load_asd(channel: str):\n",
" with open(FILE_PATH, \"rb\") as f:\n",
" file_version = read_file_version(f)\n",
"\n",
Expand Down
41 changes: 24 additions & 17 deletions topostats/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,45 +919,52 @@ def get_data(self) -> None:
else:
raise
else:
self._check_image_size_and_add_to_dict()
if suffix == ".asd":
for index, frame in enumerate(self.image):
self._check_image_size_and_add_to_dict(image=frame, filename=f"{self.filename}_{index}")
else:
self._check_image_size_and_add_to_dict(image=self.image, filename=self.filename)
else:
raise ValueError(
f"File type {suffix} not yet supported. Please make an issue at \
https://github.com/AFM-SPM/TopoStats/issues, or email [email protected] to request support for \
this file type."
)

def _check_image_size_and_add_to_dict(self) -> None:
def _check_image_size_and_add_to_dict(self, image: np.ndarray, filename: str) -> None:
"""Check the image is above a minimum size in both dimensions.
Images that do not meet the minimum size are not included for processing.
Parameters
----------
image: np.ndarray
An array of the extracted AFM image.
filename: str
The name of the file
"""
if self.image.shape[0] < self.MINIMUM_IMAGE_SIZE or self.image.shape[1] < self.MINIMUM_IMAGE_SIZE:
LOGGER.warning(f"[{self.filename}] Skipping, image too small: {self.image.shape}")
if image.shape[0] < self.MINIMUM_IMAGE_SIZE or image.shape[1] < self.MINIMUM_IMAGE_SIZE:
LOGGER.warning(f"[{filename}] Skipping, image too small: {image.shape}")
else:
self.add_to_dict()
LOGGER.info(f"[{self.filename}] Image added to processing.")
self.add_to_dict(image=image, filename=filename)
LOGGER.info(f"[{filename}] Image added to processing.")

def add_to_dict(self) -> None:
def add_to_dict(self, image: np.ndarray, filename: str) -> None:
"""Adds the image, image path and pixel to nanometre scaling value to the img_dic dictionary under
the key filename.
Parameters
----------
filename: str
The filename, idealy without an extension.
image: np.ndarray
An array of the extracted AFM image.
img_path: str
The path to the AFM file (with a frame number if applicable)
px_2_nm: float
The length of a pixel in nm.
filename: str
The name of the file
"""
self.img_dict[self.filename] = {
"filename": self.filename,
"img_path": self.img_path.with_name(self.filename),
self.img_dict[filename] = {
"filename": filename,
"img_path": self.img_path.with_name(filename),
"pixel_to_nm_scaling": self.pixel_to_nm_scaling,
"image_original": self.image,
"image_original": image,
"image_flattened": None,
"grain_masks": self.grain_masks,
}
Expand Down

0 comments on commit 038b1b3

Please sign in to comment.