1"""
2Merging Ceed Data with MCS Data
3================================
4
5The following script shows how to merge the ceed h5 data file that contains all
6the experimental data from the projector stimulation side, with the multi-array
7electrode (MEA) data recorded by the MCS system.
8
9The MCS data must be first exported from the native MCS format to the h5
10format.
11"""
12import logging
13import os.path
14
15from ceed.analysis.merge_data import CeedMCSDataMerger
16
17# The ceed file with the projector experimental data
18ceed_file = '../data/experiment_data_ceed.h5'
19# The h5 file exported by MCS containing the MEA data
20mcs_file = '../data/experiment_data_mcs.h5'
21if not os.path.exists(ceed_file) or not os.path.exists(mcs_file):
22 raise Exception(
23 'Data not found, please manually extract ceed/examples/data/experiment'
24 '_data.zip to ceed/examples/data/')
25
26# The ceed file that will be generated containing the time aligned and merged
27# ceed data and MEA data between the two systems
28output_file = '../data/experiment_data_merged.h5'
29# Optional text notes that will be integrated into the merged file
30notes = """
31At time x, the slice woke up
32"""
33# Optional full filename of a text file containing notes that will be
34# integrated into the merged file. This will be appended to the notes above
35notes_filename = None
36
37# whether to skip failed alignments quietly or to print the exceptions
38debug = False
39
40# class that actually merges the two files
41merger = CeedMCSDataMerger(
42 ceed_filename=ceed_file, mcs_filename=mcs_file, debug=debug)
43
44# read the MCS data - MCS data is one long file containing the data for all the
45# ceed experiments that played. Ceed however, splits the data into experiments
46merger.read_mcs_data()
47# read the overall data from this file, including the Ceed-MCS data link
48# configuration required to be able to align the two files
49merger.read_ceed_data()
50# this parses the Ceed-MCS data-link data adn tries to break the MCS data into
51# experiments so we can later locate individual experiments for alignment
52merger.parse_mcs_data()
53
54# print header for alignment logs
55print(merger.get_skipped_frames_summary_header)
56
57# this dictionary will accumulate the alignment metadata for all the
58# experiments in the data files. Each item is an experiment
59alignment = {}
60# `get_experiment_numbers` lists all the experiments in the ceed file
61# bad experiments to be ignored can be added to the `ignore_list`
62for experiment in merger.get_experiment_numbers(ignore_list=[]):
63 # read and parse the ceed data for this experiment
64 merger.read_ceed_experiment_data(experiment)
65 merger.parse_ceed_experiment_data()
66
67 try:
68 # compute the alignment, ignore remaining frame if Ceed projected more
69 # frames than recorded in MCS file, e.g. if MCS stopped recording
70 # during experiment
71 align = alignment[experiment] = merger.get_alignment(
72 ignore_additional_ceed_frames=True)
73 # see get_skipped_frames_summary for meaning of the printed values
74 print(merger.get_skipped_frames_summary(align, experiment))
75 except Exception as e:
76 print(
77 "Couldn't align MCS and ceed data for experiment "
78 "{} ({})".format(experiment, e))
79 if debug:
80 logging.exception(e)
81
82# now actually merge the files and include the alignment metadata
83merger.merge_data(
84 output_file, alignment, notes=notes, notes_filename=notes_filename)