def test_alpha_diversity_group_unknown_metric(self):
     with patch.object(AlphaRepo, 'available_metrics') as mock_metrics:
         mock_metrics.return_value = ['metric-a', 'metric-b']
         metric = 'observed_otus'
         with self.assertRaises(UnknownResource):
             alpha_group(self.post_body,
                         alpha_metric=metric,
                         summary_statistics=False,
                         return_raw=True)
    def test_alpha_diversity_group_unknown_sample(self):
        # One ID not found (out of two)
        with patch.object(AlphaRepo, 'exists') as mock_exists, \
                patch.object(AlphaRepo, 'available_metrics') as mock_metrics:
            mock_metrics.return_value = ['observed_otus']
            mock_exists.side_effect = [True, False]
            with self.assertRaises(UnknownID):
                alpha_group(self.post_body, 'observed_otus')

        # Multiple IDs do not exist
        with patch.object(AlphaRepo, 'exists') as mock_exists, \
                patch.object(AlphaRepo, 'available_metrics') as mock_metrics:
            mock_metrics.return_value = ['observed_otus']
            mock_exists.side_effect = [False, False]
            with self.assertRaises(UnknownID):
                alpha_group(self.post_body, 'observed_otus')
    def test_alpha_diversity_group_return_summary_only(self):
        with patch.object(AlphaRepo, 'get_alpha_diversity') as mock_method, \
                patch.object(AlphaRepo, 'exists') as mock_exists, \
                patch.object(AlphaRepo, 'available_metrics') as mock_metrics:

            mock_metrics.return_value = ['observed_otus']
            mock_exists.return_value = [True, True]
            mock_method.return_value = pd.Series(
                {
                    'sample-foo-bar': 7,
                    'sample-baz-bat': 9.5,
                    'sample-qux-quux': 7.5,
                    'sample-4': 8,
                },
                name='observed_otus')
            metric = 'observed_otus'
            response, code = alpha_group(
                body={
                    'sample_ids': [
                        'sample-foo-bar',
                        'sample-baz-bat',
                        'sample-qux-quux',
                        'sample-4',
                    ]
                },
                alpha_metric=metric,
                summary_statistics=True,
                percentiles=[100, 0, 50, 20],
                return_raw=False,
            )

            exp = {
                'alpha_metric': 'observed_otus',
                'group_summary': {
                    'mean': 8,
                    'median': 7.75,
                    'std': sqrt(0.875),
                    'group_size': 4,
                    'percentile': [100, 0, 50, 20],
                    'percentile_values': [9.5, 7, 7.75, 7.3]
                }
            }

            self.assertEqual(code, 200)
            obs = json.loads(response.data)
            self.assertCountEqual(exp.keys(), obs.keys())
            self.assertCountEqual(exp['alpha_metric'], obs['alpha_metric'])
            self.assertCountEqual(exp['group_summary'].keys(),
                                  obs['group_summary'].keys())
            gs_exp = exp['group_summary']
            gs_obs = obs['group_summary']
            npt.assert_array_almost_equal(gs_exp.pop('percentile'),
                                          gs_obs.pop('percentile'))
            npt.assert_array_almost_equal(gs_exp.pop('percentile_values'),
                                          gs_obs.pop('percentile_values'))
            # checks of the numerical parts of the expected and observed are
            #  almost the same
            pdt.assert_series_equal(pd.Series(gs_exp),
                                    pd.Series(gs_obs),
                                    check_exact=False)
 def test_alpha_diversity_group_return_raw_only_metadata_query_only(self):
     post_body = {
         'metadata_query': {
             'some query': 'value'
         },
     }
     with patch.object(AlphaRepo, 'get_alpha_diversity') as mock_method, \
             patch.object(AlphaRepo, 'exists') as mock_exists, \
             patch.object(AlphaRepo,
                          'available_metrics') as mock_metrics, \
             patch.object(MetadataRepo,
                          'sample_id_matches') as mock_matches:
         mock_metrics.return_value = ['observed_otus']
         # first two values are used on checking requested ids, the other
         # two are used for checking ids that match metadata query
         mock_exists.side_effect = [True, True, True, True]
         mock_method.return_value = pd.Series(
             {
                 'sample-foo-bar': 8.25,
                 'sample-baz-bat': 9.01
             },
             name='observed_otus')
         mock_matches.return_value = ['sample-foo-bar', 'sample-4']
         metric = 'observed_otus'
         response, code = alpha_group(post_body,
                                      alpha_metric=metric,
                                      summary_statistics=False,
                                      return_raw=True)
     self.assertEqual(code, 200)
     args, kwargs = mock_method.call_args
     self.assertCountEqual(args[0], ['sample-foo-bar', 'sample-4'])
    def test_alpha_diversity_group_return_raw_only(self):
        with patch.object(AlphaRepo, 'get_alpha_diversity') as mock_method, \
                patch.object(AlphaRepo, 'exists') as mock_exists, \
                patch.object(AlphaRepo, 'available_metrics') as mock_metrics:
            mock_metrics.return_value = ['observed_otus']
            mock_exists.return_value = [True, True]
            mock_method.return_value = pd.Series(
                {
                    'sample-foo-bar': 8.25,
                    'sample-baz-bat': 9.01
                },
                name='observed_otus')
            metric = 'observed_otus'
            response, code = alpha_group(self.post_body,
                                         alpha_metric=metric,
                                         summary_statistics=False,
                                         return_raw=True)

        exp = {
            'alpha_metric': 'observed_otus',
            'alpha_diversity': {
                'sample-foo-bar': 8.25,
                'sample-baz-bat': 9.01,
            }
        }
        obs = json.loads(response.data)

        self.assertDictEqual(exp, obs)
        self.assertEqual(code, 200)
 def test_alpha_diversity_improper_parameters(self):
     with patch.object(AlphaRepo, 'get_alpha_diversity') as mock_method, \
             patch.object(AlphaRepo, 'exists') as mock_exists, \
             patch.object(AlphaRepo, 'available_metrics') as mock_metrics:
         mock_metrics.return_value = ['observed_otus']
         mock_exists.return_value = [True, True]
         mock_method.return_value = pd.Series(
             {
                 'sample-foo-bar': 8.25,
                 'sample-baz-bat': 9.01
             },
             name='observed_otus')
         metric = 'observed_otus'
         with self.assertRaises(IncompatibleOptions):
             alpha_group(self.post_body,
                         alpha_metric=metric,
                         summary_statistics=False,
                         return_raw=False)
    def test_alpha_diversity_group_percentiles_none(self):
        with patch.object(AlphaRepo, 'get_alpha_diversity') as mock_method, \
                patch.object(AlphaRepo, 'exists') as mock_exists, \
                patch.object(AlphaRepo, 'available_metrics') as mock_metrics:

            mock_metrics.return_value = ['observed_otus']
            mock_exists.return_value = [True, True]
            mock_method.return_value = pd.Series(
                {
                    'sample-foo-bar': 7,
                    'sample-baz-bat': 9.5,
                    'sample-qux-quux': 7.5,
                    'sample-4': 8,
                },
                name='observed_otus')
            metric = 'observed_otus'
            response, code = alpha_group(
                body={
                    'sample_ids': [
                        'sample-foo-bar',
                        'sample-baz-bat',
                        'sample-qux-quux',
                        'sample-4',
                    ]
                },
                alpha_metric=metric,
                summary_statistics=True,
                percentiles=None,
                return_raw=False,
            )
            self.assertEqual(code, 200)
            obs = json.loads(response.data)
            self.assertIn('group_summary', obs)
            summary = obs['group_summary']
            self.assertIn('percentile', summary)
            perc = summary['percentile']
            # check default value of percentiles is returned
            npt.assert_array_almost_equal(perc, list(range(10, 91, 10)))