Created
January 7, 2025 08:00
-
-
Save matham/b08038efbce10332e6e7dff6c9deb07d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from pathlib import Path | |
import csv | |
import numpy as np | |
root = Path(r"...") | |
odors = {"CH4", "NH3", "TBP", "TEP", "Air", "Humidity"} | |
odors = list(sorted(odors)) | |
confusents = {"None", "Diesel", "Gasoline", "Roundup", "Tobacco", "Smoke", "Vanilla"} | |
confusents = list(sorted(confusents)) | |
def get_item(name): | |
odor = "Air" | |
confusent = "None" | |
for o in odors: | |
if o.lower() in name.lower(): | |
odor = o | |
break | |
for c in confusents: | |
if c.lower() in name.lower(): | |
confusent = c | |
break | |
return odor, confusent | |
toc = [("Filename", "Record ID", "Run ID", "Device ID", "Analyte", "Confusant", "Trial")] | |
counts = np.zeros((len(odors), len(confusents))) | |
files = list(sorted(root.glob("*.csv"), key=lambda x: int(x.name.split(" - ")[0]))) | |
for file in files: | |
with open(file, "r") as fh: | |
reader = csv.reader(fh, delimiter=",") | |
next(reader) | |
line = next(reader) | |
name = line[6] | |
sensor = line[5] | |
odor, confusent = get_item(name) | |
counts[odors.index(odor), confusents.index(confusent)] += 1 | |
with open(file, "r") as fh: | |
contents = fh.read() | |
contents = contents.replace(",item,", ",Analyte,Confusant,") | |
contents = contents.replace(f",{name},", f",{odor},{confusent},") | |
print(f"{name} -> {odor} / {confusent}") | |
rec, run, *_, trial = file.name.split(" - ") | |
rec = int(rec) | |
run = run[4:] | |
trial = int(trial.split(" ")[1]) | |
new_name = f"item_{rec:03}_run_{run}_trial_{trial}.csv" | |
with open(file.parent / new_name, "w") as fh: | |
fh.write(contents) | |
toc.append((new_name, str(rec), run, sensor, odor, confusent, str(trial))) | |
np.savetxt(root / "summary.csv", counts, delimiter=",", fmt="%d") | |
with open(root / "table_of_contents.csv", "w", newline='') as fh: | |
writer = csv.writer(fh, delimiter=",") | |
writer.writerows(toc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment