Commit 10bddd10 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

Fixed sorting in heatmap

parent 0a1484cb
"""The /analytics controller. Please refer to doc/api for more information."""
import json
import logging
from typing import Tuple
from uuid import UUID
......
......@@ -63,17 +63,9 @@ class HeatmapTask(AnalyticTask):
ranking_method=ranking_method)
# sort by ranking_value
df = pd.merge(df, stats[['feature', ranking_method]], how='left',
left_index=True, right_on='feature')
df.sort_values(ranking_method, ascending=False, inplace=True)
df.drop(ranking_method, axis=1, inplace=True)
z_df = pd.merge(z_df, stats[['feature', ranking_method]], how='left',
left_index=True, right_on='feature')
z_df.sort_values(ranking_method, ascending=False, inplace=True)
z_df.drop(ranking_method, axis=1, inplace=True)
stats.sort_values(ranking_method, ascending=False, inplace=True)
self.sort(df, stats[ranking_method], ranking_method)
self.sort(z_df, stats[ranking_method], ranking_method)
self.sort(stats, stats[ranking_method], ranking_method)
# discard rows according to max_rows
df = df[:max_rows]
......@@ -81,6 +73,8 @@ class HeatmapTask(AnalyticTask):
stats = stats[:max_rows]
# prepare output for front-end
df['feature'] = df.index
z_df['feature'] = z_df.index
df = pd.melt(df, id_vars='feature', var_name='id')
z_df = pd.melt(z_df, id_vars='feature', var_name='id')
df = df.merge(z_df, on=['id', 'feature'])
......@@ -91,3 +85,14 @@ class HeatmapTask(AnalyticTask):
'data': df.to_dict(orient='list'),
'stats': stats.to_dict(orient='list')
}
@staticmethod
def sort(df, order, method):
order = order.tolist()
if method == 'P.Value' or method == 'adj.P.Val':
order = [1 - x for x in order]
elif method == 'logFC' or method == 't':
order = [abs(x) for x in order]
df['sort_value'] = order
df.sort_values('sort_value', ascending=False, inplace=True)
df.drop('sort_value', axis=1, inplace=True)
"""This module provides tests for the heatmap analysis main module."""
import json
import pytest
import pandas as pd
import numpy as np
......@@ -184,3 +182,63 @@ class TestHeatmap:
feature_col = data['feature'].tolist()
assert ['D', 'C', 'D', 'C'] == feature_col
assert result['stats']['feature'] == ['D', 'C']
def test_sorts_correct_for_different_criteria(self):
numerical_arrays = [
pd.DataFrame([[101, 'foo', 5], [101, 'bar', -12],
[102, 'foo', 10], [102, 'bar', -25],
[103, 'foo', 15], [103, 'bar', -20],
[104, 'foo', 20], [104, 'bar', -50]],
columns=['id', 'feature', 'value'])
]
subsets = [[101, 102], [103, 104]]
result = self.task.main(numerical_arrays=numerical_arrays,
numericals=[],
categoricals=[],
ranking_method='P.Value',
id_filter=[],
max_rows=100,
subsets=subsets)
stats = result['stats']['P.Value']
assert all([stats[i] < stats[i + 1] for i in range(len(stats) - 1)])
result = self.task.main(numerical_arrays=numerical_arrays,
numericals=[],
categoricals=[],
ranking_method='adj.P.Val',
id_filter=[],
max_rows=100,
subsets=subsets)
stats = result['stats']['adj.P.Val']
assert all([stats[i] < stats[i + 1] for i in range(len(stats) - 1)])
result = self.task.main(numerical_arrays=numerical_arrays,
numericals=[],
categoricals=[],
ranking_method='B',
id_filter=[],
max_rows=100,
subsets=subsets)
stats = result['stats']['B']
assert all([stats[i] > stats[i + 1] for i in range(len(stats) - 1)])
result = self.task.main(numerical_arrays=numerical_arrays,
numericals=[],
categoricals=[],
ranking_method='logFC',
id_filter=[],
max_rows=100,
subsets=subsets)
stats = result['stats']['logFC']
assert all([abs(stats[i]) > abs(stats[i + 1])
for i in range(len(stats) - 1)])
result = self.task.main(numerical_arrays=numerical_arrays,
numericals=[],
categoricals=[],
ranking_method='t',
id_filter=[],
max_rows=100,
subsets=subsets)
stats = result['stats']['t']
assert all([stats[i] > stats[i + 1] for i in range(len(stats) - 1)])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment