def setupDirs(options, doPanos=False, log=False): projectSubDir = str(options.year)+"_"+str(options.project_number)+"_"+options.project_name if options.year and options.project_number and options.project_name else "" for use in tools.getUses(options): dirToCreate = os.path.join(tools.getDir(options.base_dir, use), str(options.year), projectSubDir) tools.makeDir(dirToCreate, log=log) if doPanos and use=="pano": for panoName in options.panos: tools.makeDir(os.path.join(dirToCreate, panoName), log=log)
def makeTableScan(input_dir, output_dir, output_file): name = "{0}/{1}".format(output_dir, output_file) input_file_pattern = "{0}/TAP0_*/*.csv".format(input_dir) input_files = glob.glob(input_file_pattern) # get dictionary mapping TAP0 setting to files file_dict = {} for tap in settings: tap_name = "TAP0_{0}".format(tap) tap_match = "/{0}/".format(tap_name) for f in input_files: if tap_match in f: file_dict[tap] = f tools.makeDir(output_dir) run(file_dict, name)
def makeTable(input_dir, output_dir, output_file_name, cable_type): # setup input and output output_file = "{0}/{1}".format(output_dir, output_file_name) input_file_pattern = "{0}/*.csv".format(input_dir) tools.makeDir(output_dir) input_files = glob.glob(input_file_pattern) # map to define modules and channels for each cable type cable_type_map = { 1: { "modules": ["M1"], "channels": ["CMD", "D0", "D1", "D2", "D3"], "requireModule": False }, 2: { "modules": ["M1"], "channels": ["CMD", "D0", "D1"], "requireModule": False }, 3: { "modules": ["M1", "M2"], "channels": ["CMD", "D0", "D1"], "requireModule": True }, 4: { "modules": ["M1", "M2", "M3"], "channels": ["CMD", "D0", "D1"], "requireModule": True }, } # get values from map modules = cable_type_map[cable_type]["modules"] channels = cable_type_map[cable_type]["channels"] requireModule = cable_type_map[cable_type]["requireModule"] # create table run(input_files, output_file, modules, channels, requireModule)
def process(cable_number, input_dir, output_dir, output_file): print("Processing Cable {0}".format(cable_number)) tools.makeDir(output_dir) out = "{0}/{1}".format(output_dir, output_file) output_column_titles = [ "Cable Number", "Separation (mm)", "Primary Link (Channel)", "Primary 741 mV, Secondary 269mV", "Primary 741 mV, Secondary 741mV", "Primary 741 mV, Secondary 1119mV", "All 269mV", "All 1119mV", ] # write to output file with open(out, 'w', newline='') as output_csv: output_writer = csv.writer(output_csv) output_writer.writerow(output_column_titles) for separation in separations: for channel in channels: # one row of output csv file output_row = [cable_number, separation, channel] data_dir = "{0}/{1}mm_spacing/Link_{2}".format( input_dir, separation, channel) for name in file_names: f = name.format(channel) input_file = "{0}/{1}".format(data_dir, f) # check that file exists if not os.path.exists(input_file): print("ERROR: Required input file does not exist: {0}". format(input_file)) return area = getArea(input_file) output_row.append(area) output_writer.writerow(output_row)
def plotData(input_file, output_file, plot_dir, title, column_indices, xlim, ylim, drawMean): verbose = False tools.makeDir(plot_dir) data = tools.getData(input_file) output_png = "{0}/{1}.png".format(plot_dir, output_file) output_pdf = "{0}/{1}.pdf".format(plot_dir, output_file) # get default colors prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] # get data from column column_index_1 = column_indices[0] column_index_2 = column_indices[1] data_label = "" x_vals = [] y1_vals = [] y2_vals = [] for i, row in enumerate(data): # skip first row if i < 1: continue # second row has labels elif i == 1: data_label = row[column_index_1] # third row is the beginning of data values else: # WARNING: make sure to convert strings to floats! x = float(i - 1) y1 = float(row[column_index_1]) y2 = float(row[column_index_2]) x_vals.append(x) y1_vals.append(y1) y2_vals.append(y2) if verbose: print("{0}: {1}, {2}".format(i, row[column_index_1], row[column_index_2])) # plot fig, ax = plt.subplots(figsize=(6, 6)) x_array = np.array(x_vals) y1_array = np.array(y1_vals) y2_array = np.array(y2_vals) y1_mean = np.mean(y1_array) y2_mean = np.mean(y2_array) y1_std = np.std(y1_array) y2_std = np.std(y2_array) # extend x to plot mean and std dev x_extended = np.insert(x_array, 0, float(x_array[0] - 1)) x_extended = np.append(x_extended, float(x_array[-1] + 1)) if verbose: print("x = {0}".format(x_array)) print("x_extended = {0}".format(x_extended)) print("y1 = {0}".format(y1_array)) print("y2 = {0}".format(y2_array)) print("y1_mean = {0}".format(y1_mean)) print("y2_mean = {0}".format(y2_mean)) print("y1_std = {0}".format(y1_std)) print("y2_std = {0}".format(y2_std)) ax.set_xlim(xlim) ax.set_ylim(ylim) ax.set_title(title, fontsize=16) ax.set_xlabel("Channel", fontsize=12) ax.set_ylabel(data_label, fontsize=12) p1 = plt.scatter(x_array, y1_array, color=colors[0], label="before lashing") p2 = plt.scatter(x_array, y2_array, color=colors[1], label="after lashing") objects = [p1, p2] if drawMean: p3 = plt.axline((x_extended[0], y1_mean), (x_extended[-1], y1_mean), color=colors[0], linestyle="--", label="mean") p4 = plt.axline((x_extended[0], y2_mean), (x_extended[-1], y2_mean), color=colors[1], linestyle="--", label="mean") p5 = plt.fill_between(x_extended, y1_mean - y1_std, y1_mean + y1_std, color=colors[0], alpha=0.2, label="std dev") p6 = plt.fill_between(x_extended, y2_mean - y2_std, y2_mean + y2_std, color=colors[1], alpha=0.2, label="std dev") objects = [p1, p2, p3, p4, p5, p6] # specify order for legend labels = [o.get_label() for o in objects] plt.legend(objects, labels, loc='upper right', prop={'size': 12}) plt.savefig(output_png) plt.savefig(output_pdf)
def plotData(input_file_1, input_file_2, label_1, label_2, output_file, plot_dir, title, column_index, xlim, ylim, drawMean): verbose = False tools.makeDir(plot_dir) data_1 = tools.getData(input_file_1) data_2 = tools.getData(input_file_2) output_png = "{0}/{1}.png".format(plot_dir, output_file) output_pdf = "{0}/{1}.pdf".format(plot_dir, output_file) # get default colors prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] # get data from column data_label = "" x_vals = [] y1_vals = [] y2_vals = [] # get x, y1, and data label for i, row in enumerate(data_1): # first row has labels if i == 0: data_label = row[column_index] # second row is the beginning of data values else: # WARNING: make sure to convert strings to floats! x = float(row[0]) y1 = float(row[column_index]) x_vals.append(x) y1_vals.append(y1) if verbose: print("{0}: {1}".format(i, row[column_index])) # get y2 for i, row in enumerate(data_2): # second row is the beginning of data values if i > 0: # WARNING: make sure to convert strings to floats! y2 = float(row[column_index]) y2_vals.append(y2) if verbose: print("{0}: {1}".format(i, row[column_index])) # plot fig, ax = plt.subplots(figsize=(6, 6)) x_array = np.array(x_vals) y1_array = np.array(y1_vals) y2_array = np.array(y2_vals) y1_mean = np.mean(y1_array) y2_mean = np.mean(y2_array) y1_std = np.std(y1_array) y2_std = np.std(y2_array) # take ratio of means r21 = y2_mean / y1_mean # get error of ratio (assuming independent variables without correlation) # q = x / y # getMultiplicationError(q, x, dx, y, dy) r21_err = tools.getMultiplicationError(r21, y2_mean, y2_std, y1_mean, y1_std) # extend x to plot mean and std dev x_extended = np.insert(x_array, 0, float(x_array[0] - 1)) x_extended = np.append(x_extended, float(x_array[-1] + 1)) if verbose: print("x = {0}".format(x_array)) print("x_extended = {0}".format(x_extended)) print("y1 = {0}".format(y1_array)) print("y2 = {0}".format(y2_array)) print("y1_mean = {0}".format(y1_mean)) print("y2_mean = {0}".format(y2_mean)) print("y1_std = {0}".format(y1_std)) print("y2_std = {0}".format(y2_std)) print("r21 = {0}".format(r21)) print("r21_err = {0}".format(r21_err)) ax.set_xlim(xlim) ax.set_ylim(ylim) ax.set_title(title, fontsize=16) ax.set_xlabel("Channel", fontsize=12) ax.set_ylabel(data_label, fontsize=12) p1 = plt.scatter(x_array, y1_array, color=colors[0], label=label_1) p2 = plt.scatter(x_array, y2_array, color=colors[1], label=label_2) objects = [p1, p2] if drawMean: p3 = plt.axline((x_extended[0], y1_mean), (x_extended[-1], y1_mean), color=colors[0], linestyle="--", label="mean") p4 = plt.axline((x_extended[0], y2_mean), (x_extended[-1], y2_mean), color=colors[1], linestyle="--", label="mean") p5 = plt.fill_between(x_extended, y1_mean - y1_std, y1_mean + y1_std, color=colors[0], alpha=0.2, label="std dev") p6 = plt.fill_between(x_extended, y2_mean - y2_std, y2_mean + y2_std, color=colors[1], alpha=0.2, label="std dev") objects = [p1, p2, p3, p4, p5, p6] # text text_x = xlim[0] + 0.1 * (xlim[1] - xlim[0]) text_y1 = ylim[0] + 0.9 * (ylim[1] - ylim[0]) text_y2 = ylim[0] + 0.8 * (ylim[1] - ylim[0]) text_y3 = ylim[0] + 0.7 * (ylim[1] - ylim[0]) equation1 = r"$\mu_1 = {0:.2f} \pm {1:.2f}$".format(y1_mean, y1_std) equation2 = r"$\mu_2 = {0:.2f} \pm {1:.2f}$".format(y2_mean, y2_std) # use double curly brackets {{21}} so that this is not used as an index by format equation3 = r"$r_{{21}} = {0:.2f} \pm {1:.2f}$".format(r21, r21_err) ax.text(text_x, text_y1, equation1, fontsize=15) ax.text(text_x, text_y2, equation2, fontsize=15) ax.text(text_x, text_y3, equation3, fontsize=15) # specify order for legend labels = [o.get_label() for o in objects] plt.legend(objects, labels, loc='upper right', prop={'size': 12}) plt.savefig(output_png) plt.savefig(output_pdf)
# Save segment matData = { 'data': data, 'channels': channels, 'freq': fs, } if segType == 'ictal': matData['latency'] = i sio.savemat( 'pipeline-data/seizure-data/{0}/{0}_{1}_segment_{2}.mat'.format( ptName, segType, i + 1 + segTotal - nanSkips), matData) # Go to the next segment pos += fs return clipSegs - nanSkips if __name__ == '__main__': clipRoot = sys.argv[1].rstrip('/') ptName = sys.argv[2] segType = sys.argv[3] fs = int(sys.argv[4]) clipDir = os.path.join(clipRoot, ptName) makeDir(clipDir) makeDir('pipeline-data/seizure-data/' + ptName) sliceClips(clipDir, segType, fs, ptName)
def makePlots(input_file, plot_dir): tools.makeDir(plot_dir) data_map = readData(input_file) for cable_number in data_map: print("Plotting Cable {0}".format(cable_number)) cable_dir = "{0}/Cable_{1}".format(plot_dir, cable_number) tools.makeDir(cable_dir) for dataset in datasets: separation_input_values = {} for separation in separations: stats_map = getStats(data_map, cable_number, separation) separation_input_values[separation] = {} separation_input_values[separation]["x_values"] = amplitudes separation_input_values[separation]["y_values"] = stats_map[dataset]["averages"] separation_input_values[separation]["y_errors"] = stats_map[dataset]["std_devs"] channel_input_values = {} for channel in channels: #print("{0}: {1}".format(channel, # data_map[cable_number][separation][channel][dataset], # ) #) channel_input_values[channel] = {} channel_input_values[channel]["x_values"] = amplitudes channel_input_values[channel]["y_values"] = data_map[cable_number][separation][channel][dataset] channel_input_values[channel]["y_errors"] = np.zeros(len(amplitudes)) # Vary channels # No error bars title = "Cable {0} Areas ({1}mm spacing)".format(cable_number, separation) x_label = x_axis_labels[dataset] y_label = "Area" labels = [x_label, y_label] label_format = "Link {0}" xlim = [0.0, 1.5e3] ylim = [0.0, 1.0e5] limits = [xlim, ylim] output_name = "{0}/vary_channel_areas_{1}mm_spacing_{2}".format(cable_dir, separation, dataset) doRatio = False plot(channel_input_values, channels, limits, title, labels, label_format, output_name, doRatio) # Vary separations # For each separation, plot average over channels # The std dev over channels is used for error bars title = "Cable {0} Ave. Areas".format(cable_number) x_label = x_axis_labels[dataset] y_label = "Ave. Area" labels = [x_label, y_label] label_format = "{0} mm spacing" xlim = [0.0, 1.5e3] ylim = [0.0, 1.0e5] limits = [xlim, ylim] output_name = "{0}/vary_separation_areas_{1}".format(cable_dir, dataset) doRatio = False plot(separation_input_values, separations, limits, title, labels, label_format, output_name, doRatio) title = "Cable {0} Ratios of Ave. Areas".format(cable_number) x_label = x_axis_labels[dataset] y_label = "Ratio of Ave. Areas" labels = [x_label, y_label] label_format = "{0} mm spacing" xlim = [0.0, 1.5e3] ylim = [0.0, 2.0] limits = [xlim, ylim] output_name = "{0}/vary_separation_ratios_{1}".format(cable_dir, dataset) doRatio = True plot(separation_input_values, separations, limits, title, labels, label_format, output_name, doRatio)
def plotData(input_file, output_file, plot_dir, title, x_column_index, y_column_index, ylim, drawMean, drawFit): verbose = False tools.makeDir(plot_dir) data = tools.getData(input_file) output_png = "{0}/{1}.png".format(plot_dir, output_file) output_pdf = "{0}/{1}.pdf".format(plot_dir, output_file) # get default colors prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] # get data from column x_label = "" y_label = "" x_vals = [] y_vals = [] # loop over data for i, row in enumerate(data): # first row has labels if i == 0: x_label = row[x_column_index] y_label = row[y_column_index] # second row is the beginning of data values else: # WARNING: make sure to convert strings to floats! x = float(row[x_column_index]) y = float(row[y_column_index]) x_vals.append(x) y_vals.append(y) if verbose: print("{0}: {1}".format(i, row[y_column_index])) # plot fig, ax = plt.subplots(figsize=(6, 6)) x_array = np.array(x_vals) y_array = np.array(y_vals) y_mean = np.mean(y_array) y_std = np.std(y_array) # extend x to define xlim and plot mean and std dev step = x_array[1] - x_array[0] x_extended = np.insert(x_array, 0, float(x_array[0] - step)) x_extended = np.append(x_extended, float(x_array[-1] + step)) xlim = [x_extended[0], x_extended[-1]] if verbose: print("x = {0}".format(x_array)) print("x_extended = {0}".format(x_extended)) print("xlim = {0}".format(xlim)) print("y = {0}".format(y_array)) print("y_mean = {0}".format(y_mean)) print("y_std = {0}".format(y_std)) # format plot ax.set_xlim(xlim) ax.set_ylim(ylim) ax.set_title(title, fontsize=16) ax.set_xlabel(x_label, fontsize=12) ax.set_ylabel(y_label, fontsize=12) p1 = plt.scatter(x_array, y_array, color=colors[0], label="data") objects = [p1] # draw mean and std dev on plot if drawMean: p2 = plt.axline((x_extended[0], y_mean), (x_extended[-1], y_mean), color=colors[0], linestyle="--", label="mean") p3 = plt.fill_between(x_extended, y_mean - y_std, y_mean + y_std, color=colors[0], alpha=0.2, label="std dev") objects = [p1, p2, p3] # text text_x = xlim[0] + 0.1 * (xlim[1] - xlim[0]) text_y = ylim[0] + 0.9 * (ylim[1] - ylim[0]) equation = r"$\mu = {0:.2f} \pm {1:.2f}$".format(y_mean, y_std) ax.text(text_x, text_y, equation, fontsize=15) # draw fit on plot if drawFit: model = np.polyfit(x_array, y_array, 1) predict = np.poly1d(model) #print(predict) p4 = plt.plot(x_array, predict(x_array), label='fit', color='red', linestyle='dashed') #objects = [p1, p4] # text text_x = xlim[0] + 0.1 * (xlim[1] - xlim[0]) text_y = ylim[0] + 0.9 * (ylim[1] - ylim[0]) f_x = "{0}".format(predict) # remove whitespace and newlines f_x = f_x.strip() equation = r"y = {0}".format(f_x) ax.text(text_x, text_y, equation, fontsize=15) # specify order for legend labels = [o.get_label() for o in objects] plt.legend(objects, labels, loc='upper right', prop={'size': 12}) plt.savefig(output_png) plt.savefig(output_pdf)