Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError: local variable 'status' referenced before assignment #44

Closed
rds-itga opened this issue Oct 16, 2020 · 9 comments
Closed

Comments

@rds-itga
Copy link

rds-itga commented Oct 16, 2020

Hi everybody,

I've just installed instamatic via pip install instamatic
I tried to run this short code:

from instamatic import TEMController
ctrl = TEMController.initialize()

but I got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instamatic\TEMController\TEMController.py", line 66, in initialize
    ctrl = _ctrl = TEMController(tem=tem, cam=cam)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instamatic\TEMController\TEMController.py", line 121, in __init__
    print(self)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instamatic\TEMController\TEMController.py", line 125, in __repr__
    return (f'Mode: {self.tem.getFunctionMode()}\n'
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instamatic\TEMController\microscope_client.py", line 92, in wrapper
    return self._eval_dct(dct)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\instamatic\TEMController\microscope_client.py", line 106, in _eval_dct
    if status == 200:
UnboundLocalError: local variable 'status' referenced before assignment

Could you help me to solve it, please?
Thank you
Regards

@stefsmeets
Copy link
Member

Hi @rds-itga , yup, this seems like a bug. Thanks for reporting. Should not be hard to fix, I will try to have a look at it today or tomorrow.

@stefsmeets
Copy link
Member

Hi @rds-itga , this is a bit of an odd error, meaning that it should not happen. That means something else is not working correctly. Can you tell me a little bit more about your environment and did you do anything else before running the command? Did you change the config? Can you print the full output? Thanks!

@rds-itga
Copy link
Author

Hi,
here are all the steps I followed:


pip install instamatic
Collecting instamatic
  Downloading instamatic-1.6.0-py3-none-any.whl (3.1 MB)
     |████████████████████████████████| 3.1 MB 2.2 MB/s
Collecting pyserialem>=0.3.0
  Downloading pyserialem-0.3.1-py3-none-any.whl (36 kB)
Collecting pywinauto>=0.6.8
  Downloading pywinauto-0.6.8-py2.py3-none-any.whl (362 kB)
     |████████████████████████████████| 362 kB 3.3 MB/s
Requirement already satisfied: scipy>=1.3.2 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (1.5.0)
Collecting lmfit>=1.0.0
  Downloading lmfit-1.0.1.tar.gz (258 kB)
     |████████████████████████████████| 258 kB 3.3 MB/s
Requirement already satisfied: pandas>=1.0.0 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (1.0.5)
Collecting comtypes>=1.1.7
  Downloading comtypes-1.1.7.zip (180 kB)
     |████████████████████████████████| 180 kB 3.2 MB/s
Collecting mrcfile>=1.1.2
  Downloading mrcfile-1.1.2-py2.py3-none-any.whl (41 kB)
     |████████████████████████████████| 41 kB 92 kB/s
Requirement already satisfied: pyyaml>=5.3 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (5.3.1)
Collecting virtualbox>=2.0.0
  Downloading virtualbox-2.0.0-py2.py3-none-any.whl (250 kB)
     |████████████████████████████████| 250 kB 3.3 MB/s
Requirement already satisfied: matplotlib>=3.1.2 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (3.2.2)
Collecting h5py>=2.10.0
  Downloading h5py-2.10.0-cp37-cp37m-win_amd64.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 1.3 MB/s
Collecting scikit-image>=0.17.1
  Downloading scikit_image-0.17.2-cp37-cp37m-win_amd64.whl (11.5 MB)
     |████████████████████████████████| 11.5 MB 3.3 MB/s
Requirement already satisfied: pillow>=7.0.0 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (7.2.0)
Requirement already satisfied: ipython>=7.11.1 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (7.13.0)
Collecting tqdm>=4.41.1
  Downloading tqdm-4.50.2-py2.py3-none-any.whl (70 kB)
     |████████████████████████████████| 70 kB 1.4 MB/s
Requirement already satisfied: numpy>=1.17.3 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from instamatic) (1.18.1)
Collecting tifffile>=2019.7.26.2
  Downloading tifffile-2020.10.1-py3-none-any.whl (152 kB)
     |████████████████████████████████| 152 kB 6.8 MB/s
Requirement already satisfied: six in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from pywinauto>=0.6.8->instamatic) (1.14.0)
Requirement already satisfied: pywin32 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from pywinauto>=0.6.8->instamatic) (227)
Collecting asteval>=0.9.16
  Downloading asteval-0.9.19.tar.gz (207 kB)
     |████████████████████████████████| 207 kB ...
Collecting uncertainties>=3.0.1
  Downloading uncertainties-3.1.4-py2.py3-none-any.whl (246 kB)
     |████████████████████████████████| 246 kB 3.3 MB/s
Requirement already satisfied: pytz>=2017.2 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from pandas>=1.0.0->instamatic) (2019.3)
Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from pandas>=1.0.0->instamatic) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from matplotlib>=3.1.2->instamatic) (2.4.6)
Requirement already satisfied: cycler>=0.10 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from matplotlib>=3.1.2->instamatic) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from matplotlib>=3.1.2->instamatic) (1.2.0)
Collecting imageio>=2.3.0
  Downloading imageio-2.9.0-py3-none-any.whl (3.3 MB)
     |████████████████████████████████| 3.3 MB 6.8 MB/s
Collecting networkx>=2.0
  Downloading networkx-2.5-py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 6.4 MB/s
Collecting PyWavelets>=1.1.1
  Downloading PyWavelets-1.1.1-cp37-cp37m-win_amd64.whl (4.2 MB)
     |████████████████████████████████| 4.2 MB 3.3 MB/s
Requirement already satisfied: pickleshare in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (0.7.5)
Requirement already satisfied: traitlets>=4.2 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (4.3.3)
Requirement already satisfied: decorator in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (4.4.2)
Requirement already satisfied: jedi>=0.10 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (0.15.2)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (3.0.4)
Requirement already satisfied: pygments in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (2.6.1)
Requirement already satisfied: backcall in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (0.1.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (0.4.3)
Requirement already satisfied: setuptools>=18.5 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from ipython>=7.11.1->instamatic) (46.1.3.post20200330)
Requirement already satisfied: future in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from uncertainties>=3.0.1->lmfit>=1.0.0->instamatic) (0.18.2)
Requirement already satisfied: ipython-genutils in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from traitlets>=4.2->ipython>=7.11.1->instamatic) (0.2.0)
Requirement already satisfied: parso>=0.5.2 in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from jedi>=0.10->ipython>=7.11.1->instamatic) (0.5.2)
Requirement already satisfied: wcwidth in c:\users\rds\appdata\local\continuum\anaconda3\envs\openclassroom\lib\site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=7.11.1->instamatic) (0.1.9)
Building wheels for collected packages: lmfit, comtypes, asteval
  Building wheel for lmfit (setup.py) ... done
  Created wheel for lmfit: filename=lmfit-1.0.1-py3-none-any.whl size=82005 sha256=767f50cf5b8469f6ea55d7f9da1d6f959583740e31e6d4659d4e9be0716c380a
  Stored in directory: c:\users\rds\appdata\local\pip\cache\wheels\d8\b1\40\a92e2a410f0e836855bb3c077e2ee8e6c93adf1898d69aff8c
  Building wheel for comtypes (setup.py) ... done
  Created wheel for comtypes: filename=comtypes-1.1.7-py3-none-any.whl size=164608 sha256=d0fe8ff326ddd9d60787dfde33cb779bfde7fb4ba708f1ebb7f1b9dcc0c39d45
  Stored in directory: c:\users\rds\appdata\local\pip\cache\wheels\01\6c\bd\be8ac2d1cf71f2231e4d7d692c12235699259c8358ce624092
  Building wheel for asteval (setup.py) ... done
  Created wheel for asteval: filename=asteval-0.9.19-py3-none-any.whl size=17499 sha256=f0bd38635f17db8af8192eff6b9121e154e16732aaec52641d682e73076f4258
  Stored in directory: c:\users\rds\appdata\local\pip\cache\wheels\a6\a4\52\305cdda6cd84c39736fc2cd5ead2985242956dc040a256a74b
Successfully built lmfit comtypes asteval
Installing collected packages: tqdm, imageio, tifffile, networkx, PyWavelets, scikit-image, mrcfile, asteval, uncertainties, lmfit, pyserialem, comtypes, pywinauto, virtualbox, h5py, instamatic
Successfully installed PyWavelets-1.1.1 asteval-0.9.19 comtypes-1.1.7 h5py-2.10.0 imageio-2.9.0 instamatic-1.6.0 lmfit-1.0.1 mrcfile-1.1.2 networkx-2.5 pyserialem-0.3.1 pywinauto-0.6.8 scikit-image-0.17.2 tifffile-2020.10.1 tqdm-4.50.2 uncertainties-3.1.4 virtualbox-2.0.0

(OpenClassroom) C:\Users\rds>python
Python 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from instamatic import TEMController
No config directory found, creating new one in C:\Users\rds\AppData\Roaming\instamatic
Configuration directory has been initialized.
Directory: C:\Users\rds\AppData\Roaming\instamatic
Please review and restart the program.

(OpenClassroom) C:\Users\rds>python
Python 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from instamatic import TEMController
Config directory: C:\Users\rds\AppData\Roaming\instamatic\config
>>> ctrl = TEMController.initialize()
Microscope: simulate (server)
Connected to TEM server (localhost:8088)
Camera    : simulate (stream)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\rds\AppData\Local\Continuum\anaconda3\envs\OpenClassroom\lib\site-packages\instamatic\TEMController\TEMController.py", line 66, in initialize
    ctrl = _ctrl = TEMController(tem=tem, cam=cam)
  File "C:\Users\rds\AppData\Local\Continuum\anaconda3\envs\OpenClassroom\lib\site-packages\instamatic\TEMController\TEMController.py", line 121, in __init__
    print(self)
  File "C:\Users\rds\AppData\Local\Continuum\anaconda3\envs\OpenClassroom\lib\site-packages\instamatic\TEMController\TEMController.py", line 125, in __repr__
    return (f'Mode: {self.tem.getFunctionMode()}\n'
  File "C:\Users\rds\AppData\Local\Continuum\anaconda3\envs\OpenClassroom\lib\site-packages\instamatic\TEMController\microscope_client.py", line 92, in wrapper
    return self._eval_dct(dct)
  File "C:\Users\rds\AppData\Local\Continuum\anaconda3\envs\OpenClassroom\lib\site-packages\instamatic\TEMController\microscope_client.py", line 106, in _eval_dct
    if status == 200:
UnboundLocalError: local variable 'status' referenced before assignment
>>>

Regards

@rds-itga
Copy link
Author

defaults.yaml

gridmontage:
  gridspec:
    direction: 'downup'
    zigzag: true
    flip: true
  overlap: 0.1
montage:
  from_serialem_mrc:
    direction: 'downup'
    zigzag: true
    flip: true
    rot90: 3
    flipud: true
    fliplr: false
predicrystal:
  location: 'C:\predicrystal'
  classifier: holey
  filter_distance: 2.0

settings.yaml

microscope: simulate
camera: simulate
calibration: simulate

# Global toggle to force simulated camera/microscope interface
simulate: False

data_directory: C:/instamatic
#flatfield: C:/instamatic/flatfield.tiff
flatfield:

# Run the TEM connection in a different process (recommended)
use_tem_server: True
tem_server_host: 'localhost'
tem_server_port: 8088
tem_require_admin: False
tem_communication_protocol: 'pickle'  # pickle, json, msgpack, yaml

# Run the Camera connection in a different process
use_cam_server: False
cam_server_host: 'localhost'
cam_server_port: 8087
cam_use_shared_memory: true

# Submit collected data to an indexing server (CRED only)
use_indexing_server_exe: False
indexing_server_exe: 'instamatic.dialsserver.exe'
indexing_server_host: 'localhost'
indexing_server_port: 8089
dials_script: 'E:/cctbx/dials_script.bat'

# JEOL only, automatically set the rotation speed via Goniotool (instamatic.goniotool)
use_goniotool: False
goniotool_server_host: 'localhost'
goniotool_server_port: 8090

# For InsteaDMatic to control the rotation speed on a FEI/TFS system
fei_server_host: '192.168.12.1'
fei_server_port: 8091

# Automatically submit the data to an indexing server running in a VM (VirtualBox)
use_VM_server_exe: False
VM_server_exe: 'instamatic.VMserver.exe'
VM_server_host: 'localhost'
VM_server_port: 8092
VM_ID: "Ubuntu 14.04.3"
VM_USERNAME: "lab6"
VM_PWD: "testtest"
VM_STARTUP_DELAY: 50
VM_DESKTOP_DELAY: 20
VM_SHARED_FOLDER: F:\SharedWithVM

# Testing variables
cred_relax_beam_before_experiment: false
cred_track_stage_positions: false

# Here the panels for the GUI can be turned on/off/reordered
modules:
  - 'cred'
  - 'cred_tvips'
  - 'cred_fei'
  - 'sed'
  - 'autocred'
  - 'red'
  - 'machine_learning'
  - 'ctrl'
  - 'debug'
  - 'about'
  - 'console'
  - 'io'

@stefsmeets
Copy link
Member

Good stuff. I can fix the error quite easily, but it probably will not address the underlying issue.

You can try:

use_tem_server: False

This will avoid the issue altogether by running the interface in the same process (not ideal, but OK for scripting work).

Alternatively, can you try to first run:

instamatic.temserver

And then in a different tab, do the thing from before:

from instamatic import TEMController
ctrl = TEMController.initialize()

@rds-itga
Copy link
Author

Hi, with
use_tem_server: False

it seems to work, here is what the the terminal returned to me:

Python 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from instamatic import TEMController
Config directory: C:\Users\rds\AppData\Roaming\instamatic\config
>>>
>>> ctrl = TEMController.initialize()
Microscope: simulate
Camera    : simulate (stream)

Mode: mag1
High tension: 200 kV
Current density: 101870.12 pA/cm2
GunShift(x=3770, y=58589)
GunTilt(x=63225, y=43005)
BeamShift(x=46346, y=40527)
BeamTilt(x=62627, y=64505)
ImageShift1(x=45491, y=29424)
ImageShift2(x=15383, y=27640)
DiffShift(x=44088, y=41496)
Stage(x=32280.0, y=-15346.0, z=148.0, a=6.0, b=-36.0)
Magnification(value=25000, index=10)
DiffFocus(value=n/a)
Brightness(value=12545)
SpotSize(1)
Saved alignments: ('neutral.yaml',)```

Thank you very much for your help and your reactivity. :)

Regards

@stefsmeets
Copy link
Member

No worries!

@rds-itga
Copy link
Author

Hi again,

I just changed default tem_server_port 8088 into 8087 (as i don't use a cam_server) and now it is working in tem_server mode.
I don't know why it does not work with port 8088, maybe another application is using it on my machine but how to know it
Finally here is the config file i used and which is working

microscope: simulate
camera: simulate
calibration: simulate

# Global toggle to force simulated camera/microscope interface
simulate: false

data_directory: C:\instamatic
#flatfield: C:/instamatic/flatfield.tiff
flatfield:

# Run the TEM connection in a different process (recommended)
use_tem_server: true
tem_server_host: 'localhost'
tem_server_port: 8087
tem_require_admin: false
tem_communication_protocol: 'pickle'  # pickle, json, msgpack, yaml

# Run the Camera connection in a different process
use_cam_server: false
cam_server_host: 'localhost'
cam_server_port: 8087
cam_use_shared_memory: true

# Submit collected data to an indexing server (CRED only)
use_indexing_server_exe: false
indexing_server_exe: 'instamatic.dialsserver.exe'
indexing_server_host: 'localhost'
indexing_server_port: 8089
dials_script: 'E:/cctbx/dials_script.bat'

# JEOL only, automatically set the rotation speed via Goniotool (instamatic.goniotool)
use_goniotool: false
goniotool_server_host: 'localhost'
goniotool_server_port: 8090

# For InsteaDMatic to control the rotation speed on a FEI/TFS system
fei_server_host: '192.168.12.1'
fei_server_port: 8091

# Automatically submit the data to an indexing server running in a VM (VirtualBox)
use_VM_server_exe: false
VM_server_exe: 'instamatic.VMserver.exe'
VM_server_host: 'localhost'
VM_server_port: 8092
VM_ID: "Ubuntu 14.04.3"
VM_USERNAME: "lab6"
VM_PWD: "testtest"
VM_STARTUP_DELAY: 50
VM_DESKTOP_DELAY: 20
VM_SHARED_FOLDER: F:\SharedWithVM

# Testing variables
cred_relax_beam_before_experiment: false
cred_track_stage_positions: false

# Here the panels for the GUI can be turned on/off/reordered
modules:
  - 'cred'
  - 'cred_tvips'
  - 'cred_fei'
  - 'sed'
  - 'autocred'
  - 'red'
  - 'machine_learning'
  - 'ctrl'
  - 'debug'
  - 'about'
  - 'console'
  - 'io'

Regards

@stefsmeets
Copy link
Member

Hi @rds-itga , nice debugging there! Thanks for letting me know. The choice of port is somewhat abritrary, but it is indeed possible that it was already in use (or maybe some hanging process). instamatic will try to connect to a port if it sees that it is open, but it does not check what it connects too 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants