Skip to content

Commit 123d8ee

Browse files
committed
Added option to generate greyscale plots
1 parent d772773 commit 123d8ee

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

structure_threader/plotter/structplot.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -797,9 +797,13 @@ def plotk(self, kvals, output_dir):
797797
with open(filepath, "w") as fh:
798798
fh.write(ploty_html(pdiv))
799799

800-
def plotk_static(self, kval, output_dir):
800+
def plotk_static(self, kval, output_dir, bw=False):
801801
"""
802-
802+
Generates a structure plot in svg format.
803+
:param kval: (int) Must match the K value from self.kvals
804+
:param output_dir: (string) Path of the plot file
805+
:param bw: If True, plots will be generated with patterns instead of
806+
colors to distinguish k groups.
803807
"""
804808

805809
qvalues = self.kvals[kval].qvals
@@ -817,19 +821,36 @@ def plotk_static(self, kval, output_dir):
817821
axe = fig.add_subplot(111, xlim=(-.5, numinds - .5), ylim=(0, 1))
818822

819823
for i in range(qvalues.shape[1]):
820-
# Get bar color. If K exceeds the 12 colors, generate random color
821-
try:
822-
clr = clist[i]
823-
except IndexError:
824-
clr = np.random.rand(3, 1)
824+
825+
# Determine color/pattern arguments
826+
kwargs = {}
827+
828+
# Use colors todistinguish k groups
829+
if not bw:
830+
# Get bar color. If K exceeds the 12 colors, generate random
831+
# color
832+
try:
833+
clr = clist[i]
834+
except IndexError:
835+
clr = np.random.rand(3, 1)
836+
837+
kwargs["facecolor"] = clr
838+
kwargs["edgecolor"] = "grey"
839+
840+
else:
841+
grey_rgb = [(float((i + 1)) / (float(qvalues.shape[1]) + 1))
842+
for _ in range(3)]
843+
844+
kwargs["facecolor"] = grey_rgb
845+
kwargs["edgecolor"] = "white"
825846

826847
if i == 0:
827-
axe.bar(range(numinds), qvalues[:, i], facecolor=clr,
828-
edgecolor="grey", width=1)
848+
axe.bar(range(numinds), qvalues[:, i],
849+
width=1, label="K {}".format(i + 1), **kwargs)
829850
former_q = qvalues[:, i]
830851
else:
831852
axe.bar(range(numinds), qvalues[:, i], bottom=former_q,
832-
facecolor=clr, edgecolor="grey", width=1)
853+
width=1, label="K {}".format(i + 1), **kwargs)
833854
former_q = former_q + qvalues[:, i]
834855

835856
# Annotate population info
@@ -860,15 +881,23 @@ def plotk_static(self, kval, output_dir):
860881
plt.yticks([])
861882
plt.xticks([])
862883

884+
# Add k legend
885+
legend = plt.legend(bbox_to_anchor=(1.2, .5), loc=7, borderaxespad=0.)
886+
legend.get_frame().set_facecolor("white")
887+
863888
kfile = self.kvals[kval].file_path
864889
filename = splitext(basename(kfile))[0]
865890
filepath = join(output_dir, filename)
866891

867892
plt.savefig("{}.svg".format(filepath), bbox_inches="tight")
868893

894+
# Clear plot object
895+
plt.clf()
896+
plt.close()
897+
869898

870899
def main(result_files, fmt, outdir, bestk=None, popfile=None, indfile=None,
871-
filter_k=None):
900+
filter_k=None, bw=False):
872901
"""
873902
Wrapper function that generates one plot for each K value.
874903
:return:
@@ -890,7 +919,7 @@ def main(result_files, fmt, outdir, bestk=None, popfile=None, indfile=None,
890919

891920
if k >= 1 and k in filter_k:
892921
klist.plotk([k], outdir)
893-
klist.plotk_static(k, outdir)
922+
klist.plotk_static(k, outdir, bw=bw)
894923

895924
# If a sequence of multiple bestk is provided, plot all files in a single
896925
# plot

structure_threader/structure_threader.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def create_plts(resultsdir, wrapped_prog, Ks, bestk, arg):
254254
for i in plt_list]
255255

256256
sp.main(plt_files, wrapped_prog, outdir, bestk=bestk, popfile=arg.popfile,
257-
indfile=arg.indfile)
257+
indfile=arg.indfile, bw=arg.blacknwhite)
258258

259259

260260
def maverick_merger(outdir, k_list, no_tests):
@@ -424,11 +424,16 @@ def argument_parser(args):
424424
help="Override 'K' values from the given list"
425425
" to be ploteted in the combined figure.",
426426
metavar="'2 4 5'", default=None)
427+
plot_opts.add_argument("-bw", dest="blacknwhite",
428+
action="store_const", const=True,
429+
help="Set this flag to draw greyscale plots"
430+
" instead of colored ones.")
427431

428432
# ####################### PLOT ARGUMENTS ##################################
429433
# Group definitions
430434

431435
main_opts = plot_parser.add_argument_group("Main plotting options")
436+
extra_opts = plot_parser.add_argument_group("Extra plotting options")
432437
sort_opts = plot_parser.add_argument_group("Plot sorting options")
433438

434439
# Group options
@@ -454,6 +459,11 @@ def argument_parser(args):
454459
" the current working directory"
455460
" will be used.")
456461

462+
extra_opts.add_argument("-bw", dest="blacknwhite",
463+
action="store_const", const=True,
464+
help="Set this flag to draw greyscale plots"
465+
" instead of colored ones.")
466+
457467
sort_opts_ex.add_argument("--pop", dest="popfile", type=str,
458468
required=False,
459469
help="File with population information.",
@@ -597,7 +607,7 @@ def main():
597607
bestk = [int(x) for x in arg.bestk]
598608

599609
sp.main(infiles, arg.format, arg.outpath, bestk, popfile=arg.popfile,
600-
indfile=arg.indfile, filter_k=bestk)
610+
indfile=arg.indfile, filter_k=bestk, bw=arg.blacknwhite)
601611

602612

603613
if __name__ == "__main__":

0 commit comments

Comments
 (0)