Skip to content

Commit 07da6d8

Browse files
committed
1) fix datatype to uint16 for now; 2) fix bema tilt set from tuple or list; 3) fix thumbnail dims and use 8-bit; 4)tif resolution is a rational number; 5) fix com error parsing
1 parent 6af41f4 commit 07da6d8

5 files changed

Lines changed: 23 additions & 16 deletions

File tree

pytemscript/clients/com_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ def handle_com_error(com_error):
233233
""" Try catching COM error. """
234234
try:
235235
error_code = TEMScriptingError(int(com_error.args[0])).name
236-
error_msg = com_error.args[2][0].split("]")[-1]
236+
error_msg = com_error.args[2][0]
237+
if error_msg is not None:
238+
error_msg = error_msg.split("]")[-1]
237239
except (ValueError, IndexError, TypeError):
238240
error_code = TEMScriptingError.E_NOT_OK.name
239241
error_msg = str(com_error)

pytemscript/modules/autoloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def load_cartridge(self, slot: int) -> None:
5959

6060
def unload_cartridge(self) -> None:
6161
""" Unloads the cartridge currently in the microscope and puts it back into its
62-
slot in the cassette.
62+
slot in the cassette. Does nothing if no cartridge is on stage.
6363
"""
6464
if self.is_available:
6565
body = RequestBody(attr=self.__id + ".UnloadCartridge()")

pytemscript/modules/extras.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
import numpy as np
88
from collections import OrderedDict
9+
from fractions import Fraction
910
import PIL.Image as PilImage
1011
import PIL.TiffImagePlugin as PilTiff
1112

@@ -182,8 +183,8 @@ def __create_tiff_tags(self):
182183
dpcm_width = 1 / (float(pixel_width) * 100)
183184
dpcm_height = 1 / (float(pixel_height) * 100)
184185

185-
tiff_tags[PilTiff.X_RESOLUTION] = int(dpcm_width)
186-
tiff_tags[PilTiff.Y_RESOLUTION] = int(dpcm_height)
186+
tiff_tags[PilTiff.X_RESOLUTION] = Fraction(int(dpcm_width), 1)
187+
tiff_tags[PilTiff.Y_RESOLUTION] = Fraction(int(dpcm_height), 1)
187188

188189
# Bit Depth & Color Interpretation
189190
bit_depth = metadata.get("bit_depth", 16)
@@ -201,7 +202,7 @@ def save(self,
201202
202203
:param fn: Filepath
203204
:type fn: Path or str
204-
:param bool thumbnail: Create a 512px-wide thumbnail, height is adjusted to keep the aspect ratio. Only for non-MRC formats
205+
:param bool thumbnail: Create a 512px-wide 8-bit thumbnail, height is adjusted to keep the aspect ratio. Only for non-MRC formats
205206
:param bool overwrite: Overwrite existing file
206207
"""
207208
fn = os.path.abspath(fn)
@@ -219,11 +220,12 @@ def save(self,
219220
raise FileExistsError("File %s already exists, use overwrite flag" % fn)
220221

221222
logging.getLogger("PIL").setLevel(logging.INFO)
222-
pil_image = PilImage.fromarray(self.data.copy(), mode='I;16')
223+
data = self.data.copy()
223224

224225
if thumbnail:
225-
# create a thumbnail
226-
width, height = self.data.shape
226+
# create an 8-bit thumbnail
227+
pil_image = PilImage.fromarray(data, mode='L')
228+
height, width = data.shape
227229
if width < height:
228230
width = max(round(width * 512 / height), 1)
229231
thumbnail_size = (width, 512)
@@ -232,6 +234,8 @@ def save(self,
232234
thumbnail_size = (512, height)
233235

234236
pil_image.thumbnail(size=thumbnail_size, resample=PilImage.Resampling.LANCZOS)
237+
else:
238+
pil_image = PilImage.fromarray(data, mode='I;16')
235239

236240
# create tiff tags
237241
if ext in [".tif", ".tiff"] and not thumbnail:

pytemscript/modules/illumination.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ def beam_tilt(self) -> Union[Vector, float]:
279279
return Vector(0.0, 0.0) # Microscope might return nonsense if DFMode is OFF
280280

281281
@beam_tilt.setter
282-
def beam_tilt(self, tilt: Union[Vector, float]) -> None:
282+
def beam_tilt(self, tilt: Union[Vector, float, List[float], Tuple[float, float]]) -> None:
283283
body = RequestBody(attr=self.__id + ".DFMode", validator=int)
284284
mode = self.__client.call(method="get", body=body)
285285

286286
if isinstance(tilt, float):
287287
tilt = Vector(tilt, tilt)
288288

289-
tilt *= 1e-3 # mrad to rad
289+
tilt = Vector.convert_to(tilt) * 1e-3 # mrad to rad
290290

291291
if tilt == (0.0, 0.0):
292292
body = RequestBody(attr=self.__id + ".Tilt", value=tilt)

pytemscript/utils/misc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ def send_data(sock, data: bytes, datatype="msg") -> None:
6060
:param str datatype: data type
6161
6262
The packet includes the following:
63-
- type: 2 bytes
63+
- header: 2 bytes
6464
- length of data: 4 bytes
65-
- checksum: 20 bytes - only if type is "data"
65+
- checksum: 20 bytes - only if header == HEADER_DATA
66+
- actual data
6667
"""
6768
packet = bytearray()
6869
packet.extend(HEADER_MSG if datatype == "msg" else HEADER_DATA)
@@ -163,16 +164,16 @@ def convert_image(obj,
163164
metadata = {
164165
"width": width or int(obj.Width),
165166
"height": height or int(obj.Height),
166-
"bit_depth": int(bit_depth or (obj.BitDepth if advanced else obj.Depth)),
167-
"pixel_type": ImagePixelType(obj.PixelType).name if advanced else ImagePixelType.SIGNED_INT.name,
167+
"bit_depth": 16, # int(bit_depth or (obj.BitDepth if advanced else obj.Depth)),
168+
"pixel_type": ImagePixelType.UNSIGNED_INT.name # ImagePixelType(obj.PixelType).name if advanced
168169
}
169170
if pixel_size is not None:
170171
metadata["PixelSize.Width"] = pixel_size
171172
metadata["PixelSize.Height"] = pixel_size
172173
if advanced:
173174
metadata.update({item.Key: item.ValueAsString for item in obj.Metadata})
174-
if "BitsPerPixel" in metadata:
175-
metadata["bit_depth"] = int(metadata["BitsPerPixel"])
175+
#if "BitsPerPixel" in metadata:
176+
# metadata["bit_depth"] = int(metadata["BitsPerPixel"])
176177

177178
return Image(data, name, metadata)
178179

0 commit comments

Comments
 (0)