cosplay package¶
Submodules¶
cosplay.cli module¶
-
cosplay.cli.
main
()¶ Process arguments and pass them to server.main.
-
cosplay.cli.
return_parser
()¶ Return argparse argument parser.
cosplay.dump_mem module¶
From Dave Hylands’ json-ipc/dump_mem.py ( https://github.com/dhylands/json-ipc.git ). Provides the dump_mem function, which dumps memory in hex/ASCII.
-
cosplay.dump_mem.
dump_mem
(buf, prefix='', address=0, line_width=16, show_ascii=True, show_addr=True, log=<function print_>)¶ Dumps out a hex/ASCII representation of the given buffer.
-
cosplay.dump_mem.
hexlify
(buf)¶
-
cosplay.dump_mem.
print_
(s)¶ Print string s.
In Python 2, print is not a function. This function makes it possible to use it like one.
cosplay.pkt module¶
This file is based on Dave Hylands’ json-ipc/json_pkt.py ( https://github.com/dhylands/json-ipc.git ).
-
class
cosplay.pkt.
Packet
(serial_port, show_packets=False)¶ -
ANS_no
= 0¶
-
ANS_yes
= 1¶
-
INS_ask_user
= 3¶
-
INS_check_for_sequences_on_server
= 2¶
-
INS_send_sequences
= 4¶
-
STATE_EOT
= 8¶
-
STATE_ETX
= 6¶
-
STATE_LEN_0
= 1¶
-
STATE_LEN_1
= 2¶
-
STATE_LRC
= 7¶
-
STATE_PAYLOAD
= 5¶
-
STATE_SOH
= 0¶
-
STATE_STX
= 4¶
-
STATE_TYPE
= 3¶
-
process_byte
(byte)¶ Process a single byte. Return an object when one is successfully parsed, otherwise return None.
Parameters: byte (bytes object / bytearray) – Input byte. For MicroPython and Python 3 this should be a bytes object. In Python 2 a bytearray should be used. Returns: out – Returns object if package is successfully parsed, otherwise returns None. Return type: 2d array / string / int
-
receive
(time_out=0)¶ Try to receive an object.
This function tries to receive an object until time_out. If a byte is received, time_out becomes obsolete and the function times out if no more bytes are received for 1s. Returns None upon time out.
Parameters: time_out (int) – Approximate time in ms until return if no bytes are received. If time_out = 0, the function never times out. Returns: out – Received object or None in case of time out. Return type: object
-
send
(obj)¶ Convert a Python object into its string representation and then send it using the ‘serial_port’ passed in the constructor.
Parameters: obj (list, string or int) – object that is send via ‘serial_port’
-
-
cosplay.pkt.
lrc
(str)¶ Return longitudinal redundancy checksum.
cosplay.serial_port module¶
Based on Dave Hylands’ json-ipc/serial_port.py (https://github.com/dhylands/json-ipc.git). This module implements the SerialPort class, which allows the host to talk to another device using a serial like interface over a UART.
-
class
cosplay.serial_port.
SerialPort
¶ Bases:
object
Implements a PySerial port.
-
close_serial
()¶ Close serial connection if it is open.
-
connect_serial
(port, baud=115200)¶ Try to connect to serial port.
This function tries to connect to a serial port named port.
Parameters: port (string) – name of port (e.g. /dev/ttyACM0) Returns: True if connection could be established, otherwise False. Return type: bool
-
is_byte_available
()¶ Check if byte can be read from serial port.
-
read_byte
()¶ Read a byte from the serial port.
Returns: Value of byte. Return type: int
-
write
(data)¶ Write data to the serial port.
Parameters: data (string / bytearray) – Data to write.
-
-
cosplay.serial_port.
autoscan
()¶ Check all serial ports to see if they are MicroPython devices.
Checks serial ports until it finds a port with matching VID:PID for a MicroPython board.
Returns: Full device name/path. None if no MicroPython device was found. Return type: string
-
cosplay.serial_port.
is_micropython_usb_device
(port)¶ Check a USB device to see if it looks like a MicroPython device.
Parameters: port (serial.tools.list_ports object) – Port to check. Returns: True if device connected to ‘port’ looks like a MicroPython device, False otherwise. Return type: bool
cosplay.server module¶
-
cosplay.server.
ask_user
()¶ Ask user whether sequences on server or microcontroller shall be used.
Returns: True if sequences on server shall be used, False otherwise. Return type: bool
-
cosplay.server.
check_for_sequences
(sequences_arg=None)¶ Try to find sequences.
This function returns a list of paths to all files ending with ‘.tsv’ in sequence_arg. Shell-style wildcards can be used. sequences_arg can be directories, files or a mixture of both. Directories are searched non-recursively on the first level. If no sequence_arg is specified, the default location of COSgen is used.
Parameters: sequences_arg (string, optional) – Path to sequence files, can be None. Returns: List of paths to sequence files. None if no sequences were found. Return type: list
-
cosplay.server.
connect
(port_name=None)¶ Establish connection to pyboard.
This function tries to connect to port_name. If port_name is None, tries to connect to the first serial port with a maching VID:PID for the MicroPython Pyboard.
Parameters: port_name (string, optional) – Name of port the microcontroller is connected to. If None, tries to automatically detected port and connect. Default is None. Returns: port – Port object that is connected. None if no connection could be establish. Return type: cosplay.serial_port.SerialPort object
-
cosplay.server.
find_current_scan_dir
(vendor, storage_root=None)¶ Find directory of current scan.
This function finds the scan directory with the most recent inode change time of the fid file.
Parameters: vendor (string) – Name of the MRI vendor, currently only Bruker is supported. Returns: Path to current scan directory. Return type: string
List all entries in path excluding hidden ones.
This function implements the same functionality as os.listdir but ignores hidden entires.
Parameters: path (string) – Path to directory Returns: List of all non-hidden entries in the directory. Return type: list
-
cosplay.server.
main
(verbose, vendor, port_name, sequences, storage_path=None, storage_root=None)¶ Main function running on server.
This function constantly tries to receive data from pyboard. It acts according to the instructions received from the board. Exit this function by pressing CTRL-C.
Parameters: - verbose (int) – Verbosity level.
- vendor (string) – Vendor of the MRI scanner.
- port_name (string) – Port name.
- sequences (string) – Path to sequences (can include wildcards).
- storage_path (string) – String to storage location for delivered sequences.
-
cosplay.server.
process_message
(obj, error_msgs)¶ Add obj to error_msgs if it is an error message.
Parameters: - obj (string) – Input message.
- error_msgs (string) – Already accumulated error messages.
Returns: updated error_msgs
Return type: string
-
cosplay.server.
save_sequence
(obj, storage_path, error_msgs, vendor, verbose=0, storage_root=None)¶ Save sequence in storage_path.
This function saves a sequence and error messages in two separate files. If storage_path is None, the files are saved in the most recent scan directory (see ‘find_current_scan_dir’).
Parameters: - obj (2d matrix) – Sequence.
- storage_path (string) – Path to directory, where files shall be stored. If it is None, the most recent scan directory is used.
- error_msgs (string) – String containing error messages. Is stored in same directory as the sequence.
- vendor (string) – Name of MRI vendor.
- verbose (int, optional) – If ‘verbose’ is larger than 1, the sequence is printed to the screen. Default is 0.
-
cosplay.server.
send_sequences
(sequences_paths, pkt, verbose)¶ Send sequences to microcontroller.
Parameters: - sequences_paths (list) – List of paths (strings) to sequences that are sent.
- pkt (cosplay.pkt Packet object) – Object that sends the sequences.
- verbose (int) – If larger than 1, print path to every sequence sent. If larger than 2, print every sequence sent.
-
cosplay.server.
signal_handler_end_program
(signal, frame)¶
cosplay.tsv module¶
-
cosplay.tsv.
cast
(s)¶ Cast string to float if possible.
This functions casts a string to float if possible and returns the string otherwise.
Parameters: s (string) – Input strings Returns: Float number of string input s, if possible, s otherwise. Return type: float or string
-
cosplay.tsv.
dump
(matrix, file_obj)¶ Serialize matrix as a tsv formatted stream to file_obj.
This function converts a matrix into a tsv formatted string and stores it in a file.
Parameters: - matrix (2d array) – Input matrix.
- file_obj (file_obj) – A .write()-supporting file-like object.
-
cosplay.tsv.
dumps
(matrix)¶ Serialize matrix to a tsv formatted string.
Parameters: matrix (2d array) – Input matrix. Returns: tsv formatted string. Return type: string
-
cosplay.tsv.
load
(file_obj)¶ Load BIDS sequence tsv file [BIDS].
This function converts a BIDS sequence tsv file to a matrix.
Parameters: file_obj (file object) – File object of BIDS sequence tsv file [BIDS]. Returns: First line contains names for columns. First columns contains event numbers. Return type: 2d array
-
cosplay.tsv.
loads
(s)¶ Convert tsv formatted string into matrix.
This function converts a ‘Brain Image Data Structure’ [BIDS] sequence tsv string into a matrix.
Parameters: s (string) – String form BIDS sequence tsv file[BIDS]. Returns: First line contains names for columns. First columns contains event numbers. Return type: 2d array
[BIDS] | Brain Imaging Data Structure Specification (http://bids.neuroimaging.io/bids_spec1.0.1.pdf) |