Python Interface Examples¶
There are a couple of examples.
Hello Imspector Example¶
Connects to Imspector, displays the parameters and doubles the exposure time of the sample camera if a camera is existing as device.
"""
Connects to Imspector, displays the parameters and doubles the exposure time of the sample camera
if a camera is existing as device.
Requirements: Latest Python 2.X or 3.X, specpy (https://pypi.python.org/pypi/specpy/1.0.2)
which itself requires NumPy >= 1.8.1, Imspector with "Run Server" checked and an open measurement
See also: http://imspectordocs.readthedocs.io/en/latest/specpy/examples.html
"""
# Python 2/3 compatibility
from __future__ import absolute_import, division, print_function
# import specpy
import specpy as sp
# connect to local Imspector
im = sp.get_application()
# print Imspector host and version
print('Connected to Imspector {} on {}'.format(im.version(), im.host()))
# get active measurement and print nicely
msr = im.active_measurement()
from pprint import pprint
print('Parameters of measurement {}'.format(msr.name()))
pprint(msr.parameters())
# read and write a parameter
if 'SimCam' in msr.parameters():
time = msr.parameters('SimCam/ExposureTime')
msr.set_parameters('SimCam/ExposureTime', 2 * time)
# now have a look at Imspector, the parameter should have changed in the measurement properties of this measurement
Data Analysis Example¶
Connects to Imspector, opens a recorded measurement, computes statistical measures on this data and prints them in a file.
data_analysis_example.py - data_analysis_example.msr
"""
Connects to Imspector, opens a recorded measurement, computes statistical measures on this data
and prints them in a file.
Requirements: Latest Python 2.X or 3.X, specpy (https://pypi.python.org/pypi/specpy/1.0.2)
which itself requires NumPy >= 1.8.1, Imspector with "Run Server" checked and the file
"data_analysis_example.msr".
See also: http://imspectordocs.readthedocs.io/en/latest/specpy/examples.html
"""
# Python 2/3 compatibility
from __future__ import absolute_import, division, print_function
# import numpy
import numpy as np
# import specpy
import specpy as sp
# connect to local Imspector and open an example file
im = sp.get_application()
measurement = im.open("data_analysis_example.msr")
# set threshold
threshold = 410
# open output file
file = open('data_analysis_example_output.txt', 'w')
# for each stack in the measurement
for name in measurement.stack_names():
# get the stack (as our stub object)
stack = measurement.stack(name)
# get the data array from the stack
data = stack.data()
# compute mean and std
mean = data.mean()
standard_deviation = data.std()
# print mean and std to console and to file
print('stack {} has mean {} and std {}'.format(name, mean, standard_deviation))
print('stack {} has mean {} and std {}'.format(name, mean, standard_deviation), file=file)
# apply mask (all values smaller threshold)
masked_data = np.ma.masked_less(data, threshold)
# compute mean and std on masked image
mean = masked_data.mean()
standard_deviation = masked_data.std()
# print mean and std on masked image to console and to file
print('masked stack {} with threshold {} has mean {} and std {}'.format(name, threshold, mean, standard_deviation))
print('masked stack {} with threshold {} has mean {} and std {}'.format(name, threshold, mean, standard_deviation), file=file)
# change pixels (you can see this in Imspector) below threshold to another value
np.putmask(data, data < threshold, 4095)
file.close()
Measurement Example¶
Example of how to traverse the measurements, configurations, stacks hierarchy of Imspector with Python. Also runs a measurement.
"""
Example of how to traverse the measurements, configurations, stacks hierarchy of Imspector with Python.
Also runs a measurement.
Requirements: Latest Python 2.X or 3.X, specpy (https://pypi.python.org/pypi/specpy/1.0.2)
which itself requires NumPy >= 1.8.1, Imspector with "Run Server" checked and an open measurement.
March, 2015, Jan Keller (jan.keller@mpibpc.mpg.de)
See also: http://imspectordocs.readthedocs.io/en/latest/specpy/examples.html
"""
# Python 2/3 compatibility
from __future__ import absolute_import, division, print_function
import pprint
pp = pprint.PrettyPrinter(indent=2)
# import NumPy
import numpy as np
# import specpy
import specpy as sp
# get imspector object and print version
im = sp.get_application()
print('Imspector {} on {}'.format(im.version(), im.host()))
# print existing device drivers
print('devices')
pp.pprint(im.device_drivers())
# print current imspector parameters
print('parameters')
pp.pprint(im.parameters())
# list all open measurements
print('names of open measurements')
pp.pprint(im.measurement_names())
# get currently active measurement
msr = im.active_measurement()
print('name of active measurement: {}'.format(msr.name()))
print('configurations in active measurement')
pp.pprint(msr.configuration_names())
print('number of stacks: {}'.format(msr.number_of_stacks()))
pp.pprint(msr.stack_names())
# get another measurement (the first one, should be at least one open)
measurement_name = im.measurement_names()[0]
msr = im.measurement(measurement_name)
print('name of chosen measurement: {}'.format(msr.name()))
print('configurations in chosen measurement')
pp.pprint(msr.configuration_names())
print('active configuration: {}'.format(msr.active_configuration().name()))
print('number of stacks: {}'.format(msr.number_of_stacks()))
pp.pprint(msr.stack_names())
# set active configuration (the first one, should contain at least one)
configuration_name = msr.configuration_names()[0]
configuration = msr.configuration(configuration_name)
msr.activate(configuration)
print('active configuration: {}'.format(msr.active_configuration().name()))
# create new stack (will be added to active measurement)
s = msr.create_stack(np.int32, [100, 100, 1, 1])
print('number of stacks: {}'.format(msr.number_of_stacks()))
pp.pprint(msr.stack_names())
# runs the measurement
print('active configuration {}'.format(msr.active_configuration().name()))
im.run(msr)
print('number of stacks: {}'.format(msr.number_of_stacks()))
pp.pprint(msr.stack_names())
# set a configuration and runs it
configuration = msr.configuration(configuration_name)
msr.activate(configuration)
print('active configuration {}'.format(msr.active_configuration().name()))
im.run(msr)
print('number of stacks: {}'.format(msr.number_of_stacks()))
pp.pprint(msr.stack_names())