scholi / pySPM

Python library to handle Scanning Probe Microscopy Images. Can read nanoscan .xml data, Bruker AFM images, Nanonis SXM files as well as iontof images(ITA, ITM and ITS).
Apache License 2.0
58 stars 33 forks source link

Bruker get_channel floating point parsing error #38

Closed SylviaWhittle closed 6 months ago

SylviaWhittle commented 6 months ago

Describe the bug Hi there 😄

We have been using pySPM for a while now to open Bruker .spm files, and it has been going seamlessly. Today we have noticed that on files that previously definitely loaded properly, it now throws this error. The files are non-square in their aspect ratio.

The issue appears to be the handling of floating point values in the get_channel() method of the Bruker class.

What is weird is that this error happens on both version 0.2.23 and 0.5.1 and manually setting the versions to previous versions does not fix it.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[9], [line 7](vscode-notebook-cell:?execution_count=9&line=7)
      [3](vscode-notebook-cell:?execution_count=9&line=3) data = pySPM.Bruker(file)
      [5](vscode-notebook-cell:?execution_count=9&line=5) data.list_channels()
----> [7](vscode-notebook-cell:?execution_count=9&line=7) height = data.get_channel('Height')

File [/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173), in Bruker.get_channel(self, channel, backward, corr, debug, encoding, lazy, mfm)
    [171](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:171)     print("xres/yres", xres, yres)
    [172](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:172) scan_size = self.layers[i][b'Scan Size'][0].split()
--> [173](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173) aspect_ratio = [int(x) for x in self.layers[i][b'Aspect Ratio'][0].split(b":")]
    [174](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:174) if debug:
    [175](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:175)     print("aspect ratio", aspect_ratio)

File [/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173), in <listcomp>(.0)
    [171](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:171)     print("xres/yres", xres, yres)
    [172](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:172) scan_size = self.layers[i][b'Scan Size'][0].split()
--> [173](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:173) aspect_ratio = [int(x) for x in self.layers[i][b'Aspect Ratio'][0].split(b":")]
    [174](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:174) if debug:
    [175](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/Caskroom/miniforge/base/envs/new/lib/python3.10/site-packages/pySPM/Bruker.py:175)     print("aspect ratio", aspect_ratio)

ValueError: invalid literal for int() with base 10: b'0.51216'

To Reproduce file used: not_working_file.spm.zip

I had to compress it since GitHub won't allow .spm uploads. I hope this doesn't corrupt it.

from pathlib import Path

import pySPM

print("pySPM version: ", pySPM.__version__)

file = Path('/Users/sylvi/Downloads/not_working_file.spm')

data = pySPM.Bruker(file)

data.list_channels()

height = data.get_channel('Height')

Expected behavior Loading the height channel data as usual.

Screenshots image

Information:

Please run the following and attach the result to your issue

import sys
import pySPM
import numpy as np
import scipy
import matplotlib as mpl
print("Python",sys.version)
print("pySPM",pySPM.__version__)
print("numpy",np.__version__)
print("scipy",scipy.__version__)
print("matplotlib", mpl.__version__)
Python 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:26:08) [Clang 14.0.6 ]
pySPM 0.5.1
numpy 1.26.3
scipy 1.10.1
matplotlib 3.7.1
scholi commented 6 months ago

It seems that the aspect ratio is a float and not a pair of int. Which picture has the correct aspect ratio? A or B? A) image B) image

SylviaWhittle commented 6 months ago

Thank you for your attention

B is correct.

I have been told that we have successfully used pySPM on these images before which is strange... It seems that it shouldn't have ever worked if the code assumed a pair of integers.

scholi commented 6 months ago

What is the real size of your image? 300μm horizontally? but for the vertical axis Gwyddion tells me about 210μm while the aspect ratio of 0.51216 of 300μm is 153μm which is strange...

scholi commented 6 months ago

Could you try this branch: https://github.com/scholi/pySPM/tree/Bruker_non_square Please test it with Bruker file having various aspect ratio. If you see that this work then we can merge it to master and create a new release. I have myself no more access to a Bruken instrument and thus cannot perform the tests myself....

SylviaWhittle commented 6 months ago

The real size of the image as reported by Nanoscope, are 300nm x 210nm (0.7 aspect ratio) image

I also held a tape measure up to my screen and confirmed an aspect ratio of 0.7.

We have no idea where the 0.51216 aspect ratio could come from. It is very strange. It is also strange that these files loaded perfectly fine before about December 22nd 2023. Perhaps there is some user error in there somewhere...

Your branch works on both the files that were not previously able to be loaded. I cannot share the second one as it is a collaborator's data and we don't have permission to share it.

Here is a screenshot of it working: image

Thank you for your time, it is very much appreciated by our team 😄

scholi commented 6 months ago

Ok good to hear that it works :). So I will merge this branch to master.