Last active
March 26, 2018 14:37
-
-
Save afrendeiro/d9956c1fb2c51ceacc0c126ea79e21c4 to your computer and use it in GitHub Desktop.
Parse raw data out of CSV files exported from a Thrombin Generation Assay (TGA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
from argparse import ArgumentParser | |
# Parse command-line arguments | |
parser = ArgumentParser( | |
prog="CSV parser", | |
description="Gets some column out of CSVs." | |
) | |
parser.add_argument( | |
dest="root_dir", | |
help="Directory to be crawled for CSV files.", | |
type=str) | |
parser.add_argument( | |
"-o", "--output", | |
dest="output_file", | |
help="Output file.", | |
default="processed_data.csv", | |
type=str) | |
args = parser.parse_args() | |
# Process directory | |
print("Processing directory '{}'.".format(args.root_dir)) | |
res = pd.DataFrame() | |
times = dict() | |
for file in os.listdir(os.path.abspath(args.root_dir)): | |
if os.path.isdir(file) or ("CSV" not in file.upper()) or (file == args.output_file): | |
continue | |
print("Processing file '{}'.".format(os.path.join(args.root_dir, file))) | |
header = pd.read_csv( | |
os.path.join(args.root_dir, file), | |
nrows=12, encoding="latin1", sep=";", decimal=",", index_col=0) | |
data = pd.read_csv( | |
os.path.join(args.root_dir, file), | |
skiprows=21, encoding="latin1", sep=";", decimal=",") | |
name = header.loc["Sample", "Data"] + " " + header.loc["Date", "Data"] | |
times[name] = header.loc["Time", "Data"] | |
res[name] = data['Raw[cnt]'] | |
order = pd.Series(times).sort_values() | |
res[order.index].to_csv(os.path.join(args.root_dir, args.output_file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment