Extract Key Information from Raw Experiment for Running in the Notebook - Glasgow-ICG/optical-gating-alignment GitHub Wiki
Given a complete experiment, the following code will collect the reference periods and a periods.csv file together, as required for the example notebook.
Thanks to Jonny Taylor for this nice compact way of doing this.
# Import
import glob
import shutil
import csv
import os
# Set-up
experiment_folder = "<<REPLACE>>"
number_stacks = len([o for o in os.listdir(experiment_folder)
if o.startswith('Stack')])
# Make Folders
try:
os.rmdir("local-data")
os.mkdir("local-data")
except:
os.makedirs("local-data", exist_ok=True)
# periods.csv
csv_out = open(os.path.join("local-data","periods.csv"), 'w')
csv_out.write("Stack, Period, DriftX, DriftY, Live\n")
# Collate period information
for stack in range(1, number_stacks+1):
period_path = glob.glob(os.path.join(experiment_folder,"Stack {0:04d}/ref*/period.txt".format(stack)))[-1]
with open(period_path, 'r') as period_path:
reader = csv.reader(period_path, delimiter=' ')
for row in reader:
# row = (stack, period, driftX, driftY, prospective)
csv_out.write("{0:04d}, {1}, {2}, {3}, {4}\n".format(stack, row[0], row[1], row[2], row[3]))
csv_out.close()
# Copy Period Files
for stack in range(1, number_stacks+1):
r = glob.glob(os.path.join(experiment_folder,"Stack {0:04d}/ref*/*.tif".format(stack)))[-1]
shutil.copyfile(r, "local-data/reference{0:04d}.tif".format(stack))