コード例 #1
0
def construct_representative_point_response(service_area_ids,
                                            include_census_data, engine):
    """Construct responses for /api/representative_points requests."""
    return representative_points.fetch_representative_points(
        service_area_ids=service_area_ids,
        include_census_data=include_census_data,
        engine=engine)
コード例 #2
0
    def test_rp_request(self, mock_fetch):
        """Test provider requests in a simple case."""
        request_service_areas = {
            'service_area_ids': ['ca_los_angeles_county_00000', 'not_valid']
        }

        def _mock_get_json(force=True):
            return request_service_areas

        mock_request = mock.MagicMock()
        mock_request.get_json = _mock_get_json

        mock_fetch.return_value = fetch_representative_points(
            service_area_ids=request_service_areas['service_area_ids'],
            include_census_data=False,
            engine=engine)

        try:
            results = representative_points.representative_points_request(
                self.app, mock_request, engine)
        except Exception:
            pytest.fail('Could not fetch representative_points.')

        assert len(results) > 1000
        assert all(result['service_area_id'] in
                   request_service_areas['service_area_ids']
                   for result in results)
コード例 #3
0
 def test_fetch_representative_points_two_service_areas():
     """Test fetch_representative_points."""
     service_areas = [
         'ca_los_angeles_county_00000', 'ca_los_angeles_county_00000'
     ]
     results = representative_points.fetch_representative_points(
         service_areas, include_census_data=False, engine=engine)
     assert len(results) > 1000
コード例 #4
0
ファイル: adequacy.py プロジェクト: philipdickinson/encompass
def mock_adequacy_calculation(provider_ids, service_area_ids):
    """Mock adequacy calculation."""
    points = representative_points.fetch_representative_points(
        service_area_ids)
    point_ids = [point['id'] for point in points]

    return [
        mock_adequacy(point_id, random.choice(provider_ids))
        for point_id in point_ids
    ]
コード例 #5
0
def check_sample_points_exist():
    """
    Retrieve sample points for a service area from the sample data and if none exists, exit
    with nonzero code to indicate that initial sample data population should proceed.
    """
    engine = connect.create_db_engine(echo=True)
    service_areas = ['ca_los_angeles_county_00000'
                     ]  # A service area from the base sample data.
    results = representative_points.fetch_representative_points(
        service_areas, include_census_data=False, engine=engine)
    if len(results) == 0:
        exit(1)  # Exit nonzero to indicate that no records exist.
コード例 #6
0
def fetch_census_info_by_service_area(service_area_ids, engine):
    """
    Fetch census information aggregated across an entire service area.

    For each service area and census field combination, calculate the average of each
    point's census value, weighted by population.

    The returned mapping for each service area has the same structure as each representative point.
    """
    logger.debug('Fetching census information for {} service areas'.format(len(service_area_ids)))
    # Fetch all points for the given service areas.
    all_points = representative_points.fetch_representative_points(
        service_area_ids=service_area_ids,
        include_census_data=True,
        engine=engine
    )

    # Infer the demographics fields from the first returned point.
    try:
        sample_demographic_map = all_points[0]['demographics']
    except IndexError:
        logger.debug('No points found for specified service areas.')
        return {}

    # Group points by service area.
    points_by_service_area = collections.defaultdict(list)
    for point in all_points:
        points_by_service_area[point['service_area_id']].append(point)

    response = {}
    for service_area in points_by_service_area:

        service_area_demographics = collections.defaultdict(dict)
        points = points_by_service_area[service_area]
        total_population = sum(point['population'] for point in points)

        # Set the service area value to the weighted average of its constituent points' values.
        for category in sample_demographic_map:
            for field in sample_demographic_map[category]:
                service_area_demographics[category][field] = sum(
                    point['population'] * point['demographics'][category][field]
                    for point in points
                ) / total_population

        response[service_area] = dict(service_area_demographics)

    return response
コード例 #7
0
 def test_fetch_representative_points_no_valid_service_area():
     """Test fetch_representative_points."""
     service_areas = ['not_valid']
     results = representative_points.fetch_representative_points(
         service_areas, include_census_data=False, engine=engine)
     assert len(results) == 0