MagVenture class

This is a python translation of the MagVenture part of the MAGIC toolbox (https://github.com/nigelrogasch/MAGIC).

MAGICPy Copyright (C) 2021 Ole Numssen

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

class magicpy.magicpy.MagVenture(portid, flush_before_cmd=True, wait_s=1, wait_l=1)

Class to manage communication with an Magventure TMS stimulator connected to a serial port.

Example:

> m = MagVenture(‘/dev/ttyS4’) > m.connect() > m.arm() > m.set_amplitude(50) > m.fire() > m.disarm() > m.disconnect()

portserial.Serial()

Information about the serial connection.

arm(get_response=False)

Enables the stimulator.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

static bin_z(val, z=8)

Integer to binary conversion. Includes zero padding and stripping of the ‘0b’ prefix.

Example: MagVenture.bin_z(1,z=8) -> ‘00000001’ Parameters ———- val : int z : int, default: 8

Length of string to pad with zeros.

bin_val : str

static calc_crc(cmd)

Computes CRC code needed for the MagVenture device to accept the command.

cmdlist of str

The command that should be send to the stimulator as bytestring. E.g.: [‘01’,’02’]

Str : The CRC in hexstring.

connect(force=False)

Opens or reopens the desired port.

forcebool

Reopen if already open. Default is False.

disarm(get_response=False)

Disables the sitmulator.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

disconnect()

Closes the port.

fire(get_response=False)

Fires a single pulse. Note that this is sent through the serial port and thus has a high jitter. If you need precise pulse timing, you need to send TTL pulse triggers to the stimulators Trigger-in port.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

get_baratio()

Gets the B/A Ratio

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

get_burst()

Sets the train parameters.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

get_current_dir()

Gets the current direction.

device_responsestr

The current direction, one of ‘Normal’ or ‘Reversed’.

errorbool

Non-zero value indicates error code, or zero on success.

get_ipi()

Gets interpulse interval msec.

interpulse interva,float

The interpulse in msec

errorbool

Non-zero value indicates error code, or zero on success.

get_mode()

Gets stimulator mode information.

modestr

The stimulators mode, one of ‘Standard’, ‘Power’, ‘Twin’, ‘Dual.

errorbool

Non-zero value indicates error code, or zero on success.

get_raw_info()

Gets all current raw information from device for set commands.

Informationdict

Raw information from stimulator.

get_status()

Gets the status of the stimulator.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

get_status2()

Gets the second status (Status2) of the stimulator.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

get_status_set_get(get_response=False)

# TODO: remove this and use status2() Get info from device in set/get commands

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

get_waveform()

Gets the waveform from stimulator..

waveformstr

The device’ waveform.

errorbool

Non-zero value indicates error code, or zero on success.

static hexlm(val, fac=1, z=4)

Get LSB and MSB from hexified, zeropadded value.

val : float fac : int, default: 1

Multiplication factor

zint, default: 4

Zero padding to z

LSB : str MSB : str

parse_response(read_data, only_last=True)

Parses the stimulator response.

read_databytes

Bytestring. The raw response from the stimulator.

only_lastbool

Return only the last response if multiple responses are in read_data. If not, returns turn into lists of dict.

infodict or list of dict

The parsed response sent back from the device.

info_rawdict or list of dict

The raw response sent back from the device.

process_command(command_length, command_bytes, resp_len_exp=8, get_response=False)

Prepares the command to be sent to the stimulator. Start and end bytes are added, the crc for the command_bytes are computed, and everything is sent to the stimulator.

command_lengthstr

Length of command to be sent to the stimulator, in bytes.

command_byteslist of str or str

Stimulator command as bytestring list.

resp_len_expint

Length of stimulator response that one expects.

get_responsebool or int

Whether to request a response from the stimulator. Default: False. get_response = 2 or 3 returns raw_response.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

send_train(get_response=False)

Sends a train of pulses to the specified port.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

set_amplitude(a_amp, b_amp=None, get_response=False)

Sets the amplitude in standard or twin mode. If only one amplitude (a_amp) is given, standard mode is selected. If both amplitudes (a_amp, b_amp) are given, Dual/ Twin Mode is selected.

a_ampint

Indicates A amplitude in percentage in Standard Mode

Default is set to ‘None’

b_ampint, optinal

Indicates B amplitude in percentage in Dual/ Twin Mode (otherwise only A amplitude required) Default is None

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

set_baratio(baratio, current_dir=None, n_pulses_per_burst=None, ipi=None, get_response=False)

Sets the B/A Ratio

If current_dir, n_pulses_per_burst, or ipi is not set it is read from the stimulator. This takes some time, so better set arguments accordingly.

baratioint

When working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

current_dirstr

Current direction of the stimulator, one of ‘Normal’, ‘Reverse’.

n_pulses_per_burstint

Biphasic pulses (= burst) per trigger in. Can be one of 2,3,4,5.

ipifloat

represents Inter Pulse Interval which defines the duration between the beginning of the first pulse to the beginning of the second pulse.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

set_burst(n_pulses_per_burst, current_dir=None, ipi=None, baratio=None, get_response=False)

Sets the number of pulses per burst.

If current_dir, ipi, or baratio is not set it is read from the stimulator. This takes some time, so better set arguments accordingly.

current_dir: str

defines current direction of the device in the current status. Valid inputs in magventure:’Normal’,’Reverse’

n_pulses_per_burst: int

Biphasic Burst index in the current status of the device which can be 2,3,4, or 5 pulses in each stimulation

ipi: int

represents Inter Pulse Interval of the current status of the device which defines the duration between the beginning of the first pulse to the beginning of the second pulse.

baratio: int

when working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

set_current_dir(current_dir, n_pulses_per_burst=None, ipi=None, baratio=None, get_response=False)

Sets the current direction.

current_dirstr

Current direction of the stimulator, one of ‘Normal’, ‘Reverse’.

n_pulses_per_burstint

Biphasic pulses (= burst) per trigger in. Can be one of 2,3,4,5.

ipiint

The inter pulse interval defines the duration between the beginning of the first pulse to the beginning of the second pulse in a burst.

baratioint

When working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

set_ipi(ipi, current_dir=None, n_pulses_per_burst=None, baratio=None, get_response=False)

Sets the interpulse interval.

If current_dir, n_pulses_per_burst, or baratio is not set it is read from the stimulator. This takes some time, so better set arguments accordingly.

ipi: float

Inter pulse interval in ms, which defines the duration between the beginning of the first pulse to the beginning of the second pulse.

current_dir: str

Current direction of the stimulator, one of ‘Normal’, ‘Reverse’.

n_pulses_per_burst: int

Biphasic Burst index in the current status of the device which can be 2, 3, 4, or 5 pulses in each stimulation.

baratio: int

when working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device

errorbool

Non-zero value indicates error code, or zero on success.

set_mode(mode, current_dir, n_pulses_per_burst, ipi, baratio=1, get_response=False)

Sets the stimulation mode. This includes current directions, burst_pulses, and others. Note that you can use burst_pulses to send out multiple single pulses as a burst for each trigger-in.

modestr

Working mode, one of ‘Standard’,’Power’,’Twin’,’Dual’

current_dirstr

Current direction of the stimulation. One of ‘Normal’, ‘Reverse’.

n_pulses_per_burstint

Biphasic pulses (= burst) per trigger in. Can be one of 2,3,4,5.

ipiint

The inter pulse interval defines the duration between the beginning of the first pulse to the beginning of the second pulse in a burst.

baratio: int

When working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

set_page(page, get_response=False)

Configures the desired page.

pagestr

Which page to configure. One of ‘Main’,’Train’,’Trig’,’Config’,’Protocol’.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

set_train(reprate, n_pulses, n_trains, iti, get_response=False)

Sets train parameters. A train is a sequence of multiple pulses in a specific rate. Multiple trains (of the same type) can be sent successively.

reprateint

Number of pulses per second.

n_pulsesint

Number of pulses in each train.

n_trains: int

Total amount of trains per sequence.

itiint

Time interval between two trains in seconds. This is the time period between the last pulse in the first train and the first pulse in the next train.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

set_waveform(waveform, current_dir=None, n_pulses_per_burst=None, ipi=None, baratio=None, get_response=False)

Sets the waveform.

If current_dir, n_pulses_per_burst, ipi, baratios is not set it is read from the stimulator. This takes some time, so better set arguments accordingly.

waveform: str

defines the desired waveform. Valid inputs in magventure: ‘Monophasic’,’Biphasic’,’HalfSine’,’BiphasicBurst’

current_dir: str

defines current direction of the device in the current status. Valid inputs in magventure:’Normal’,’Reverse’

n_pulses_per_burst: int

Biphasic Burst index in the current status of the device which can be 2,3,4, or 5 pulses in each stimulation

ipi: int

represents Inter Pulse Interval of the current status of the device which defines the duration between the beginning of the first pulse to the beginning of the second pulse.

baratio: int

when working in Twin Mode, the amplitude of the two pulses A and B are controlled in an adjustable ratio between 0.2-5.0. “Pulse B” is now adjusted to a selected percent ratio proportional to “Pulse A”.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.

settrig_chargedelay(trig_in_delay, trig_out_delay, charge_delay, get_response=False)

Sets trigger and charge delay.

trig_in_delayint

Trigger-in delay in millliseconds.

trig_out_delay: int

Trigger-out delay in milliseconds.

charge_delay: int

Time in milliseconds to make the device wait before recharging.

get_responsebool

Whether to request a response from the stimulator. Default: False.

device_responsedict

The response sent back from the device.

errorbool

Non-zero value indicates error code, or zero on success.