def test_to_dict(self): """ The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report("redfang") a = Attribute("a", "b") a2 = Attribute("a2", "b2") r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup("pgid") pg.add_plot(Plot("pid", "anImg")) pg.add_plot(Plot("pid2", "anImg2")) r.add_plotgroup(pg) t = Table("tabid") t.add_column(Column("c1")) r.add_table(t) d = r.to_dict() log.debug("\n" + pformat(d)) self.assertEqual("redfang", d["id"]) self.assertEqual("redfang.a", d["attributes"][0]["id"]) self.assertEqual("redfang.a2", d["attributes"][1]["id"]) self.assertEqual("redfang.pgid", d["plotGroups"][0]["id"]) self.assertEqual("redfang.pgid.pid", d["plotGroups"][0]["plots"][0]["id"]) self.assertEqual("redfang.pgid.pid2", d["plotGroups"][0]["plots"][1]["id"]) self.assertEqual("redfang.tabid", d["tables"][0]["id"]) self.assertEqual("redfang.tabid.c1", d["tables"][0]["columns"][0]["id"])
def test_to_dict(self): """ The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) d = r.to_dict() log.debug("\n" + pformat(d)) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d['plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d['plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][0]['columns'][0]['id'])
def test_get_plotgroup_by_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) pg = r.get_plotgroup_by_id('pgid1') self.assertEqual(pg, pg1)
def test_get_plotgroup_by_id_with_bad_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) bad_pg = r.get_plotgroup_by_id('id_that_does_not_exist') self.assertIsNone(bad_pg)
def test_get_plotgroup_by_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) pg = r.get_plotgroup_by_id('pgid1') self.assertEqual(pg, pg1)
def test_get_plotgroup_by_id_with_bad_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') pg1.add_plot(Plot('pid1', 'anImg')) r.add_plotgroup(pg1) bad_pg = r.get_plotgroup_by_id('id_that_does_not_exist') self.assertIsNone(bad_pg)
def test_get_plot_by_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') p1 = Plot('pid1', 'anImg') pg1.add_plot(p1) r.add_plotgroup(pg1) p = r.get_plotgroup_by_id('pgid1').get_plot_by_id('pid1') assert p == p1
def test_get_plot_by_id_with_bad_id(self): r = Report('redfang') pg1 = PlotGroup('pgid1') p1 = Plot('pid1', 'anImg') pg1.add_plot(p1) r.add_plotgroup(pg1) bad_p = r.get_plotgroup_by_id('pgid1').get_plot_by_id( 'id_that_does_not_exist') assert bad_p is None
def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ tags = ["alpha", "beta", "gamma"] r = Report('redfang', tags=tags) a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) pg = PlotGroup('pgid2') pg.add_plot(Plot('pid2', 'anImg2')) pg.add_plot(Plot('pid22', 'anImg22')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) t = Table('tabid2') t.add_column(Column('c2')) r.add_table(t) d = r.to_dict() log.debug(str(d)) assert 'redfang' == d['id'] assert 'redfang.a' == d['attributes'][0]['id'] assert 'redfang.a2' == d['attributes'][1]['id'] assert 'redfang.pgid' == d['plotGroups'][0]['id'] assert 'redfang.pgid.pid' == d['plotGroups'][0]['plots'][0]['id'] assert 'redfang.pgid.pid2' == d['plotGroups'][0]['plots'][1]['id'] assert 'redfang.pgid2' == d['plotGroups'][1]['id'] assert 'redfang.pgid2.pid2' == d['plotGroups'][1]['plots'][0]['id'] assert 'redfang.pgid2.pid22' == d['plotGroups'][1]['plots'][1]['id'] assert 'redfang.tabid' == d['tables'][0]['id'] assert 'redfang.tabid.c1' == d['tables'][0]['columns'][0]['id'] assert 'redfang.tabid2' == d['tables'][1]['id'] assert 'redfang.tabid2.c2' == d['tables'][1]['columns'][0]['id'] assert list(sorted(d['tags'])) == list(sorted(tags)) loaded_report = load_report_from(d) assert list(sorted(loaded_report.tags)) == list(sorted(tags)) log.info(repr(r)) assert repr(r) is not None
def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) pg = PlotGroup('pgid2') pg.add_plot(Plot('pid2', 'anImg2')) pg.add_plot(Plot('pid22', 'anImg22')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) t = Table('tabid2') t.add_column(Column('c2')) r.add_table(t) d = r.to_dict() log.debug(str(d)) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d['plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d['plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.pgid2', d['plotGroups'][1]['id']) self.assertEqual('redfang.pgid2.pid2', d['plotGroups'][1]['plots'][0]['id']) self.assertEqual('redfang.pgid2.pid22', d['plotGroups'][1]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][0]['columns'][0]['id']) self.assertEqual('redfang.tabid2', d['tables'][1]['id']) self.assertEqual('redfang.tabid2.c2', d['tables'][1]['columns'][0]['id']) log.info(repr(r)) self.assertIsNotNone(repr(r))
def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) pg = PlotGroup('pgid2') pg.add_plot(Plot('pid2', 'anImg2')) pg.add_plot(Plot('pid22', 'anImg22')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) t = Table('tabid2') t.add_column(Column('c2')) r.add_table(t) d = r.to_dict() log.debug(str(d)) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d[ 'plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d[ 'plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.pgid2', d['plotGroups'][1]['id']) self.assertEqual('redfang.pgid2.pid2', d[ 'plotGroups'][1]['plots'][0]['id']) self.assertEqual('redfang.pgid2.pid22', d[ 'plotGroups'][1]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][ 0]['columns'][0]['id']) self.assertEqual('redfang.tabid2', d['tables'][1]['id']) self.assertEqual('redfang.tabid2.c2', d[ 'tables'][1]['columns'][0]['id']) log.info(repr(r)) self.assertIsNotNone(repr(r))
def test_to_dict(self): """Test plotGroup to_dict function.""" a = PlotGroup('123', title='foo title', legend='foo legend', thumbnail='foo thumbnail') a.add_plot(Plot('id', 'i1', caption='a caption')) d = a.to_dict() log.debug(pformat(d)) self.assertEquals('123', d['id']) self.assertEquals('foo title', d['title']) self.assertEquals('foo legend', d['legend']) self.assertEquals('foo thumbnail', d['thumbnail']) self.assertEquals(1, len(d['plots'])) log.info(a) self.assertIsNotNone(repr(a))
def test_to_dict(self): """Test plotGroup to_dict function.""" a = PlotGroup('123', title='foo title', legend='foo legend', thumbnail='foo thumbnail') a.add_plot(Plot('id', 'i1', caption='a caption')) d = a.to_dict() log.debug(pformat(d)) self.assertEquals('123', d['id']) self.assertEquals('foo title', d['title']) self.assertEquals('foo legend', d['legend']) self.assertEquals('foo thumbnail', d['thumbnail']) self.assertEquals(1, len(d['plots'])) log.info(a) self.assertIsNotNone(repr(a))
def test_to_dict(self): """Test plotGroup to_dict function.""" a = PlotGroup('123', title='foo title', legend='foo legend', thumbnail='foo thumbnail') a.add_plot(Plot('id', 'i1', caption='a caption')) d = a.to_dict() log.debug(pformat(d)) assert '123' == d['id'] assert 'foo title' == d['title'] assert 'foo legend' == d['legend'] assert 'foo thumbnail' == d['thumbnail'] assert 1 == len(d['plots']) log.info(a) assert repr(a) is not None
def test_to_dict_multi(self): """ Multiple complex elements. The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report("redfang") a = Attribute("a", "b") a2 = Attribute("a2", "b2") r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup("pgid") pg.add_plot(Plot("pid", "anImg")) pg.add_plot(Plot("pid2", "anImg2")) r.add_plotgroup(pg) pg = PlotGroup("pgid2") pg.add_plot(Plot("pid2", "anImg2")) pg.add_plot(Plot("pid22", "anImg22")) r.add_plotgroup(pg) t = Table("tabid") t.add_column(Column("c1")) r.add_table(t) t = Table("tabid2") t.add_column(Column("c2")) r.add_table(t) d = r.to_dict() log.debug(str(d)) self.assertEqual("redfang", d["id"]) self.assertEqual("redfang.a", d["attributes"][0]["id"]) self.assertEqual("redfang.a2", d["attributes"][1]["id"]) self.assertEqual("redfang.pgid", d["plotGroups"][0]["id"]) self.assertEqual("redfang.pgid.pid", d["plotGroups"][0]["plots"][0]["id"]) self.assertEqual("redfang.pgid.pid2", d["plotGroups"][0]["plots"][1]["id"]) self.assertEqual("redfang.pgid2", d["plotGroups"][1]["id"]) self.assertEqual("redfang.pgid2.pid2", d["plotGroups"][1]["plots"][0]["id"]) self.assertEqual("redfang.pgid2.pid22", d["plotGroups"][1]["plots"][1]["id"]) self.assertEqual("redfang.tabid", d["tables"][0]["id"]) self.assertEqual("redfang.tabid.c1", d["tables"][0]["columns"][0]["id"]) self.assertEqual("redfang.tabid2", d["tables"][1]["id"]) self.assertEqual("redfang.tabid2.c2", d["tables"][1]["columns"][0]["id"]) log.info(repr(r)) self.assertIsNotNone(repr(r))
def test_to_dict(self): """ The id of report sub elements is prepended with the id of the parent element when to_dict is called. """ r = Report('redfang') a = Attribute('a', 'b') a2 = Attribute('a2', 'b2') r.add_attribute(a) r.add_attribute(a2) pg = PlotGroup('pgid') pg.add_plot(Plot('pid', 'anImg')) pg.add_plot(Plot('pid2', 'anImg2')) r.add_plotgroup(pg) t = Table('tabid') t.add_column(Column('c1')) r.add_table(t) d = r.to_dict() log.debug("\n" + pformat(d)) r2 = load_report_from(d) self.assertEqual(r.uuid, r2.uuid) self.assertEqual('redfang', d['id']) self.assertEqual('redfang.a', d['attributes'][0]['id']) self.assertEqual('redfang.a2', d['attributes'][1]['id']) self.assertEqual('redfang.pgid', d['plotGroups'][0]['id']) self.assertEqual('redfang.pgid.pid', d['plotGroups'][0]['plots'][0]['id']) self.assertEqual('redfang.pgid.pid2', d['plotGroups'][0]['plots'][1]['id']) self.assertEqual('redfang.tabid', d['tables'][0]['id']) self.assertEqual('redfang.tabid.c1', d['tables'][0]['columns'][0]['id'])
def to_report(self, output_dir, report_id=Constants.R_ID): """ This needs to be cleaned up. Keeping the old interface for testing purposes. """ started_at = time.time() log.info("Found {n} movies.".format(n=len(self.movies))) log.info("Working from {n} alignment file{s}: {f}".format( n=len(self.alignment_file_list), s='s' if len(self.alignment_file_list) > 1 else '', f=self.alignment_file_list)) # make this a dict {attribute_key_name:Aggreggator} so it's easy to # access the instances after they've been computed. # there's duplicated keys in the attributes? # number_of_aligned_reads/mapped_reads_n _total_aggregators = self._get_total_aggregators() null_filter = lambda r: True total_model = StatisticsModel( _total_aggregators.values(), filter_func=null_filter) # need to create specific instances for a given movie. This is used to # create the mapping reports stats table movie_models = {} def _my_filter(movie_name1, movie_name2): return movie_name1 == movie_name2 for movie in self.movies: ags = [k() for k in self.COLUMN_AGGREGATOR_CLASSES] # Note this WILL NOT work because of how scope works in python # filter_by_movie_func = lambda m_name: movie.name == m_name _my_filter_func = functools.partial(_my_filter, movie) model = StatisticsModel(ags, filter_func=_my_filter_func) movie_models[movie] = model # The statistic models that will be run all_models = [total_model] + movie_models.values() log.debug(all_models) # Run all the analysis. Now the aggregators can be accessed analyze_movies(self.movies, self.alignment_file_list, all_models) # temp structure used to create the report table. The order is # important # add total values _to_a = lambda k: _total_aggregators[k].attribute _row = [_to_a(n) for n in self.COLUMN_ATTR] _row.insert(0, 'All Movies') movie_datum = [_row] # Add each individual movie stats for movie_name_, model_ in movie_models.iteritems(): _row = [movie_name_] for a in model_.aggregators: _row.append(a.attribute) movie_datum.append(_row) log.info(movie_datum) # create the Report table table = self._to_table(movie_datum) for movie_name, model in movie_models.iteritems(): log.info("Movie name {n}".format(n=movie_name)) for a in model.aggregators: log.info(movie_name + " " + repr(a)) log.info("") log.info("Total models") for a in total_model.aggregators: log.info(a) attributes = get_attributes(_total_aggregators) log.info("Attributes from streaming mapping Report.") for a in attributes: log.info(a) plot_config_views = self._get_plot_view_configs() plot_groups = [] ds = openDataFile(self.alignment_file) ds.updateCounts() if len(ds) > 0: # keeping the ids independent requires a bit of dictionary madness # {report_id:HistogramAggregator} id_to_aggregators = {k: _total_aggregators[v] for k, v in self.HISTOGRAM_IDS.iteritems()} plot_groups = to_plot_groups(plot_config_views, output_dir, id_to_aggregators) rb_pg = PlotGroup(Constants.PG_RAINBOW) rb_png = "mapped_concordance_vs_read_length.png" make_rainbow_plot(self.alignment_file, rb_png) rb_plt = Plot(Constants.P_RAINBOW, rb_png, caption=get_plot_caption(spec, Constants.PG_RAINBOW, Constants.P_RAINBOW)) rb_pg.add_plot(rb_plt) plot_groups.append(rb_pg) self.add_more_plots(plot_groups, output_dir) tables = [table] report = Report(report_id, attributes=attributes, plotgroups=plot_groups, tables=tables, dataset_uuids=self.dataset_uuids) log.debug(report) run_time = time.time() - started_at log.info("Completed running in {s:.2f} sec.".format(s=run_time)) return report
def to_report(self, output_dir, report_id=Constants.R_ID): """ This needs to be cleaned up. Keeping the old interface for testing purposes. """ started_at = time.time() log.info("Found {n} movies.".format(n=len(self.movies))) log.info("Working from {n} alignment file{s}: {f}".format( n=len(self.alignment_file_list), s='s' if len(self.alignment_file_list) > 1 else '', f=self.alignment_file_list)) # make this a dict {attribute_key_name:Aggreggator} so it's easy to # access the instances after they've been computed. # there's duplicated keys in the attributes? # number_of_aligned_reads/mapped_reads_n _total_aggregators = self._get_total_aggregators() null_filter = lambda r: True total_model = StatisticsModel( _total_aggregators.values(), filter_func=null_filter) # need to create specific instances for a given movie. This is used to # create the mapping reports stats table movie_models = {} def _my_filter(movie_name1, movie_name2): return movie_name1 == movie_name2 for movie in self.movies: ags = [k() for k in self.COLUMN_AGGREGATOR_CLASSES] # Note this WILL NOT work because of how scope works in python # filter_by_movie_func = lambda m_name: movie.name == m_name _my_filter_func = functools.partial(_my_filter, movie) model = StatisticsModel(ags, filter_func=_my_filter_func) movie_models[movie] = model # The statistic models that will be run all_models = [total_model] + movie_models.values() log.debug(all_models) # Run all the analysis. Now the aggregators can be accessed analyze_movies(self.movies, self.alignment_file_list, all_models) # temp structure used to create the report table. The order is # important # add total values _to_a = lambda k: _total_aggregators[k].attribute _row = [_to_a(n) for n in self.COLUMN_ATTR] _row.insert(0, 'All Movies') movie_datum = [_row] # Add each individual movie stats for movie_name_, model_ in movie_models.iteritems(): _row = [movie_name_] for a in model_.aggregators: _row.append(a.attribute) movie_datum.append(_row) log.info(movie_datum) # create the Report table table = self._to_table(movie_datum) for movie_name, model in movie_models.iteritems(): log.info("Movie name {n}".format(n=movie_name)) for a in model.aggregators: log.info(movie_name + " " + repr(a)) log.info("") log.info("Total models") for a in total_model.aggregators: log.info(a) attributes = get_attributes(_total_aggregators) self.add_more_attributes(attributes) log.info("Attributes from streaming mapping Report.") for a in attributes: log.info(a) plot_config_views = self._get_plot_view_configs() plot_groups = [] ds = openDataFile(self.alignment_file) ds.updateCounts() if len(ds) > 0: # keeping the ids independent requires a bit of dictionary madness # {report_id:HistogramAggregator} id_to_aggregators = {k: _total_aggregators[v] for k, v in self.HISTOGRAM_IDS.iteritems()} plot_groups = to_plot_groups(plot_config_views, output_dir, id_to_aggregators) rb_pg = PlotGroup(Constants.PG_RAINBOW) rb_png = "mapped_concordance_vs_read_length.png" make_rainbow_plot(self.alignment_file, op.join(output_dir, rb_png)) rb_plt = Plot(Constants.P_RAINBOW, rb_png) rb_pg.add_plot(rb_plt) plot_groups.append(rb_pg) self.add_more_plots(plot_groups, output_dir) tables = [table] report = Report(report_id, attributes=attributes, plotgroups=plot_groups, tables=tables, dataset_uuids=self.dataset_uuids) log.debug(report) run_time = time.time() - started_at log.info("Completed running in {s:.2f} sec.".format(s=run_time)) return report
def _test(): pg = PlotGroup('foo') pg.add_plot(Plot('id', 'i1')) pg.add_plot(Plot('id', 'i2'))
def _test(): pg = PlotGroup('foo') pg.add_plot(Plot('id', 'i1')) pg.add_plot(Plot('id', 'i2'))