#!/usr/bin/python
"""
RECORDINGS EXAMPLE
This script illustrates how basic recordings can be run
via the python API. The following steps are needed for a full
recording:
1. Initialize the recording settings.
2. Prepare the stimulation array (see stimulate.py for more
explanation).
3. Define an electrode group for data storage.
4. Begins the recording.
5. Stop the recording and save the file.
For further explanation about specific functions, please have
a look at the API documentation.
In order to view raw traces, you must have the decompression library
installed. Instructions to do so can be found here:
https://share.mxwbio.com/d/5b5017febe354c8e942a/
"""
import maxlab as mx
import matplotlib.pyplot as plt
import numpy as np
import time
from h5py import File
import os
# Recordings will be saved in the current working directory
dir_name = os.getcwd()
# File name of the recording
file_name = "test_recording"
full_file_name = f"{dir_name}/{file_name}.raw.h5"
# fmt: off
electrodes = [4446,4447,4448,4665,4666,4667,4668,4669,4885,
4886,4887,4888,4889,5105,5106,5107,5108,5109,
5326,5327,5328,
17095,17096,17097,17314,17315,17316,17317,17318,
17533,17534,17535,17536,17537,17538,17539,17753,
17754,17755,17756,17757,17758,17759,17973,17974,
17975,17976,17977,17978,17979,18194,18195,18196,
18197,18198,18415,18416,18417]
# fmt: on
def run_experiment():
if os.path.isfile(full_file_name):
print(f"File {full_file_name} already exists. Please delete or rename the file")
exit()
# Initialize system
mx.initialize()
if mx.send(mx.Core().enable_stimulation_power(True)) != "Ok":
raise RuntimeError("The system didn't initialize correctly.")
time.sleep(mx.Timing.waitInit)
# If we use MaxTwo, recordings from multiple wells can be generated.
# In order to do so, we need to specify from which well(s) we wish to
# record with the following variable. E.g., if wells 2 and 4, then
# recording_wells = [2, 4]. For this example, we use well 0 only:
wells = [0]
mx.activate(wells)
# Prepare the stimulation array
array = mx.Array("test")
array.select_electrodes(electrodes)
array.route()
array.download()
time.sleep(mx.Timing.waitAfterDownload)
mx.offset()
time.sleep(mx.Timing.waitInMX2Offset)
mx.clear_events() # Empty event-buffer before adding anything to it
s = mx.Saving()
s.open_directory(dir_name)
s.start_file(file_name)
# With the new data format, we must declare which electrodes
# we want to store data from. This can be set through the group_define
# function which has the following form:
# s.group_define(well_nr, "any_name", list_of_channels_to_record_from)
# Here, data only from routed channels are saved.
config = array.get_config()
routed_channels = config.get_channels()
s.group_define(wells, "routed_channels", routed_channels)
s.start_recording(wells)
print("Start recording")
time.sleep(10)
print("Stop recording")
s.stop_recording()
time.sleep(mx.Timing.waitAfterRecording)
s.stop_file()
s.group_delete_all()
# Find location of saved data and display
print(f"Data saved at {full_file_name}.")
def visualize_data():
# Plot recorded data
file = File(full_file_name)
traces = np.array(file["wells/well000/rec0000/groups/routed_channels/raw"]).astype(
float
)
lsb = file["wells/well000/rec0000/settings/lsb"][0] # bits to V multiplier
sampling_rate = file["wells/well000/rec0000/settings/sampling"][
0
] # frames per second
# Plot the first n voltage traces
n = 5
fig, axs = plt.subplots(
n,
1,
figsize=(14, n * 1.5),
dpi=100,
sharex=True,
facecolor="w",
edgecolor="k",
)
for i in range(n):
times = np.linspace(
0, len(traces[i]) / (sampling_rate / 1000), len(traces[i])
) # times in ms
voltages = (
(512 - traces[i, :]) * lsb * 10**6
) # Voltage, in uV. Note that the voltage signal is inverted relative to the bit value.
axs[i].plot(times, voltages)
# Add axis labels
axs[i].set_ylabel("uV")
axs[i].set_xlabel("ms")
plt.tight_layout()
plt.show()
if __name__ == "__main__":
run_experiment()
visualize_data()