Beispiel #1
0
    def get(self, request):
        bins = request.query_params.get('histogram', None)

        wage_field = self.get_wage_field(
            request.query_params.get('contract-year'))
        contracts_all = self.get_queryset(request.query_params, wage_field)

        stats = contracts_all.aggregate(Min(wage_field), Max(wage_field),
                                        Avg(wage_field), StdDev(wage_field))

        page_stats = {
            'minimum': stats[wage_field + '__min'],
            'maximum': stats[wage_field + '__max'],
            'average': quantize(stats[wage_field + '__avg']),
            'first_standard_deviation':
            quantize(stats[wage_field + '__stddev'])
        }

        if bins and bins.isnumeric():
            values = contracts_all.values_list(wage_field, flat=True)
            page_stats['wage_histogram'] = get_histogram(values, int(bins))

        pagination = self.pagination_class(page_stats)
        results = pagination.paginate_queryset(contracts_all, request)
        serializer = ContractSerializer(results, many=True)
        return pagination.get_paginated_response(serializer.data)
Beispiel #2
0
 def test_works_on_decimal_values(self):
     values = [decimal.Decimal(random.randrange(1, 101, 1))
               for _ in range(10000)]
     bins = get_histogram(values, 5)
     self.assertEqual(len(bins), 5)
     self.assertEqual(bins[0]['min'], min(values))
     self.assertEqual(bins[-1]['max'], max(values))
Beispiel #3
0
 def test_simple_histogram(self):
     num_bins = 3
     bins = get_histogram([1, 2, 3], num_bins)
     self.assertEqual(len(bins), num_bins)
     self.assertEqual(bins[0]['min'], 1)
     self.assertEqual(bins[2]['max'], 3)
     for b in bins:
         self.assertEqual(b['count'], 1)
Beispiel #4
0
 def test_returns_bins_on_empty_input(self):
     bins = get_histogram([], 10)
     self.assertEqual(len(bins), 10)
     self.assertEqual(bins[0]['min'], 0.0)
     self.assertEqual(bins[0]['max'], 0.1)
     self.assertEqual(bins[-1]['max'], 1.0)
     for b in bins:
         self.assertEqual(b['count'], 0)
Beispiel #5
0
 def test_returns_bins_on_empty_input(self):
     bins = get_histogram([], 10)
     self.assertEqual(len(bins), 10)
     self.assertEqual(bins[0]['min'], 0.0)
     self.assertEqual(bins[0]['max'], 0.1)
     self.assertEqual(bins[-1]['max'], 1.0)
     for b in bins:
         self.assertEqual(b['count'], 0)
Beispiel #6
0
 def test_simple_histogram(self):
     num_bins = 3
     bins = get_histogram([1, 2, 3], num_bins)
     self.assertEqual(len(bins), num_bins)
     self.assertEqual(bins[0]['min'], 1)
     self.assertEqual(bins[2]['max'], 3)
     for b in bins:
         self.assertEqual(b['count'], 1)
Beispiel #7
0
 def test_works_on_decimal_values(self):
     values = [
         decimal.Decimal(random.randrange(1, 101, 1)) for _ in range(10000)
     ]
     bins = get_histogram(values, 5)
     self.assertEqual(len(bins), 5)
     self.assertEqual(bins[0]['min'], min(values))
     self.assertEqual(bins[-1]['max'], max(values))
Beispiel #8
0
 def test_bigger_histogram(self):
     num_bins = 12
     values = [round(random.random(), 2) for _ in range(10000)]
     bins = get_histogram(values, num_bins)
     self.assertEqual(len(bins), num_bins)
     mx = max(values)
     mn = min(values)
     self.assertEqual(bins[0]['min'], mn)
     self.assertEqual(bins[-1]['max'], mx)
Beispiel #9
0
 def test_bigger_histogram(self):
     num_bins = 12
     values = [round(random.random(), 2) for _ in range(10000)]
     bins = get_histogram(values, num_bins)
     self.assertEqual(len(bins), num_bins)
     mx = max(values)
     mn = min(values)
     self.assertEqual(bins[0]['min'], mn)
     self.assertEqual(bins[-1]['max'], mx)
Beispiel #10
0
    def get(self, request):
        bins = request.query_params.get('histogram', None)

        """
        wage_field determines prices for a given year:
        This year, next year, or the year after.
        Relies on indexing a list, which may be less reliable than a tuple.

        This is used both here in get() and downstream in get_query_set(),
        so we have to pass it through.
        """
        possible_wage_fields = ['current_price', 'next_year_price', 'second_year_price']
        year = request.query_params.get('contract-year', 0)
        wage_field = possible_wage_fields[int(year)]
        contracts_all = self.get_queryset(request.query_params, wage_field)

        stats = contracts_all.aggregate(
            Min(wage_field), Max(wage_field),
            Avg(wage_field), StdDev(wage_field))

        page_stats = {
            'minimum': stats[wage_field + '__min'],
            'maximum': stats[wage_field + '__max'],
            'average': quantize(stats[wage_field + '__avg']),
            'first_standard_deviation': quantize(
                stats[wage_field + '__stddev']
            )
        }

        if bins and bins.isnumeric():
            values = contracts_all.values_list(wage_field, flat=True)
            page_stats['wage_histogram'] = get_histogram(values, int(bins))

        pagination = self.pagination_class(page_stats)
        results = pagination.paginate_queryset(contracts_all, request)
        serializer = ContractSerializer(results, many=True)
        return pagination.get_paginated_response(serializer.data)
Beispiel #11
0
Datei: views.py Projekt: 18F/calc
    def get(self, request):
        bins = request.query_params.get('histogram', None)

        """
        wage_field determines prices for a given year:
        This year, next year, or the year after.
        Relies on indexing a list, which may be less reliable than a tuple.

        This is used both here in get() and downstream in get_query_set(),
        so we have to pass it through.
        """
        possible_wage_fields = ['current_price', 'next_year_price', 'second_year_price']
        year = request.query_params.get('contract-year', 0)
        wage_field = possible_wage_fields[int(year)]
        contracts_all = self.get_queryset(request.query_params, wage_field)

        stats = contracts_all.aggregate(
            Min(wage_field), Max(wage_field),
            Avg(wage_field), StdDev(wage_field))

        page_stats = {
            'minimum': stats[wage_field + '__min'],
            'maximum': stats[wage_field + '__max'],
            'average': quantize(stats[wage_field + '__avg']),
            'first_standard_deviation': quantize(
                stats[wage_field + '__stddev']
            )
        }

        if bins and bins.isnumeric():
            values = contracts_all.values_list(wage_field, flat=True)
            page_stats['wage_histogram'] = get_histogram(values, int(bins))

        pagination = self.pagination_class(page_stats)
        results = pagination.paginate_queryset(contracts_all, request)
        serializer = ContractSerializer(results, many=True)
        return pagination.get_paginated_response(serializer.data)
Beispiel #12
0
 def test_when_inputs_are_same_value(self):
     bins = get_histogram([5, 5, 5], 2)
     self.assertEqual(len(bins), 2)
     self.assertEqual(bins[0]['min'], 4.5)
     self.assertEqual(bins[1]['max'], 5.5)
     self.assertEqual(bins[1]['count'], 3)
Beispiel #13
0
 def test_when_inputs_are_same_value(self):
     bins = get_histogram([5, 5, 5], 2)
     self.assertEqual(len(bins), 2)
     self.assertEqual(bins[0]['min'], 4.5)
     self.assertEqual(bins[1]['max'], 5.5)
     self.assertEqual(bins[1]['count'], 3)