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
62 stars 33 forks source link

"li" variable not defined in ITM.py #18

Closed jablevin closed 3 years ago

jablevin commented 3 years ago

When running >>> res = AI.get_raw_spectrum(ROI=[ROI1, ROI2], prog=True, sf=sf, k0=k0) I get the error that local variable "li" is referenced before assignment.

The code from the file is below. The error arises near the end "for k in li"

elif type(ROI) in [list, tuple]:
            multi_roi = True
           Spectrum = np.zeros((number_channels, len(ROI)), dtype=np.float32)
            for s in T:
                raw = self.get_raw_raw_data(s)
                rawv = struct.unpack('<{}I'.format(len(raw)//4), raw)
                i = 0
                while i < len(rawv):
                    b = rawv[i]
                    if b & 0xc000000000:
                        x = b & 0x0fffffff
                        y = rawv[i+1] & 0x0fffffff
                        dt = dts[x]
                        fp = dt%1
                        ip = int(dt)
                        li = []
                        for k, R in enumerate(ROI):
                            if R[y,x]:
                                li.append(k)
                        i += 3
                    else:
                        for k in li:
                                Spectrum[b-ip, k] += (1-fp)
                                Spectrum[b-ip-1, k] += fp
                        i += 1
scholi commented 3 years ago

There is definitely a python bug, but I'm not sure to remember correctly how the rawSpectrum is encoded to correct it and beeing sure that the data you get make sense.... I guess you should predefine li before the while. So add

li=[]

at line 1050

Can you test it with your data and tell me if the results make sens?

scholi commented 3 years ago

Can I get some feedback? Does this solution works?

andrealucero commented 3 years ago

Hello. I have a similar error when I try to run the following cell from the "Introduction to pySPM a python library for ToF-SIMS data analysis": AI = pySPM.ITM(get_data("Cysteine_B3p_p_01.itm")) sf, k0 = A.auto_mass_cal() try:

Reconstruction is a slow porcess, so the data are stored in order to speed up the running speed of this notebook

res = pySPM.utils.load("Intro_pySPM_tofsims", 'Cysteine_ROI')

except: res = AI.get_raw_spectrum(ROI=[ROI1, ROI2], prog=True, sf=sf, k0=k0) pySPM.utils.save("Intro_pySPM_tofsims", Cysteine_ROI=res)

The error is:


OSError Traceback (most recent call last)

in 4 # Reconstruction is a slow porcess, so the data are stored in order to speed up the running speed of this notebook ----> 5 res = pySPM.utils.load("Intro_pySPM_tofsims", 'Cysteine_ROI') 6 except: ~\Anaconda3\lib\site-packages\pySPM\utils\save.py in load(filename, *keys) 92 """ ---> 93 filename = findPKZ(filename) 94 if len(keys) == 0: ~\Anaconda3\lib\site-packages\pySPM\utils\save.py in findPKZ(filename) 36 if not os.path.exists(filename): ---> 37 raise IOError("File \"{}\" not found".format(filename)) 38 return filename OSError: File "Intro_pySPM_tofsims.pkz" not found During handling of the above exception, another exception occurred: UnboundLocalError Traceback (most recent call last) in 5 res = pySPM.utils.load("Intro_pySPM_tofsims", 'Cysteine_ROI') 6 except: ----> 7 res = AI.get_raw_spectrum(ROI=[ROI1, ROI2], prog=True, sf=sf, k0=k0) 8 pySPM.utils.save("Intro_pySPM_tofsims", Cysteine_ROI=res) ~\Anaconda3\lib\site-packages\pySPM\ITM.py in get_raw_spectrum(self, scans, ROI, FOVcorr, deadTimeCorr, **kargs) 1063 li.append(k) 1064 i += 3 -> 1065 else: 1066 for k in li: 1067 Spectrum[b-ip, k] += (1-fp) UnboundLocalError: local variable 'li' referenced before assignment Looks like it's not only the assignment of "li". I'm not an expert, so I would appreciate your help. Thank you in advance. Update 23/08/21 Tried again and the code in the cell worked
scholi commented 3 years ago

and? did you try the proposed solution? Does it work?

andrealucero commented 3 years ago

Yes. I did what you suggested before and it worked. Thank you!

scholi commented 3 years ago

OK. I'll add it for next release then. Thank you for the feedback.