From 1ed971107865114ba6b5d8cfc491da6250c0dc94 Mon Sep 17 00:00:00 2001 From: Marcus Fedarko Date: Mon, 25 Feb 2019 17:37:12 -0800 Subject: [PATCH] Attempt to fix travis issues with #46 --- rankratioviz/generate.py | 21 ++++++++----- rankratioviz/tests/testing_utilities.py | 41 +++++++++++++------------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/rankratioviz/generate.py b/rankratioviz/generate.py index 7d362289..0d2f55e3 100755 --- a/rankratioviz/generate.py +++ b/rankratioviz/generate.py @@ -83,7 +83,7 @@ def gen_rank_plot(V): Returns: - altair.Chart object for the rank plot. + JSON describing altair.Chart for the rank plot. """ # Get stuff ready for the rank plot @@ -91,7 +91,7 @@ def gen_rank_plot(V): # angry if you pass in ints as column IDs). This is a problem with # OrdinationResults files, since just getting the raw column IDs gives int # values (0 for the first column, 1 for the second column, etc.) - V.columns = [str(c) for c in V.columns] + V.columns = ["Rank " + str(c) for c in V.columns] # The default rank column is just whatever the first rank is. This is what # the rank plot will use when it's first drawn. @@ -150,7 +150,11 @@ def gen_rank_plot(V): # lines gridOpacity=0.35 ).interactive() - return rank_chart + + rank_chart_json = rank_chart.to_dict() + rank_ordering = "rankratioviz_rank_ordering" + rank_chart_json["datasets"][rank_ordering] = list(V.columns) + return rank_chart_json def gen_sample_plot(table, metadata): @@ -163,7 +167,7 @@ def gen_sample_plot(table, metadata): Returns: - altair.Chart object for the sample scatterplot. + JSON describing altair.Chart for the sample plot. """ # Used to set x-axis and color @@ -272,7 +276,7 @@ def gen_visualization(V, processed_table, df_sample_metadata, output_dir): index_path: a path to the index.html file for the output visualization. This is needed when calling q2templates.render(). """ - rank_plot_chart = gen_rank_plot(V) + rank_plot_json = gen_rank_plot(V) sample_plot_json = gen_sample_plot(processed_table, df_sample_metadata) os.makedirs(output_dir, exist_ok=True) # copy files for the visualization @@ -307,8 +311,9 @@ def gen_visualization(V, processed_table, df_sample_metadata, output_dir): # write new files rank_plot_loc = os.path.join(output_dir, 'rank_plot.json') sample_plot_loc = os.path.join(output_dir, 'sample_plot.json') - rank_plot_chart.save(rank_plot_loc) # For reference: https://stackoverflow.com/a/12309296 - with open(sample_plot_loc, "w") as jfile: - json.dump(sample_plot_json, jfile) + with open(rank_plot_loc, "w") as jf: + json.dump(rank_plot_json, jf) + with open(sample_plot_loc, "w") as jf2: + json.dump(sample_plot_json, jf2) return index_path diff --git a/rankratioviz/tests/testing_utilities.py b/rankratioviz/tests/testing_utilities.py index 0eea109d..dd4c8baa 100644 --- a/rankratioviz/tests/testing_utilities.py +++ b/rankratioviz/tests/testing_utilities.py @@ -101,12 +101,15 @@ def validate_rank_plot_json(input_ranks_loc, rank_json_loc): assert rank_plot["title"] == "Ranks" basic_vegalite_json_validation(rank_plot) dn = rank_plot["data"]["name"] - # Check that we have the same count of ranks as in the input ranks file + # Check that we have the same count of ranked features as in the + # input ranks file: this assumes that every ranked feature has an entry + # in the BIOM table, which is a reasonable assumption assert len(rank_plot["datasets"][dn]) == len(ranked_features) # Loop over every rank included in this JSON file: - prev_coefs_val = float("-inf") + rank_ordering = rank_plot["datasets"]["rankratioviz_rank_ordering"] + prev_rank_0_val = float("-inf") prev_x_val = -1 - for rank in rank_plot["datasets"][dn]: + for feature in rank_plot["datasets"][dn]: # Check that we're using the correct "coefs" value # We use pytest's approx class to get past floating point # imprecisions. Note that we just leave this at the default for @@ -116,28 +119,26 @@ def validate_rank_plot_json(input_ranks_loc, rank_json_loc): # NOTE Based on how we construct feature labels from DEICODE # input. If that changes, this will need to change or this # will break. - feature_id = rank["Feature ID"].split("|")[2] + feature_id = feature["Feature ID"].split("|")[2] else: - feature_id = rank["Feature ID"] - # NOTE This assumes that the 0-th rank (i.e. the first) is the one - # stored in the rank plot. When we eventually update this so that - # an arbitrary number of ranks for each feature are stored in the - # rank plot, we'll need to ensure that there's a one-to-one - # correspondence between the various "coefs" and - # ranked_features[feature_id]. - assert ranked_features[feature_id][0] == approx(rank["coefs"]) - # Check that the ranks are in order (i.e. their "coefs" vals are - # monotonically increasing) + feature_id = feature["Feature ID"] + + # Check that each ranked feature matches + for r in range(len(rank_ordering)): + actual_rank_val = ranked_features[feature_id][r] + assert actual_rank_val == approx(feature[rank_ordering[r]]) + # Check that the ranks are in order (i.e. their initial rank vals + # are monotonically increasing) # (If this rank is approximately equal to the previous rank, then # don't bother with the comparison -- but still update - # prev_coefs_val, of course.) - if rank["coefs"] != approx(prev_coefs_val): - assert rank["coefs"] >= prev_coefs_val + # prev_rank_0_val, of course.) + if feature[rank_ordering[0]] != approx(prev_rank_0_val): + assert feature[rank_ordering[0]] >= prev_rank_0_val # Check that x values are also in order - assert rank["x"] == prev_x_val + 1 + assert feature["x"] == prev_x_val + 1 # Update prev_ things for the next iteration of the loop - prev_coefs_val = rank["coefs"] - prev_x_val = rank["x"] + prev_rank_0_val = feature[rank_ordering[0]] + prev_x_val = feature["x"] def validate_sample_plot_json(biom_table_loc, metadata_loc, sample_json_loc):