def get_session_trials(animal_name, session_filename): ''' Returns a time-ordered list of dicts, where each dict is info about a trial. e.g. [{"trial_num": 1, "behavior_outcome": "failure", "stm_size": 40.0, }, {"trial_num": 2, "behavior_outcome": "success", "stm_size": 35.0 }] :param animal_name: name of the animal string :param session_filename: filename for the session (string) ''' #TODO: unfuck this: hard coded paths not ideal for code reuse # path = 'input/' + animal_name + '/' + session_filename path = os.path.join(input_dir, animal_name, session_filename) df = pymworks.open_file(path) events = df.get_events([ "Announce_TrialStart", "Announce_TrialEnd", "success", "failure", "ignore", "stm_size"] ) trials = [] trial_num = 1 for index, event in enumerate(events): if (event.name == "Announce_TrialStart" and event.value == 1): trial = { "trial_num": trial_num, "stm_size": None, "behavior_outcome": None } try: if events[index - 1].name == "stm_size": trial["stm_size"] = events[index - 1].value except IndexError: print "stm_size out of range for session", session_filename, \ index try: if events[index + 1].name in ["success", "failure", "ignore"]: trial["behavior_outcome"] = events[index + 1].name except IndexError: print "beh_outcome out of range for session", session_filename,\ index if (trial["stm_size"] is not None and trial["behavior_outcome"] is not None): trials.append(trial) trial_num += 1 return trials
def get_session_statistics(animal_name, session_filename): ''' Returns a time-ordered list of dicts, where each dict is info about a trial. e.g. [{"trial_num": 1, "behavior_outcome": "failure", "stm_pos_x": 7.5, }, {"trial_num": 2, "behavior_outcome": "success", "stm_pos_x": -7.5 }] NOTE: trial_num: 1 corresponds to the FIRST trial in the session, and trials occur when Announce_TrialStart and Announce_TrialEnd events have success, failure, or ignore events between them with value=1. :param animal_name: name of the animal string :param session_filename: filename for the session (string) ''' #TODO: unfuck this: hard coded paths not ideal for code reuse #path = 'input/' + animal_name + '/' + session_filename path = os.path.join(data_dir, animal_name, session_filename) df = pymworks.open_file(path) events = df.get_events(["Announce_TrialStart", "Announce_TrialEnd", "success", "failure", "ignore", "stm_pos_x"]) result = [] index = 0 temp_events = [] last_announce = None trial_num = 0 while index < len(events): if events[index].name == "Announce_TrialStart": temp_events = [] last_announce = "Announce_TrialStart" elif events[index].name == "Announce_TrialEnd": if last_announce == "Announce_TrialStart": trial_result = {} for ev in temp_events: if ev.name == "success" and ev.value == 1: trial_result["behavior_outcome"] = "success" elif ev.name == "failure" and ev.value == 1: trial_result["behavior_outcome"] = "failure" elif ev.name == "ignore" and ev.value == 1: trial_result["behavior_outcome"] = "ignore" elif ev.name == "stm_pos_x": trial_result["stm_pos_x"] = ev.value else: pass if "behavior_outcome" in trial_result: trial_num += 1 trial_result["trial_num"] = trial_num result.append(trial_result) last_announce = "Announce_TrialEnd" else: temp_events.append(events[index]) index += 1 #FYI, testing showed some good filtering of weird events here... #blah = df.get_events(["success", "failure", "ignore"]) #print "EVENTS EQUAL? ", len(result) == len(blah) - 6, session_filename #subtract 6 because session initialization emits 2 behavior outcomes per #outcome type #print len(result), len(blah) - 6 #lines above unequal in 6/77 sessions for AB3&7 because of random behavior #outcome events firing in rapid succession. They happens within a couple #microseconds of one another so filtering these out is probably good # save each animal's analyzed session info: out_dir_by_animal = os.path.join(out_dir, '%s' % animal_name) if not os.path.exists(out_dir_by_animal): os.makedirs(out_dir_by_animal) fname = os.path.join(out_dir_by_animal, '%s.pkl' % session_filename) with open(fname, 'wb') as f: pkl.dump(result, f, protocol=pkl.HIGHEST_PROTOCOL) #protocol=pkl.HIGHEST_PROTOCOL) # make_a_figure(data_for_animal) return result
import pymworks input = 'grat10_ephys_160202_1622.mwk/grat10_ephys_160202_1622.mwk' file = pymworks.open_file(input) events = file.get_events('#announceStimulus') # find all unique stimulus names so we know what we're dealing with: stimuli_names = set([ev.value['name'] for ev in events if hasattr(ev.value, '__iter__')]) # take the name and times of a particular stimulus: def get_stim_times(stim_name): stim_times = [ev.time for ev in events if hasattr(ev.value, '__iter__') and ev.value['name'] == stim_name] kwik_times = [tb.mworks_to_audio(x/1e6) for x in stim_times] #mworks times are in microseconds - convert to seconds return kwik_times # make dictionary of stimulus name and its times: stimuli_kwik_times = {stimulus_name : get_stim_times(stimulus_name) for stimulus_name in stimuli_names} # give kwik times to..... .raw.kwd file.... # 1. determine which .raw.kwd file(s) we'll need - compare stimulus times with .kwik files' stamps. stimuli_kwik_times['0'] # collect the raw.kwd files: def get_kwd_files(): input_file_path = os.getcwd() kwd_files = [] for file in os.listdir(input_file_path):
import flask import pymworks #MAXEVENTS = 10000000 MAXEVENTS = None fn = '/home/graham/Repositories/braingram/pymworks/test/error.mwk' if len(sys.argv) > 1: fn = sys.argv[1] debug = False if len(sys.argv) > 2: debug = True datafile = pymworks.open_file(fn) app = flask.Flask(__name__) def events_to_json(events): if (MAXEVENTS is not None) and (len(events) > MAXEVENTS): s = slice(0, len(events), len(events) / MAXEVENTS + 1) else: s = slice(len(events)) return json.dumps([{'code': e.code, 'time': e.time / float(1E6), \ 'value': e.value, 'name': datafile.to_name(e.code)} \ for e in events[s]])
def get_session_statistics(animal_name, session_filename): ''' Returns a time-ordered list of dicts, where each dict is info about a trial. e.g. [{"trial_num": 1, "behavior_outcome": "failure", "stm_pos_x": 7.5, }, {"trial_num": 2, "behavior_outcome": "success", "stm_pos_x": -7.5 }] NOTE: trial_num: 1 corresponds to the FIRST trial in the session, and trials occur when Announce_TrialStart and Announce_TrialEnd events have success, failure, or ignore events between them with value = 1. :param animal_name: name of the animal string :param session_filename: filename for the session (string) ''' #TODO: unfuck this: hard coded paths not ideal for code reuse path = 'input/' + 'phase1/' + animal_name + '/' + session_filename # events_we_want = ['#stimDisplayUpdate','Announce_TrialStart', 'Announce_TrialEnd','success', 'failure', 'ignore'] df = pymworks.open_file(path) # Start by getting the pixel clock / bit code data stimulus_announces = df.get_events['#announceStimulus'] # bit_codes is a list of (time, code) tuples bit_codes = [(e.time, e.value['bit_code']) for e in stimuli if 'bit_code' in e.value] # ok, now let's grab the pixel clock events out of the open-ephys file result = [] index = 0 temp_events = [] last_announce = None trial_num = 0 while index < len(events): if events[index].name == 'Announce_TrialStart': temp_events = [] last_announce = 'Announce_TrialStart' elif events[index].name == 'Announce_TrialEnd': if last_announce == 'Announce_TrialStart': trial_result = {} for ev in temp_events: ### ANIMAL RESPONSE ### if ev.name == 'success' and ev.value == 1: trial_result['behavior_outcome'] = 'success' trial_result['time_trial_response'] = ev.time elif ev.name == 'failure' and ev.value == 1: trial_result['behavior_outcome'] = 'failure' trial_result['time_trial_response'] = ev.time elif ev.name == 'ignore' and ev.value == 1: trial_result['behavior_outcome'] = 'ignore' trial_result['time_trial_response'] = ev.time ### SCREEN INFORMATION ### #elif ev.name == '#stimDisplayUpdate': # print ev.value[0]['name'] elif ev.name == '#stimDisplayUpdate' and len(ev.value) == 1: # this won't actually happen pass elif ev.name == '#stimDisplayUpdate' and len(ev.value) == 2: # if blank screen with pixel clock on. trial_result['bit_code'] = ev.value[1]['bit_code'] # this is the actual bit code on this screen trial_result['stim_name'] = ev.value[0]['name'] # this should be BlankScreen # print 'stim: ', trial_result['stim_name'], ' and bit code: ', trial_result['bit_code'] elif ev.name == 'get_#stimDisplayUpdate' and len(ev.value) == 3: # two options here: 1)blank screen, stimulus, and pixel clock # or 2) blank, pixel, grayscreen (error trial) # if [2] element of ev.value is bit code, do this: if ev.value[2]['name'] == 'pixel clock': trial_result['time_trial_start'] = ev.time trial_result['stim_name'] = ev.value[1]['name'] trial_result['bit_code'] = ev.value[2]['bit_code'] #print "bit code as ev.value[2]['name']" # elif [2] value of ev.value is gray screen (i.e. this is an error trial), elif ev.value[2]['name'] == 'BlankScreenGray': # bit code is under ev.value[1] trial_result['bit_code'] = ev.value[1]['bit_code'] trial_result['stim_name'] = ev.value[2]['name'] #print "gray screen as ev.value[2]['name']" else: #print ev.value[2]['name'] pass ### STIMULUS PROPERTIES ### # elif ev.name == 'stm_size': # trial_result['stm_size'] = ev.value # elif ev.name == 'stm_rotation': # trial_result['stm_rotation'] = ev.value # elif ev.name == 'stm_rotation_in_depth': # trial_result['stm_rotation_in_depth'] = ev.value # elif ev.name == 'stm_pos_x': # if trial_result['stm_pos_x'] == -0.0: #weird MWorks protocol thing where some of the 0.0s come out negative, so just correcting that # trial_result['stm_pos_x'] = abs(ev.value) # else: # trial_result['stm_pos_x'] = ev.value # elif ev.name == 'stm_pos_y': # trial_result['stm_pos_y'] = ev.value else: pass print 'bit code was: ', trial_result['bit_code'], 'and stim was: ', trial_result['stim_name'] if 'behavior_outcome' in trial_result: trial_num += 1 trial_result['trial_num'] = trial_num result.append(trial_result) last_announce = 'Announce_TrialEnd' else: temp_events.append(events[index]) index += 1 #FYI, testing showed some good filtering of weird events here... #blah = df.get_events(["success", "failure", "ignore"]) #print "EVENTS EQUAL? ", len(result) == len(blah) - 6, session_filename #subtract 6 because session initialization emits 2 behavior outcomes per #outcome type #print len(result), len(blah) - 6 #lines above unequal in 6/77 sessions for AB3&7 because of random behavior #outcome events firing in rapid succession. They happen within a couple #microseconds of one another so filtering these out is probably good return result print result
def get_bitcode_simple(animal_name, session_filename): path = '/Volumes/Mac HD/Dropbox (coxlab)/Behavior/3-port-analysis-master/' + 'input/' + 'phase1/' + animal_name + '/' + session_filename display_events = ['#stimDisplayUpdate'] df = pymworks.open_file(path) events = df.get_events(display_events) temp_events = [] last_announce = None trial_num = 0 result = {'bit_code': [], 'stim_name': [], 'time': []} index = 0 while index < len(events): # result['bit_code'].append() trial_result = {'bit_code': [], 'stim_name': [], 'time': []} if events[index].name == '#stimDisplayUpdate' and len( events[index].value) == 1: # this won't actually happen pass elif events[index].name == '#stimDisplayUpdate' and len( events[index].value) == 2: trial_result['bit_code'] = events[index].value[1][ 'bit_code'] # this is the actual bit code on this screen trial_result['stim_name'] = events[index].value[0][ 'name'] # this should be BlankScreen trial_result['time'] = events[index].time elif events[index].name == '#stimDisplayUpdate' and len( events[index].value) == 3: if events[index].value[2]['name'] == 'pixel clock': trial_result['stim_name'] = events[index].value[1]['name'] trial_result['bit_code'] = events[index].value[2]['bit_code'] trial_result['time'] = events[index].time # elif [2] value of ev.value is gray screen (i.e. this is an error trial), elif events[index].value[2]['name'] == 'BlankScreenGray': # bit code is under ev.value[1] trial_result['bit_code'] = events[index].value[1]['bit_code'] trial_result['stim_name'] = events[index].value[2]['name'] trial_result['time'] = events[index].time else: #print ev.value[2]['name'] pass result['bit_code'].append(trial_result['bit_code']) result['stim_name'].append(trial_result['stim_name']) result['time'].append(trial_result['time']) index += 1 #print result return result
def load_mworks(filename): pyfilename = "%s/%s" % (filename, os.path.basename(filename)) mf = MworksFile(filename) pf = pymworks.open_file(pyfilename) return mf, pf
def load_h5(filename): h5filename = os.path.splitext(filename)[0] + '.h5' mf = TableFile(h5filename) pf = pymworks.open_file(filename) return mf, pf
#!/usr/bin/env python import datautils.grouping import pymworks import pymworks.events import gaze.io.mworks import gaze.ops.check.check import gaze.ops.check.trials import gaze.opts.clean.constrain fn = "test.mwk" # open file df = pymworks.open_file(fn) # read gaze data & info (g = 2 tuple) g = gaze.io.mworks.read(df) # find valid gaze data c = gaze.ops.check.check.check_validity(*g) # min_error_mask = gaze.ops.check.check.find_minimum_error_mask(*g) # remove invalid data cg = g[0][c == 0] # remove data outside of 'reasonable' bounds ccg = gaze.ops.clean.constrain.constrain(cg) # read trial data