Skip to content

Instantly share code, notes, and snippets.

@a-y-khan
Last active June 21, 2017 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-y-khan/fd2333e1bdbf5fbf8007c701728835da to your computer and use it in GitHub Desktop.
Save a-y-khan/fd2333e1bdbf5fbf8007c701728835da to your computer and use it in GitHub Desktop.
Extract properties from JSON file and write to CSV
import math
import sys
import ijson
import csv
import argparse
def extract_to_csv(json_filename, new_json_filename, csv_filename):
# ijson's parser chokes on NaNs, so let's get rid of them
# NaN containing fields not needed for Adam's analysis?
# if Circularity and Pixel area are NaN, we're in trouble!!!
json_file = open(json_filename, "r")
with open(new_json_filename, "w") as new_json_file:
for line in json_file:
if "NaN" in line:
line = line.replace("NaN", "0")
#print(line)
new_json_file.write(line)
columns = []
with open(new_json_filename, "rb") as input_file:
# load json iteratively
parser = ijson.parse(input_file)
particle_property_column = []
for prefix, event, value in parser:
# structured, so Circularity always before Pixel area...
if "Circularity" in prefix or "circularity" in prefix:
particle_property_column.append(prefix)
particle_property_column.append(value)
elif "Pixel area (um^2)" in prefix:
particle_property_column.append(prefix)
particle_property_column.append(value)
columns.append(particle_property_column)
particle_property_column = []
with open(csv_filename, "w+") as output_file:
csv_file = csv.writer(output_file)
for c in columns:
csv_file.writerow(c)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract relevent data from aggregate.py JSON output and write to CSV file. A second JSON file is output where NaNs are converted to 0 (JSON_mod).")
parser.add_argument("JSON")
parser.add_argument("JSON_mod")
parser.add_argument("CSV")
args = parser.parse_args()
extract_to_csv(args.JSON, args.JSON_mod, args.CSV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment