Created
April 19, 2017 20:42
-
-
Save peisenhower/fa0db7eb3512f8d3eefd9d8cfa93040c to your computer and use it in GitHub Desktop.
Matplotlib plotter for json files generated by jack
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 matplotlib.pyplot as plt | |
from numpy import loadtxt | |
import numpy as np | |
import pprint | |
import argparse | |
import json | |
from collections import namedtuple | |
pp = pprint.PrettyPrinter(indent=4) | |
def get_options(): | |
parser = argparse.ArgumentParser(description='View Results') | |
parser.add_argument('files', type=str, nargs='+', | |
help='files to be viewed in json format') | |
parser.add_argument('--output-only', '-x', action='store_true', | |
help='only generate plots, to not open viewer', default=False) | |
parser.add_argument('--well', '-w', action='store_true', | |
help='generate well plots', default=False) | |
parser.add_argument('--color', '-c', action='store_true', | |
help='generate color plots', default=False) | |
return parser.parse_args() | |
def load_data(path): | |
all_samples = None | |
with open(path, 'r') as content_file: | |
content = content_file.read() | |
all_samples = json.loads(content, object_hook=lambda d: namedtuple('sample', d.keys())(*d.values())) | |
return all_samples | |
def plot_all_wells(data, save_name=None): | |
all_cycles = list(map(lambda x: x.cycle, data)) | |
f, ax = build_configure_plot('All Wells All Colors', all_cycles) | |
# Extract only color, reduce to distinct set | |
colors = set(map(lambda x: x.color, data)) | |
wells = set(map(lambda x: x.well, data)) | |
for w in wells: | |
for c in colors: | |
# get data set for specific well and color | |
samples = list(filter(lambda x: x.color == c and x.well == w, data)) | |
samples = sorted(samples, key=lambda x: x.cycle) | |
value = list(map(lambda x: x.value, samples)) | |
cycle = list(map(lambda x: x.cycle, samples)) | |
plt.plot(cycle, value) | |
if save_name is not None: | |
f.saveFigure(save_name) | |
def plot_well_all_colors(well, data, save_name=None): | |
all_cycles = list(map(lambda x: x.cycle, data)) | |
title = 'Well {0} All Colors'.format(well) | |
f, ax = build_configure_plot(title, all_cycles) | |
# Extract only color, reduce to distinct set | |
colors = set(map(lambda x: x.color, data)) | |
for c in colors: | |
# get data set for specific well and color | |
samples = list(filter(lambda x: x.color == c and x.well == well, data)) | |
samples = sorted(samples, key=lambda x: x.cycle) | |
value = list(map(lambda x: x.value, samples)) | |
cycle = list(map(lambda x: x.cycle, samples)) | |
plt.plot(cycle, value) | |
if save_name is not None: | |
f.saveFigure(save_name) | |
def plot_color_all_wells(color, data, save_name=None): | |
all_cycles = list(map(lambda x: x.cycle, data)) | |
title = 'Color {0} All wells'.format(color.upper()) | |
f, ax = build_configure_plot(title, all_cycles) | |
wells = set(map(lambda x: x.well, data)) | |
for w in wells: | |
# get data set for specific well and color | |
samples = list(filter(lambda x: x.color == color and x.well == w, data)) | |
samples = sorted(samples, key=lambda x: x.cycle) | |
value = list(map(lambda x: x.value, samples)) | |
cycle = list(map(lambda x: x.cycle, samples)) | |
plt.plot(cycle, value) | |
if save_name is not None: | |
f.saveFigure(save_name) | |
def plot_per_color(data, save_name=None): | |
for c in set(map(lambda x: x.color, data)): | |
plot_color_all_wells(c, data, save_name) | |
def plot_per_well(data, save_name=None): | |
for w in set(map(lambda x: x.well, data)): | |
plot_well_all_colors(w, data, save_name) | |
def build_configure_plot(title, cycles=[]): | |
f = plt.figure() | |
ax = f.add_subplot(1,1,1) | |
f.suptitle(title, fontsize=14, fontweight='bold') | |
plt.xlabel('Cycle') | |
plt.ylabel('Intensity') | |
ax.grid(which='both') | |
ax.grid(which='minor', alpha=0.2) | |
ax.grid(which='major', alpha=0.5) | |
if len(cycles) > 1: | |
plt.xticks(np.arange(min(cycles), max(cycles)+1, 5.0)) | |
ax.set_xticks(np.arange(min(cycles), max(cycles)+1, 1.0), minor=True) | |
return f, ax | |
if __name__ == "__main__": | |
options = get_options() | |
for file in options.files: | |
data = load_data(file) | |
plot_all_wells(data) | |
if options.well: | |
plot_per_well(data) | |
if options.color: | |
plot_per_color(data) | |
if not options.output_only: | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment