Git Product home page Git Product logo

comet's Introduction

COMET

Control and Measurement Toolkit

Providing generic instrument drivers, instrument emulators for testing and and utilities for instrumentation applications. Inspired by QCoDeS, Lantz, Slave, FluidLab.

Install

Install from GitHub using pip

pip install git+https://github.com/hephy-dd/[email protected]

Drivers

Generic instrument drivers use a PyVISA compatible resource to communicate with.

import pyvisa

from comet.driver.keithley import K2470

rm = pyvisa.ResourceManager("@py")

with rm.open_resource("TCPIP::0.0.0.0::11001::SOCKET") as res:
    smu = K2470(res)
    print(smu.identify())

    smu.reset()
    smu.function = smu.FUNCTION_VOLTAGE
    smu.current_compliance = 1e-6
    smu.output = smu.OUTPUT_ON
    smu.voltage_level = 5.0

    reading = smu.measure_current()
    print(reading)

    smu.voltage_level = 0.0
    smu.output = smu.OUTPUT_OFF

Switching between generic drivers.

from comet.driver.keithley import K2410
from comet.driver.keithley import K2470
from comet.driver.keithley import K2657A

smu_drivers = {
    "Keithely2410": K2410,
    "Keithely2470": K2470,
    "Keitley2657A": K2657A,
}

driver_name = "Keithely2470"

rm = pyvisa.ResourceManager("@py")

with rm.open_resource("TCPIP::0.0.0.0::11001::SOCKET") as res:
    smu = smu_drivers.get(driver_name)(res)

See comet/driver for available instrument drivers.

Helpers

Estimate

Estimate remaining time for loop operations using class Estimate.

Call method advance to proceed to the next iteration and update average and remaining time calculation.

from comet.estimate import Estimate

e = Estimate(42)  # start stopwatch
for i in range(42 + 1):
    ...
    e.advance()  # stop time since last step
    print("passed:", e.passed)
    print("remaining time:", e.remaining)
    print("elapsed time:", e.elapsed)
    print("average time:", e.average)

Filters

Test if standard deviation / mean < threshold using function std_mean_filter.

from comet.filters import std_mean_filter

if std_mean_filter(readings, threshold=0.005):
    ...

Functions

Voltage ramps using LinearRange generator class.

from comet.functions import LinearRange

for voltage in LinearRange(-10, +10, 0.25):
    ...

Parameter

Bind typed and bounded parameters to classes inheriting from class ParameterBase.

from comet.parameter import ParameterBase, Parameter

class Measurement(ParameterBase):

    voltage_level = Parameter(unit="V", minimum=-1000, maximum=1000)
    current_compliance = Parameter(default="25 uA", unit="A", minimum=0, maximum="10 mA")
    terminal = Parameter(default="front", choice=["front", "rear"])
    write_output = Parameter(default=True, type=bool)

measurement = Measurement({"voltage_level": "100 V"})  # supply required parameters

# Get dictionary of all parameter values.
print(measurement.parameters)
# {'voltage_level': 100.0, 'current_compliance': 2.5e-05, 'terminal': 'front', 'write_output': True}

# Update parameter values
measurement.update_parameters({"current_compliance": "50 uA", "terminal": "rear"})

# Access individual parameter
print(measurement.voltage_level)
# 100.0
print(measurement.current_compliance)
# 5e-05
print(measurement.terminal)
# 'rear'

measurement.write_output = False
# AttributeError: can't set parameter: 'write_output'

Utils

Use pint unit registry to convert between units.

from comet.utils import ureg, to_unit

quantity = ureg("25 nA").to("A")

print(to_unit(quantity, "mA"))
# 2.5e-05

print(to_unit("1200 V", "kV"))
# 1.2

print(to_unit(2.5, "pA"))
# 2.5

Emulators

To emulate one or more instruments using TCP sockets create a emulators.yaml configuration file in your project directory specifying emulator module and port.

version: '1.0'
emulators:
  smu:
    module: keithley.k2470
    port: 11001
  lcr:
    module: keysight.e4980a
    port: 11002

To spin up the emulator sockets execute the comet.emulator package.

python -m comet.emulator

Use command line argument -f to use a custom configuration file.

python -m comet.emulator -f custom_emulators.yaml

See comet/emulator for available instrument emulators.

License

COMET is licensed under the GNU General Public License Version 3.

comet's People

Contributors

arnobaer avatar hiaslknopf avatar prakhub avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

arnobaer prakhub

comet's Issues

Add table UI widget

Add a simple to use table widget.

table = comet.Table(header=["Key", "Value"])
table.append(["spam", 1000])
table.append(["ham", 2000])
table.append(["eggs", 3000])
for row in table:
    row[0].checked = True # set check state
    row[0].color = "blue"

table

Improved process callbacks

An optimized approach for custom process/thread callback registration and calls.

class Process(comet.Process):
    def run(self):
        while self.running:
            self.push("temp", 22.5)
            self.push("humid", 50.0)
            self.push("status", 42)

process = Process(
    fail=ctrl.handle_error, # built in callback
    temp=ctrl.handle_temp,
    humid=ctrl.handle_humid
)
process.callbacks["status"] = ctrl.handle_status
...

Optional move built in callbacks begin, finish and fail also to property callbacks to avoid accidental assignment in process run().

Add tree UI widget

Add a simple to use tree widget.

tree = comet.Tree(header=["Key", "Value"])
spam = tree.append(["spam", 1000])
spam.append(["ham", 2000])
spam.append(["eggs", 3000])
for item in tree:
    item[0].checked = True # set checkd state
    for child in item.children:
        child[0].checked = False
        child[1].color = "green"

tree

Add UI scroll area

Add scroll area providing auto scroll bars.

comet.ScrollArea(layout=comet.Column(
    *[comet.CheckBox(text=f"Option {i+1}") for i in range(32)]
))

scrollarea

ShuntBox switch relais issues

In class comet.devices.hephy.ShuntBox methods enable() and enableAll() raise assertion errors for query results. This is due to trailing blank characters in the result (despite the read termination).

Window resize

Setting application properties width and height result in fixed sized window, should use function resize instead to adjust window size.

Add file/directory open/save UI dialogs

Add functions to create file/directory open/save dialogs.

>>> comet.file_open(...)
'/home/user/sample.txt'
>>> comet.files_open(...)
['/home/user/foo.txt', '/home/user/bar.txt']
>>> comet.directory_open(...)
'/home/user'
>>> comet.file_save(...)
'/home/user/sample.txt'
>>> comet.directory_save(...)
'/home/user'

Add stacked UI widget

Providing a simple UI widget stack using QStackedWidget.

stack = comet.Stack(id="stack", [
    comet.Label(id="p1", "Page 1"),
    comet.Label(id="p2", "Page 2"),
    comet.Label(id="p3", "Page 3")
])
stack.current = 1 # shows page 1
stack.current = stack.get('p3') # shows page 3

Migrate to qutie

Migrate comet.ui, comet.application and comet.process to qutie.

Note: this will introduce breaking changes.

Driver property assignment issue

In Driver.__setattr__ every property is accessed for a type check, resulting in unwanted queries. Driver attributes can be found in Driver.__dict__ (but properties not).

Driver actions and properties

Add method decorator classes for actions and properties like Lantz or Slave do provide for convenient value mapping and list like item access.

class MyDriver(Driver):
    @Action()
    def init(self): ...
    @Property(values={False: 0, True: 1})
    def output(self):
        ...
    @output.setter
    def output(self, value: int):
        ...
    @Property(keys=(0, 1, 2, 3), minimum=-1000.0, maximum=1000.0)
    def channel(self, key):
        ...
    @channel.setter
    def channel(self, key, value):
        ...
>>> with MyDriver(...) as device:
...     device.output = True
...     device.channel[0] = 10.0

Custom UI

Create simple custom UI classes to wrap PyQt5 widgets to provide an easy way to create COMET dashboards.

A bit like a simplified version of Plotly's Dash Python framework.

import comet

app = comet.Application(name='sample')
app.layout = comet.Row(
    comet.Plot(id='plot'),
    comet.Column(
        comet.Button(id='reset', text='Reset'),
        comet.Button(id='clear', text='Clear')
    )
)

@comet.event('refresh', 'clicked')
def refresh(event):
    app.get('plot').reset()
@comet.event('clear', 'clicked')
def clear(event):
    app.get('plot').clear()

app.run()

Add Corvus TT/eco driver

Add driver for Corvus TT/eco instruments (Venus-1 language)

from comet.driver.corvus import Venus1

Venus1 fixes

Corvus Venus-1 driver fixes after hardware testing.

Add message UI dialogs

Add functions to create modal message dialogs.

>>> comet.show_info(title="Hey!", text="This is informative.")
>>> comet.show_warning(title="Ho!", text="Hold your horses.")
>>> comet.show_error(title="An error occurred", text="This is a fatal error.")
>>> comet.show_critical(title="A critical error occurred", text="This is really critical.")
>>> try:
...     no_such_func()
... except NameError as e:
...     comet.show_exception(e)

Using process with functions

Provide an convenient way of using class comet.Process with functions (like class threading.Thread). Attribute target is called if no custom implementaion of run() is provided.

>>> def run(p):
...     while p.running:
...         pass
...
>>> p = comet.Process(id="proc", target=run)
>>> p.start()

Issue with step in linear range function

Linear range class functions.LinearRange does return an empty sequence if step exceeds the absolute ramp distance.

>>> list(LinearRange(0, 1, 5))
[]

It should return

>>> list(LinearRange(0, 1, 5))
[0.0, 1.0]

Resolve pyvisa future warning

FutureWarning: The visa module provided by PyVISA is being deprecated. You can replace import visa by import pyvisa as visa to achieve the same effect.

The reason for the deprecation is the possible conflict with the visa package provided by the https://github.com/visa-sdk/visa-python which can result in hard to debug situations.

Process event handling: revisited

Introducing an event handler for convenient use of callbacks from within processes (threads).

Depending on the thread context an event callback is either called in the same thread the process was created in (usually the main thread) or a PyQt5 signal is emitted from within a worker thread.

Callbacks should be accessible as dictionary and also as attribute for convenience.

def on_reading(data):
    print(data)
def on_finished():
    print("finished.")

def run(p):
    while p.running:
        p.events.reading([42]) # access as attribute
    p.events["finished"]() # access as dict item

app = comet.Application()

p = comet.Process(target=run, events=dict(reading=print)) # initial assign as dict
p.events["reading"] = on_reading # (re-)assign as dict item
p.events.finished = on_finished # assign as attribute
p.start()

app.run() # events require event loop to work!

Delay consecutive write commands

Some individual instruments (observed with a Keithley 2700) might occasionally require an additional delay between consecutive write() commands.

  • add device property write_delay
  • add overriding attribute write_delay to device write() method
  • set individual device write_delay using preferences dialog

Add VISA options to preferences

Add VISA specific instrument options (eg. read_termination, write_termination) to the resources preferences dialog.

It is required to update these options depending on the physical device settings without updating the application code.

Load device/resource settings

An easy approach to load/overwrite settings for registered devices using method load_settings().

...
>>> app.devices.add("smu", K2140(Resource("GBIP::1::INSTR")))
>>> app.devices.get("smu").resource.resource_name
'GBIP::1::INSTR'
...
>>> app.devices.load_settings() # overwrite resource values by settings
>>> app.devices.get("smu").resource.resource_name
'ASRL1::INSTR'

Issue with drivers as class attributes

It turned out to be quite tedious/impractical to forward attributes using property classes, implementing Driver sub modules. Possible solution: stick back to classic attribute instances, prevent attribute assignments for Driver instances.

class Axis(Driver):
    def __init__(self, resource, index):
        super().__init__(resource)
        self._index = index

class Venus1(Driver):
    def __init__(self, resource):
        super().__init__(resource)
        self.x = Axis(resource, 1)
        self.y = Axis(resource, 2)
        self.z = Axis(resource, 3)

# Prevent accidental assignments
>>> device = Venus1(...)
>>> device.x = 4
...
AttributeError: can't set attribute

Linear range issues

Special case

for value in comet.Range(0, 0, 1):
    print(value)

will count up infinite.

Add missing commands to Environment Box

Add following missing commands to HEPHY Environment Box driver and emulator:

  • SET:CTRL ON|OFF and GET:CTRL ? for PID controller state (0=OFF, 1=ON)
  • SET:PID_DOOR_STOP ON|OFF and GET:PID_DOOR_STOP ? for PID door open stop state (1=OFF, 2=ON) (sic!)

Generic instrument emulation socket

Create a generic instrument emulation socket server for testing and development.

from comet.emulator import RequestHandler, TCPServer

class K2700Handler(RequestHandler):
    read_terminaton = "\r"
    write_terminaton = "\r"

    @message(r'\*IDN\?')
    def idn(self, message):
        return "Keithley Model 2700 Emulator"

server = TCPServer(K2700Handler)
server.run("localhost", 10001)

Thread safe immutable data exchange

Introduce thread save immutable data exchange mechanism (exchanging only deep copies) using methods get(key, default=None) and set(key, value)

>>> def run(p):
...     for i in range(42):
...         value = p.get('count', default=0)
...         value += 1
...         p.set('count', value)
>>> p = comet.Process(target=run)
>>> p.set('count', 0)
>>> p.start()
>>> p.join()
>>> p.get('count')
42

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.