class AlphaPlottingAltTestCase(MockedJsonifyTestCase): jsonify_to_patch = [ 'microsetta_public_api.api.plotting.jsonify', 'microsetta_public_api.utils._utils.jsonify', ] def setUp(self): super().setUp() faith_pd_values = [1, 2, 3, 4] faith_pd_index = ['s01', 's02', 's04', 's05'] shannon_values = [7.24, 9.05, 8.25] shannon_index = ['s01', 's02', 'sOther'] metadata = MockMetadataElement( pd.DataFrame( { 'age_cat': ['30s', '40s', '50s', '30s', '30s'], 'num_var': [3, 4, 5, 6, 7], }, index=['s01', 's02', 's04', 's05', 'sOther'])) self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({ '__metadata__': metadata, '__alpha__': AlphaElement({ 'faith_pd': pd.Series(faith_pd_values, index=faith_pd_index), 'shannon': pd.Series(shannon_values, index=shannon_index), }) }), 'dataset2': DictElement({ '__metadata__': metadata, }), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.plotting.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources def test_simple_plot(self): response, code = plot_alpha_filtered_alt(dataset='dataset1', alpha_metric='faith_pd') self.assertEqual(200, code) def test_simple_plot_with_vertline_for_sample_id(self): response, code = plot_alpha_filtered_alt(dataset='dataset1', alpha_metric='faith_pd', sample_id='s01') self.assertEqual(200, code)
def setUpClass(cls): axis_labels = ['PC1', 'PC2', 'PC3'] cls.test_df1 = pd.DataFrame.from_dict( { 's1': [0.1, 0.2, 7], 's2': [0.9, 0.2, 7], }, orient='index', columns=axis_labels, ) cls.test_df1.index.name = 'Sample ID' cls.pcoa1 = OrdinationResults( 'pcoa1', 'pcoa1', eigvals=pd.Series( [7, 2, 1], index=axis_labels, ), samples=cls.test_df1, proportion_explained=pd.Series( [0.7, 0.2, 0.1], index=axis_labels, ), ) cls.test_metadata = pd.DataFrame( { 'age_cat': ['30s', '40s', '50s', '30s', None], 'num_cat': [7.24, 7.24, 8.25, 7.24, None], 'other': [1, 2, 3, 4, None], }, index=pd.Series(['s1', 's2', 'c', 'd', 'e'], name='#SampleID')) cls.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({ '__metadata__': MockMetadataElement(cls.test_metadata), '__pcoa__': PCOAElement({ 'sample_set': DictElement({ 'beta_metric': cls.pcoa1, }), }) }), 'dataset2': DictElement({ '__metadata__': MockMetadataElement(cls.test_metadata), }), }), }) cls.resources.accept(TrivialVisitor()) cls.res_patcher = patch( 'microsetta_public_api.api.emperor.get_resources') cls.mock_resources = cls.res_patcher.start() cls.mock_resources.return_value = cls.resources
def test_nested_element(self): element = AlphaElement(DictElement({'a': 'b', 'c': MetadataElement('d')})) mock = MagicMock() element.accept(mock) mock.visit_alpha.assert_called() mock.visit_metadata.assert_called()
def setUp(self): super().setUp() dm_values = [[0, 1, 2, 3], [1, 0, 3, 4], [2, 3, 0, 5], [3, 4, 5, 0]] ids = ['s1', 's2', 's3', 's4'] dm = DistanceMatrix( dm_values, ids=ids, ) self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({'__beta__': BetaElement({'unifrac': dm})}), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.diversity.beta.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources
def setUp(self): super().setUp() neighbors = pd.DataFrame([['s2', 's3', 's4'], ['s1', 's3', 's4'], ['s1', 's2', 's4'], ['s1', 's2', 's3']], columns=['k0', 'k1', 'k2'], index=['s1', 's2', 's3', 's4']) neighbors.index.name = 'sample_id' self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement( {'__neighbors__': BetaElement({'unifrac': neighbors})}), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.diversity.beta.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources
def setUp(self): super().setUp() faith_pd_values = [1, 2, 3, 4] faith_pd_index = ['s01', 's02', 's04', 's05'] shannon_values = [7.24, 9.05, 8.25] shannon_index = ['s01', 's02', 'sOther'] metadata = MockMetadataElement( pd.DataFrame( { 'age_cat': ['30s', '40s', '50s', '30s', '30s'], 'num_var': [3, 4, 5, 6, 7], }, index=['s01', 's02', 's04', 's05', 'sOther'])) self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({ '__metadata__': metadata, '__alpha__': AlphaElement({ 'faith_pd': pd.Series(faith_pd_values, index=faith_pd_index), 'shannon': pd.Series(shannon_values, index=shannon_index), }) }), 'dataset2': DictElement({ '__metadata__': metadata, }), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.plotting.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources
def atomic_update_resources(resource): # create a new element to store the data in element = DictElement() element.update(resource) visitor = Q2Visitor() element.accept(visitor) # after data has been loaded by the q2 visitor, update resources_alt # so that it is accessible. # Updating resources_alt from another element means the server will # not show the skeleton of any unloaded data to the client resources_alt.update(element)
class BetaTests(MockedJsonifyTestCase): jsonify_to_patch = [ 'microsetta_public_api.api.diversity.beta.jsonify', 'microsetta_public_api.utils._utils.jsonify', ] def setUp(self): super().setUp() neighbors = pd.DataFrame([['s2', 's3', 's4'], ['s1', 's3', 's4'], ['s1', 's2', 's4'], ['s1', 's2', 's3']], columns=['k0', 'k1', 'k2'], index=['s1', 's2', 's3', 's4']) neighbors.index.name = 'sample_id' self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement( {'__neighbors__': BetaElement({'unifrac': neighbors})}), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.diversity.beta.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources def tearDown(self): self.res_patcher.stop() super().tearDown() def test_k_nearest(self): results, code = k_nearest(dataset='dataset1', beta_metric='unifrac', k=1, sample_id='s1') self.assertCountEqual(json.loads(results), ['s2']) results, code = k_nearest(dataset='dataset1', beta_metric='unifrac', k=2, sample_id='s2') self.assertCountEqual(json.loads(results), ['s1', 's3']) def test_k_nearest_unknown_dataset(self): with self.assertRaises(UnknownResource): k_nearest(dataset='dne', beta_metric='unifrac', k=2, sample_id='s2') def test_k_nearest_unknown_metric(self): with self.assertRaises(UnknownMetric): k_nearest(dataset='dataset1', beta_metric='unifork', k=2, sample_id='s2') def test_k_nearest_unknown_id(self): with self.assertRaises(UnknownID): k_nearest(dataset='dataset1', beta_metric='unifrac', k=2, sample_id='s2-dne') def test_k_nearest_invalid_k(self): with self.assertRaises(InvalidParameter): k_nearest(dataset='dataset1', beta_metric='unifrac', k=724, sample_id='s2')
def setUpClass(cls): cls.post_body = { 'sample_ids': [ 'sample-1', 'sample-2', ] } cls.table = biom.Table(np.array([[0, 1, 2], [2, 4, 6], [3, 0, 1]]), ['feature-1', 'feature-2', 'feature-3'], ['sample-1', 'sample-2', 'sample-3']) cls.taxonomy_df = pd.DataFrame( [['feature-1', 'a; b; c', 0.123], ['feature-2', 'a; b; c; d; e', 0.345], ['feature-3', 'a; f; g; h', 0.678]], columns=['Feature ID', 'Taxon', 'Confidence']) cls.taxonomy_df.set_index('Feature ID', inplace=True) # variances cls.table_vars = biom.Table( np.array([[0, 1, 2], [2, 4, 6], [3, 0, 1]]), ['feature-1', 'feature-2', 'feature-3'], ['sample-1', 'sample-2', 'sample-3']) cls.table_name = 'some-table' cls.table_with_var_name = 'some-table-w-var' cls.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({ '__taxonomy__': TaxonomyElement({ cls.table_name: { 'table': cls.table, 'feature-data-taxonomy': cls.taxonomy_df, }, cls.table_with_var_name: { 'table': cls.table, 'feature-data-taxonomy': cls.taxonomy_df, 'variances': cls.table_vars, } }) }), 'dataset2': DictElement({}), '__metadata__': MockMetadataElement( pd.DataFrame( { 'age_cat': ['30s', '40s', '50s', '30s', '30s'], 'num_var': [3, 4, 5, 6, 7], }, index=['s01', 's02', 's04', 's05', 'sOther'])) }), }) cls.resources.accept(TrivialVisitor()) cls.res_patcher = patch( 'microsetta_public_api.api.taxonomy.get_resources') cls.mock_resources = cls.res_patcher.start() cls.mock_resources.return_value = cls.resources
def test_updates(self): element = DictElement({'a': DictElement({'b': None})}) element.updates(MetadataElement('Arg'), 'a', 'b') self.assertEqual('Arg', element.gets('a', 'b')) element.updates('Arg2', 'a', 'b', 'c', 'd', 'e') self.assertEqual('Arg2', element.gets('a', 'b', 'c', 'd', 'e')) element.updates('Arg3', 'c', 'd') self.assertEqual('Arg3', element['c']['d']) element.updates(DictElement({'a': DictElement({'c': MetadataElement( 'Arg4') })})) self.assertEqual('Arg3', element['c']['d']) self.assertEqual('Arg4', element['a']['c']) exp = {'bar': {'qux': 'corge', 'hoge': 'piyo'}} foo = DictElement({'bar': DictElement({'qux': 'corge'})}) foo.updates('piyo', 'bar', 'hoge') self.assertDictEqual(exp, foo) foo = DictElement({'bar': DictElement({'qux': 'corge'})}) foo.updates({'bar': DictElement({'hoge': 'piyo'})}) self.assertDictEqual(exp, foo) exp = {'bar': {'qux': 'corge', 'hoge': 'fuga'}} foo.updates('fuga', 'bar', 'hoge') self.assertDictEqual(exp, foo) foo.updates('waldo', 'bar', 'garp', 'grault') exp = {'bar': {'qux': 'corge', 'hoge': 'fuga', 'garp': {'grault': 'waldo'}}} self.assertDictEqual(exp, foo) foo = DictElement() foo.updates({'datasets': {'garply': 'grault'}}) self.assertDictEqual({'datasets': {'garply': 'grault'}}, foo) foo = DictElement() foo.updates({'datasets': AlphaElement({'garply': 'grault'})}) self.assertDictEqual({'datasets': {'garply': 'grault'}}, foo) self.assertIsInstance(foo['datasets'], AlphaElement)
@timeit('vist_taxonomy') def visit_taxonomy(self, element): element.data = _transform_dict_of_table(element, self.schema.taxonomy_kw) @timeit('visit_pcoa') def visit_pcoa(self, element): element.data = _dict_of_dict_of_paths_to_pcoa(element, self.schema.pcoa_kw) @timeit('visit_metadata') def visit_metadata(self, element): element.data = _load_q2_metadata(element, self.schema.metadata_kw) @timeit('visit_beta') def visit_beta(self, element): element.data = _dict_of_paths_to_beta_data(element, self.schema.beta_kw) @timeit('visit_dataset_detail') def visit_dataset_detail(self, element): element.data = _dict_of_literals_to_dict(element, self.schema.detail_kw) resources_alt = DictElement() def get_resources(): return resources_alt
class AlphaAltTests(MockedJsonifyTestCase): jsonify_to_patch = [ 'microsetta_public_api.api.diversity.alpha.jsonify', 'microsetta_public_api.utils._utils.jsonify', ] def setUp(self): super().setUp() faith_pd_values = [1, 2, 3, 4] faith_pd_index = ['s01', 's02', 's04', 's05'] shannon_values = [7.24, 9.05, 8.25] shannon_index = ['s01', 's02', 'sOther'] metadata = MockMetadataElement( pd.DataFrame( { 'age_cat': ['30s', '40s', '50s', '30s', '30s'], 'num_var': [3, 4, 5, 6, 7], }, index=['s01', 's02', 's04', 's05', 'sOther'])) self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({ '__metadata__': metadata, '__alpha__': AlphaElement({ 'faith_pd': pd.Series(faith_pd_values, index=faith_pd_index), 'shannon': pd.Series(shannon_values, index=shannon_index), }) }), 'dataset2': DictElement({ '__metadata__': metadata, }), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.diversity.alpha.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources def tearDown(self): self.res_patcher.stop() super().tearDown() def test_availalbe_metrics_alt(self): response, code = available_metrics_alpha_alt('dataset1') avail = json.loads(response) self.assertEqual(200, code) self.assertDictEqual(avail, {'alpha_metrics': ['faith_pd', 'shannon']}) with self.assertRaises(UnknownResource): # check for dataset that exists but has no alpha available_metrics_alpha_alt('dataset2') with self.assertRaises(UnknownResource): # check for dataset that does not exist available_metrics_alpha_alt('dataset3') def test_get_alpha_alt(self): faith_pd, code1 = get_alpha_alt('dataset1', 's01', 'faith_pd') shannon, code2 = get_alpha_alt('dataset1', 's02', 'shannon') faith_pd_value = json.loads(faith_pd) shannon_value = json.loads(shannon) self.assertEqual(faith_pd_value['data'], 1) self.assertEqual(200, code1) self.assertEqual(shannon_value['data'], 9.05) self.assertEqual(200, code2) def test_get_alpha_alt_404(self): with self.assertRaisesRegex(UnknownResource, 'dataset3'): get_alpha_alt('dataset3', 's03', 'faith_pd') with self.assertRaisesRegex(UnknownResource, '__alpha__'): get_alpha_alt('dataset2', 's03', 'faith_pd') def test_alpha_group_alt(self): request = {'sample_ids': ['s01', 's04']} response, code = alpha_group_alt(request, 'dataset1', 'faith_pd', return_raw=True) self.assertEqual(code, 200) obs = json.loads(response) self.assertDictEqual({'s01': 1, 's04': 3}, obs['alpha_diversity']) def test_alpha_group_alt_404_sample_id(self): request = {'sample_ids': ['s01', 'dne']} with self.assertRaises(UnknownID): alpha_group_alt(request, 'dataset1', 'faith_pd', return_raw=True) def test_alpha_group_alt_404_metric(self): request = {'sample_ids': ['s01', 's04']} with self.assertRaises(UnknownResource): alpha_group_alt(request, 'dataset1', 'bad-metric', return_raw=True) def test_alpha_group_alt_filter_metadata_OR(self): post_body = { 'sample_ids': [ 's04', ], 'metadata_query': { "condition": "AND", "rules": [ { "id": "age_cat", "field": "age_cat", "operator": "equal", "value": "30s", }, ], }, 'condition': "OR", } response, code = alpha_group_alt(post_body, 'dataset1', 'faith_pd', return_raw=True) obs = json.loads(response) self.assertEqual(code, 200) sample_ids = obs['alpha_diversity'].keys() self.assertCountEqual(['s01', 's04', 's05'], sample_ids) def test_alpha_exists_single_alt(self): response, code = exists_single_alt('dataset1', 'faith_pd', 's01') self.assertEqual(code, 200) obs = json.loads(response) self.assertTrue(obs) response, code = exists_single_alt('dataset1', 'faith_pd', 's-dne') self.assertEqual(code, 200) obs = json.loads(response) self.assertFalse(obs) response, code = exists_single_alt('dataset1', 'shannon', 's03') self.assertEqual(code, 200) obs = json.loads(response) self.assertFalse(obs) def test_alpha_exists_single_alt_errors(self): with self.assertRaises(UnknownResource): exists_single_alt('dataset2', 'shannon', 's03') with self.assertRaises(UnknownResource): exists_single_alt('dataset1', 'dne-metric', 's03') def test_alpha_exists_group_alt(self): body = ['s01', 's03', 's04'] response, code = exists_group_alt(body, 'dataset1', 'faith_pd') self.assertEqual(code, 200) obs = json.loads(response) self.assertListEqual(obs, [True, False, True]) def test_alpha_exists_group_alt_errors(self): body = ['s01', 's03', 's04'] with self.assertRaises(UnknownResource): exists_group_alt(body, 'dataset2', 'faith_pd') with self.assertRaises(UnknownResource): exists_group_alt(body, 'dataset1', 'dne-metric')
class BetaTests(MockedJsonifyTestCase): jsonify_to_patch = [ 'microsetta_public_api.api.diversity.beta.jsonify', 'microsetta_public_api.utils._utils.jsonify', ] def setUp(self): super().setUp() dm_values = [[0, 1, 2, 3], [1, 0, 3, 4], [2, 3, 0, 5], [3, 4, 5, 0]] ids = ['s1', 's2', 's3', 's4'] dm = DistanceMatrix( dm_values, ids=ids, ) self.resources = DictElement({ 'datasets': DictElement({ 'dataset1': DictElement({'__beta__': BetaElement({'unifrac': dm})}), }), }) self.resources.accept(TrivialVisitor()) self.res_patcher = patch( 'microsetta_public_api.api.diversity.beta.get_resources') self.mock_resources = self.res_patcher.start() self.mock_resources.return_value = self.resources def tearDown(self): self.res_patcher.stop() super().tearDown() def test_k_nearest(self): results, code = k_nearest(dataset='dataset1', beta_metric='unifrac', k=1, sample_id='s1') self.assertCountEqual(json.loads(results), ['s2']) results, code = k_nearest(dataset='dataset1', beta_metric='unifrac', k=2, sample_id='s2') self.assertCountEqual(json.loads(results), ['s1', 's3']) def test_k_nearest_unknown_dataset(self): with self.assertRaises(UnknownResource): k_nearest(dataset='dne', beta_metric='unifrac', k=2, sample_id='s2') def test_k_nearest_unknown_metric(self): with self.assertRaises(UnknownMetric): k_nearest(dataset='dataset1', beta_metric='unifork', k=2, sample_id='s2') def test_k_nearest_unknown_id(self): with self.assertRaises(UnknownID): k_nearest(dataset='dataset1', beta_metric='unifrac', k=2, sample_id='s2-dne') def test_k_nearest_invalid_k(self): with self.assertRaises(InvalidParameter): k_nearest(dataset='dataset1', beta_metric='unifrac', k=724, sample_id='s2')