python - Not saving all matplotlib graphs from script output? -
i have script run in ipython, , takes input .csv file of gene_names
, pushes them loop, where
with open('c:\users\work\desktop\book1.csv', 'ru') f: reader = csv.reader(f) pdfpages('poopyheadjoe04.pdf') pdf: row in reader: gene_name = row probe_exclusion_keyword = [] print gene_name
the gene_name values list(in .csv file) fed line, if inference_method == "approximate_random"
: (in scripts.py)
with open('c:\users\work\desktop\book1.csv', 'ru') f: reader = csv.reader(f) pdfpages('poopyheadjoe04.pdf') pdf: row in reader: gene_name = row probe_exclusion_keyword = [] print gene_name print "fetching probe ids gene %s" % gene_name probes_dict = get_probes_from_genes(gene_name) print "found %s probes: %s" % (len(probes_dict), ", ".join(probes_dict.values())) if probe_exclusion_keyword: probes_dict = {probe_id: probe_name (probe_id, probe_name) in probes_dict.iteritems() if not args.probe_exclusion_keyword in probe_name} print "probes after applying exclusion cryterion: %s" % (", ".join(probes_dict.values())) print "fetching expression values probes %s" % (", ".join(probes_dict.values())) expression_values, well_ids, donor_names = get_expression_values_from_probe_ids( probes_dict.keys()) print "found data %s wells sampled across %s donors" % (len(well_ids), len(set(donor_names))) print "combining information selected probes" combined_expression_values = combine_expression_values( expression_values, method=probes_reduction_method) print "translating locations of wells mni space" mni_coordinates = get_mni_coordinates_from_wells(well_ids) print "checking values of provided nifti file @ locations" nifti_values = get_values_at_locations( stat_map, mni_coordinates, mask_file=mask, radius=radius, verbose=true) # preparing data frame names = ["nifti values", "%s expression" % gene_name, "donor id"] data = pd.dataframe(np.array( [nifti_values, combined_expression_values, donor_names]).t, columns=names) data = data.convert_objects(convert_numeric=true) len_before = len(data) data.dropna(axis=0, inplace=true) nans = len_before - len(data) if nans > 0: print "%s wells fall outside of mask" % nans if inference_method == "fixed": print "performing fixed effect analysis" fixed_effects(data, ["nifti values", "%s expression" % gene_name]) **if inference_method == "approximate_random":** print "performing approximate random effect analysis" approximate_random_effects( data, ["nifti values", "%s expression" % gene_name], "donor id") print "poopy" pdf.savefig() plt.ion() #should add ion() here? if inference_method == "bayesian_random": print "fitting bayesian hierarchical model" bayesian_random_effects( data, ["nifti values", "%s expression" % gene_name], "donor id", n_samples, n_burnin) # if __name__ == '__main__': #what do? start trigger script run? # main()
that triggers approximate_random_effects
(in analysis.py) plot 2 graphs, violinplot , lmplot:
def approximate_random_effects(data, labels, group): correlation_per_donor = {} donor_id in set(data[group]): correlation_per_donor[donor_id], _, _, _, _ = linregress(list(data[labels[0]][data[group] == donor_id]), list(data[labels[1]][data[group] == donor_id])) average_slope = np.array(correlation_per_donor.values()).mean() t, p_val = ttest_1samp(correlation_per_donor.values(), 0) print "averaged slope across donors = %g (t=%g, p=%g)"%(average_slope, t, p_val) sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"]) plt.ylabel("linear regression slopes between %s , %s"%(labels[0],labels[1])) plt.axhline(0, color="red") sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3) plt.ion() return average_slope, t, p_val
i'm trying save both graphs gene_names pdf file, following "saving multiple figures 1 pdf file in matplotlib" , matplotlib.pdfpages
approach.
however, in pdf file, getting lmplot gene_names , not violin plot. , interestingly, adding print def approximate_random_effects
(of analysis.py) not reflect output. do fix this?
thanks! appreciated!
you need create 2 different axes plots, 1 violin plot, , 1 imshow plot. @ top of approximate_random_effects function add
fig, (ax_left, ax_right) = plt.subplots(1,2, figsize=(6,3)) # change figsize size want, default ugly 2 plots
or
fig, (ax_top, ax_bottom) = plt.subplots(2, figsize=(3, 6)) # change figsize size want, default ugly 2 plots
depending on if want top , bottom or side side. rest of answer, assume side side.
now need change plotting calls adding ax=ax_left , ax=ax_right
sns.violinplot([correlation_per_donor.values()], inner="points", names=["donors"], ax=ax_left)
and
sns.lmplot(labels[0], labels[1], data, hue=group, col=group, col_wrap=3, ax=ax_right)
Comments
Post a Comment