# Path: telluride_decoding/ingest.py
# FLAGS = flags.FLAGS
# class BrainSignal(object):
# class BrainTrial(object):
# class BrainDataFile(object):
# class MemoryBrainDataFile(BrainDataFile):
# class LocalCopy(object):
# class EdfBrainDataFile(BrainDataFile):
# class BrainExperiment(object):
#   def __init__(self, name, signal, sample_rate, data_type=None):
#   def signal(self):
#   def data_type(self):
#   def sr(self):
#   def name(self):
#   def fix_offset(self, offset_seconds):
# def find_temporal_offset_via_linear_regression(audio_trigger_times,
#                                                eeg_trigger_times,
#                                                verbose=True):
# def find_temporal_offset_via_mode_histogram(audio_triggers, eeg_triggers,
#                                             max_time=0, fs=0):
# def remove_close_times(times, min_time=0.06):
#   def __init__(self, trial_name):
#   def model_features(self):
#   def brain_data(self):
#   def model_features(self, new_dict):
#   def add_model_feature(self, name, data):
#   def trial_name(self):
#   def summary_string(self):
#   def load_sound(self, sound_data, sound_fs=None, sound_dir=None):
#   def load_brain_data(self, eeg_dir, brain_data):
#   def iterate_brain_channels(self, data_type=None):
#   def adjust_data_sizes(self, data_dict):
#   def find_audio_trigger_times(self, channel_with_trigger=1):
#   def find_eeg_trigger_times(self, channel_name='TRIG'):
#     def natus_trigger_fix(x):
#   def find_cognionix_trigger_time(self, channel_name='EXP32', level=8000):
#   def fix_eeg_offset(self, offset_seconds):
#   def assemble_brain_data(self, eeg_channel_names):
#   def write_data_as_tfrecords(self, tf_dir,
#                               reverse_data_for_test=False):
#   def __init__(self, data_filename, data_type=None):
#   def filename(self):
#   def data_type(self):
#   def __str__(self):
#   def load_all_data(self, _):
#   def __init__(self, trial_dict, sr=64, data_type=None, name='in_memory'):
#   def signal_names(self):
#   def signal_values(self, name):
#   def signal_fs(self, _):
#   def __init__(self, remote_filename):
#   def __enter__(self):
#   def __exit__(self, exception_type, exception_value, traceback):
# def parse_edf_file(sample_edf_file):
#   def __init__(self, filename, data_type=None, **kwds):
#   def load_all_data(self, data_dir):
#   def signal_names(self):
#   def signal_values(self, name):
#   def signal_fs(self, name):
#   def find_channel_index(self, desired_label='TRIG'):
#   def delete_suffix(filename, suffix):
#   def __init__(self, trial_dict, sound_dir, eeg_dir, frame_rate=64):
#   def trial_data(self, key):
#   def add_sound_data(self, sound_dict, trial):
#   def iterate_trials(self):
#   def load_all_data(self, sound_dir, eeg_dir, verbose=False):
#   def check_sound_eeg_files(self):
#   def summary(self):
#   def get_all_feature_data(self, feature_name):
#   def zscore_all_features(self, feature_name, mean, std):
#   def z_score_all_data(self):
#   def save_zscore_data(self, filename):
#   def write_all_data(self, tf_dir):
# def find_mean_std(data_list, columnwise=False):
# def normalize_data(a, data_mean, data_std):
# def convert_data_to_tfrecords(filename, data_dict):
#   def _int64_feature(value):
#   def _bytes_feature(value):
#   def _float_feature(value):
# def discover_feature_shapes(tfrecord_file_name):
# def count_tfrecords(tfrecord_file_name):
# def read_tfrecords(tfrecord_file_name, start_frame=0, frame_count=512):
# def transform_tfrecords(input_file, new_tf_dir, trial_name, transforms):
# def main(argv):


import collections
import math
import os
import tempfile
import numpy as np
import scipy.io.wavfile
import scipy.signal
import tensorflow.compat.v2 as tf
from absl import flags
from absl.testing import absltest
from telluride_decoding import ingest
    test_source = 'test_source'
    test_sr = 4
    test_data = np.arange(10)
    s = ingest.BrainSignal(test_name, test_data, test_sr, test_source)
    self.assertEqual(s.name, test_name)
    self.assertEqual(s.data_type, test_source)
    self.assertEqual(s.sr, test_sr)
    self.assertTrue(np.all(np.reshape(test_data, (-1, 1)) == s.signal))

    s.fix_offset(1)
    self.assertEqual(s.signal[0], 4)
    self.assertEqual(s.signal[-1], 9)

    # Test to make sure fix_offset works with 2d signals.
    s = ingest.BrainSignal('test', np.reshape(np.arange(20), (10, -1)), 4)
    s.fix_offset(1)
    self.assertLen(s.signal.shape, 2)
    self.assertEqual(s.signal[0, 0], 8)
    self.assertEqual(s.signal[0, 1], 9)

    # Test some of the parameter checking.
    with self.assertRaises(ValueError):
      s = ingest.BrainSignal(42, test_data, test_sr, test_source)

  def test_memory_brain_data_file(self):
    one_data = np.arange(10) + 100
    two_data = np.arange(10) + 200
    channel_data = {'one': one_data,
                    'two': two_data}
    test_sr = 4

# Path: telluride_decoding/ingest.py
# FLAGS = flags.FLAGS
# class BrainSignal(object):
# class BrainTrial(object):
# class BrainDataFile(object):
# class MemoryBrainDataFile(BrainDataFile):
# class LocalCopy(object):
# class EdfBrainDataFile(BrainDataFile):
# class BrainExperiment(object):
#   def __init__(self, name, signal, sample_rate, data_type=None):
#   def signal(self):
#   def data_type(self):
#   def sr(self):
#   def name(self):
#   def fix_offset(self, offset_seconds):
# def find_temporal_offset_via_linear_regression(audio_trigger_times,
#                                                eeg_trigger_times,
#                                                verbose=True):
# def find_temporal_offset_via_mode_histogram(audio_triggers, eeg_triggers,
#                                             max_time=0, fs=0):
# def remove_close_times(times, min_time=0.06):
#   def __init__(self, trial_name):
#   def model_features(self):
#   def brain_data(self):
#   def model_features(self, new_dict):
#   def add_model_feature(self, name, data):
#   def trial_name(self):
#   def summary_string(self):
#   def load_sound(self, sound_data, sound_fs=None, sound_dir=None):
#   def load_brain_data(self, eeg_dir, brain_data):
#   def iterate_brain_channels(self, data_type=None):
#   def adjust_data_sizes(self, data_dict):
#   def find_audio_trigger_times(self, channel_with_trigger=1):
#   def find_eeg_trigger_times(self, channel_name='TRIG'):
#     def natus_trigger_fix(x):
#   def find_cognionix_trigger_time(self, channel_name='EXP32', level=8000):
#   def fix_eeg_offset(self, offset_seconds):
#   def assemble_brain_data(self, eeg_channel_names):
#   def write_data_as_tfrecords(self, tf_dir,
#                               reverse_data_for_test=False):
#   def __init__(self, data_filename, data_type=None):
#   def filename(self):
#   def data_type(self):
#   def __str__(self):
#   def load_all_data(self, _):
#   def __init__(self, trial_dict, sr=64, data_type=None, name='in_memory'):
#   def signal_names(self):
#   def signal_values(self, name):
#   def signal_fs(self, _):
#   def __init__(self, remote_filename):
#   def __enter__(self):
#   def __exit__(self, exception_type, exception_value, traceback):
# def parse_edf_file(sample_edf_file):
#   def __init__(self, filename, data_type=None, **kwds):
#   def load_all_data(self, data_dir):
#   def signal_names(self):
#   def signal_values(self, name):
#   def signal_fs(self, name):
#   def find_channel_index(self, desired_label='TRIG'):
#   def delete_suffix(filename, suffix):
#   def __init__(self, trial_dict, sound_dir, eeg_dir, frame_rate=64):
#   def trial_data(self, key):
#   def add_sound_data(self, sound_dict, trial):
#   def iterate_trials(self):
#   def load_all_data(self, sound_dir, eeg_dir, verbose=False):
#   def check_sound_eeg_files(self):
#   def summary(self):
#   def get_all_feature_data(self, feature_name):
#   def zscore_all_features(self, feature_name, mean, std):
#   def z_score_all_data(self):
#   def save_zscore_data(self, filename):
#   def write_all_data(self, tf_dir):
# def find_mean_std(data_list, columnwise=False):
# def normalize_data(a, data_mean, data_std):
# def convert_data_to_tfrecords(filename, data_dict):
#   def _int64_feature(value):
#   def _bytes_feature(value):
#   def _float_feature(value):
# def discover_feature_shapes(tfrecord_file_name):
# def count_tfrecords(tfrecord_file_name):
# def read_tfrecords(tfrecord_file_name, start_frame=0, frame_count=512):
# def transform_tfrecords(input_file, new_tf_dir, trial_name, transforms):
# def main(argv):


# Path: test/ingest_test.py
import collections
import math
import os
import tempfile
import numpy as np
import scipy.io.wavfile
import scipy.signal
import tensorflow.compat.v2 as tf
from absl import flags
from absl.testing import absltest
from telluride_decoding import ingest

    test_source = 'test_source'
    test_sr = 4
    test_data = np.arange(10)
    s = ingest.BrainSignal(test_name, test_data, test_sr, test_source)
    self.assertEqual(s.name, test_name)
    self.assertEqual(s.data_type, test_source)
    self.assertEqual(s.sr, test_sr)
    self.assertTrue(np.all(np.reshape(test_data, (-1, 1)) == s.signal))

    s.fix_offset(1)
    self.assertEqual(s.signal[0], 4)
    self.assertEqual(s.signal[-1], 9)

    # Test to make sure fix_offset works with 2d signals.
    s = ingest.BrainSignal('test', np.reshape(np.arange(20), (10, -1)), 4)
    s.fix_offset(1)
    self.assertLen(s.signal.shape, 2)
    self.assertEqual(s.signal[0, 0], 8)
    self.assertEqual(s.signal[0, 1], 9)

    # Test some of the parameter checking.
    with self.assertRaises(ValueError):
      s = ingest.BrainSignal(42, test_data, test_sr, test_source)

  def test_memory_brain_data_file(self):
    one_data = np.arange(10) + 100
    two_data = np.arange(10) + 200
    channel_data = {'one': one_data,
                    'two': two_data}
    test_sr = 4

    df = ingest.MemoryBrainDataFile(channel_data, test_sr)