Beispiel #1
0
def child_fetch(dcid):
    contained_response = fetch_data('/node/property-values', {
        'dcids': [dcid],
        'property': 'containedInPlace',
        'direction': 'in'
    },
                                    compress=False,
                                    post=True)
    places = contained_response[dcid].get('in', [])

    overlaps_response = fetch_data('/node/property-values', {
        'dcids': [dcid],
        'property': 'geoOverlaps',
        'direction': 'in'
    },
                                   compress=False,
                                   post=True)
    places = places + overlaps_response[dcid].get('in', [])

    dcid_str = '^'.join(sorted(map(lambda x: x['dcid'], places)))
    pop = stats_api.get_stats_latest(dcid_str, 'Count_Person')

    place_type = get_place_type(dcid)
    wanted_types = WANTED_PLACE_TYPES.get(place_type, ALL_WANTED_PLACE_TYPES)
    result = collections.defaultdict(list)
    for place in places:
        for place_type in place['types']:
            place_pop = pop.get(place['dcid'], 0)
            # TODO(beets): Remove this when we push resolved places to prod.
            if place['dcid'].startswith('geoNames'):
                continue
            if place_type in wanted_types and place_pop > 0:
                result[place_type].append({
                    'name':
                    place.get('name', place['dcid']),
                    'dcid':
                    place['dcid'],
                    'pop':
                    place_pop,
                })

    # Filter equivalent place types - if a child place occurs in multiple groups, keep it in the preferred group type.
    for (preferred, equivalent) in EQUIVALENT_PLACE_TYPES.items():
        if preferred in result and equivalent in result:
            for preferred_place in result[preferred]:
                for i, equivalent_place in enumerate(result[equivalent]):
                    if preferred_place['dcid'] == equivalent_place['dcid']:
                        del result[equivalent][i]
                        break

    # Drop empty categories
    result = dict(filter(lambda x: len(x) > 0, result.items()))
    return result
Beispiel #2
0
def stats_var_property_wrapper(dcids):
    """Function to get properties for give statistical variables."""
    data = dc.fetch_data('/node/triples', {
        'dcids': dcids,
    },
                         compress=False,
                         post=True)
    result = {}
    # Get all the constraint properties
    for dcid, triples in data.items():
        pvs = {}
        for triple in triples:
            if triple['predicate'] == 'constraintProperties':
                pvs[triple["objectId"]] = ''
        pt = ''
        md = ''
        mprop = ''
        for triple in triples:
            if triple['predicate'] == 'measuredProperty':
                mprop = triple['objectId']
            if triple['predicate'] == 'populationType':
                pt = triple['objectId']
            if triple['predicate'] == 'measurementDenominator':
                md = triple['objectId']
            if triple['predicate'] in pvs:
                pvs[triple['predicate']] = triple['objectId']
        result[dcid] = {
            'mprop': mprop,
            'pt': pt,
            'md': md,
            'pvs': pvs,
        }
    return result
Beispiel #3
0
def get_parent_place(dcids):
    """ Get containedInPlace for each place in a list of places

    Args:
        dcids: ^ separated string of dcids. It must be a single string for the cache.

    Returns:
        A dictionary of lists of containedInPlace, keyed by dcid.
    """
    if dcids:
        dcids = dcids.split('^')
    else:
        dcids = []
    response = fetch_data('/node/property-values', {
        'dcids': dcids,
        'property': 'containedInPlace',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    result = {}
    for dcid in dcids:
        parents = response[dcid].get('out', [])
        parents.sort(key=lambda x: x['dcid'], reverse=True)
        for i in range(len(parents)):
            if len(parents[i]['types']) > 1:
                parents[i]['types'] = [
                    x for x in parents[i]['types']
                    if not x.startswith('AdministrativeArea')
                ]
        result[dcid] = parents
    return result
Beispiel #4
0
def get_place_stat_date_within_place():
    """
    Given ancestor place, child place type and stat vars, return the dates that
	have data for each stat var across all child places.
    """
    ancestor_place = request.args.get('ancestorPlace')
    if not ancestor_place:
        return Response(
            json.dumps('error: must provide a ancestorPlace field'),
            400,
            mimetype='application/json')
    child_place_type = request.args.get('childPlaceType')
    if not child_place_type:
        return Response(
            json.dumps('error: must provide a childPlaceType field'),
            400,
            mimetype='application/json')
    stat_vars = request.args.getlist('statVars')
    if not stat_vars:
        return Response(json.dumps('error: must provide a statVars field'),
                        400,
                        mimetype='application/json')
    response = dc.fetch_data('/v1/stat/date/within-place',
                             req_json={
                                 'ancestor_place': ancestor_place,
                                 'childPlaceType': child_place_type,
                                 'stat_vars': stat_vars,
                             },
                             compress=False,
                             post=False,
                             has_payload=False)
    return Response(json.dumps(response), 200, mimetype='application/json')
Beispiel #5
0
def child_fetch(dcid):
    response = fetch_data(
        '/node/property-values',
        {
            'dcids': [dcid],
            'property': 'containedInPlace',
            'direction': 'in'
        },
        compress=False,
        post=True
    )
    places = response[dcid].get('in', [])
    dcid_str = '^'.join(sorted(map(lambda x: x['dcid'], places)))
    pop = json.loads(get_stats_wrapper(dcid_str, 'Count_Person'))

    pop = {
        dcid: stats.get('data', {}).get('2018', 0)
        for dcid, stats in pop.items() if stats
    }

    result = collections.defaultdict(list)
    for place in places:
        for place_type in place['types']:
            if place_type in WANTED_PLACE_TYPES:
                result[place_type].append({
                    'name': place['name'],
                    'dcid': place['dcid'],
                    'pop': pop.get(place['dcid'], 0)
                })
    # Filter equivalent place types
    for (preferred, equivalent) in EQUIVALENT_PLACE_TYPES.items():
        if preferred in result and equivalent in result:
            del result[equivalent]
    return result
Beispiel #6
0
def get_landing_page_data(dcid, new_stat_vars):
    response = dc_service.fetch_data('/landing-page', {
        'place': dcid,
        'newStatVars': new_stat_vars,
    },
                                     compress=False,
                                     post=True,
                                     has_payload=False)
    return response
Beispiel #7
0
def get_node(dcid):
    """Returns data given a disease node."""
    response = dc_service.fetch_data('/internal/bio', {
        'dcid': dcid,
    },
                                     compress=False,
                                     post=False,
                                     has_payload=False)
    return response
Beispiel #8
0
def get_landing_page_data_helper(dcid, stat_vars_string):
    data = {'place': dcid}
    if stat_vars_string:
        data['newStatVars'] = stat_vars_string.split('^')
    response = dc_service.fetch_data('/landing-page',
                                     data,
                                     compress=False,
                                     post=True,
                                     has_payload=False)
    return response
Beispiel #9
0
def stat_vars(dcid):
    """
    Get all the statistical variable dcids for a place.
    """
    response = fetch_data('/place/stat-vars', {
        'dcids': [dcid],
    },
                          compress=False,
                          post=False,
                          has_payload=False)
    return response['places'][dcid].get('statVars', [])
Beispiel #10
0
def get_property_value(dcid, prop):
    """Returns the property values for a given node dcid and property label."""
    response = fetch_data('/node/property-values', {
        'dcids': [dcid],
        'property': prop,
    },
                          compress=False,
                          post=False)
    result = {}
    result["property"] = prop
    result["values"] = response.get(dcid, {})
    return Response(json.dumps(result), 200, mimetype='application/json')
Beispiel #11
0
def stats_var_property_wrapper(dcids):
    """Function to get properties for given statistical variables."""
    data = dc.fetch_data('/node/triples', {
        'dcids': dcids,
    },
                         compress=False,
                         post=True)
    ranked_statvars = current_app.config['RANKED_STAT_VARS']
    result = {}
    # Get all the constraint properties
    for dcid, triples in data.items():
        pvs = {}
        for triple in triples:
            if triple['predicate'] == 'constraintProperties':
                pvs[triple["objectId"]] = ''
        pt = ''
        md = ''
        mprop = ''
        st = ''
        mq = ''
        name = dcid
        for triple in triples:
            predicate = triple['predicate']
            objId = triple.get('objectId', '')
            objVal = triple.get('objectValue', '')
            if predicate == 'measuredProperty':
                mprop = objId
            if predicate == 'populationType':
                pt = objId
            if predicate == 'measurementDenominator':
                md = objId
            if predicate == 'statType':
                st = objId
            if predicate == 'name':
                name = objVal
            if predicate == 'measurementQualifier':
                mq = objId
            if predicate in pvs:
                pvs[predicate] = objId if objId else objVal

        result[dcid] = {
            'mprop': mprop,
            'pt': pt,
            'md': md,
            'st': st,
            'mq': mq,
            'pvs': pvs,
            'title': name,
            'ranked': dcid in ranked_statvars
        }
    return result
Beispiel #12
0
def name():
    """Get place names."""
    dcids = request.args.getlist('dcid')
    response = fetch_data('/node/property-values', {
        'dcids': dcids,
        'property': 'name',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    result = {}
    for dcid in dcids:
        values = response[dcid].get('out')
        result[dcid] = values[0]['value'] if values else ''
    return json.dumps(result)
Beispiel #13
0
def index():
    response = fetch_data('/node/property-values', {
        'dcids': ['Country'],
        'property': 'typeOf',
        'direction': 'in'
    },
                          compress=False,
                          post=True)
    countries = {'Country': []}
    for place in response['Country']['in']:
        countries['Country'].append({
            'dcid': place['dcid'],
            'name': place.get('name', place['dcid'])
        })
        countries['Country'].sort(key=lambda x: x['name'])
    return render_template('placelist.html', place_by_type=countries)
Beispiel #14
0
def cached_i18n_name(dcids, locale, should_resolve_all):
    """Returns localization names for set of dcids.

    Args:
        dcids: ^ separated string of dcids. It must be a single string for the cache.
        locale: the desired localization language code.
        should_resolve_all: True if every dcid should be returned with a
                            name, False if only i18n names should be filled

    Returns:
        A dictionary of place names, keyed by dcid (potentially sparse if should_resolve_all=False)
    """
    if not dcids:
        return {}
    dcids = dcids.split('^')
    response = fetch_data('/node/property-values', {
        'dcids': dcids,
        'property': 'nameWithLanguage',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    result = {}
    dcids_default_name = []
    locales = i18n.locale_choices(locale)
    for dcid in dcids:
        values = response[dcid].get('out')
        # If there is no nameWithLanguage for this dcid, fall back to name.
        if not values:
            dcids_default_name.append(dcid)
            continue
        result[dcid] = ''
        for locale in locales:
            for entry in values:
                if has_locale_name(entry, locale):
                    result[dcid] = extract_locale_name(entry, locale)
                    break
            if result[dcid]:
                break
    if dcids_default_name:
        if should_resolve_all:
            default_names = cached_name('^'.join(sorted(dcids_default_name)))
        else:
            default_names = {}
        for dcid in dcids_default_name:
            result[dcid] = default_names.get(dcid, '')
    return result
Beispiel #15
0
def get_parent_place(dcid):
    response = fetch_data('/node/property-values', {
        'dcids': [dcid],
        'property': 'containedInPlace',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    parents = response[dcid].get('out', [])
    parents.sort(key=lambda x: x['dcid'], reverse=True)
    for i in range(len(parents)):
        if len(parents[i]['types']) > 1:
            parents[i]['types'] = [
                x for x in parents[i]['types']
                if not x.startswith('AdministrativeArea')
            ]
    return parents
Beispiel #16
0
def get_stat_vars_union(dcids):
    """Get all the statistical variable dcids for some places.

    Args:
        dcids: Place DCIDs separated by "^" as a single string.

    Returns:
        List of unique statistical variable dcids each as a string.
    """
    dcids = dcids.split("^")
    # The two indexings are due to how protobuf fields are converted to json
    return fetch_data('/place/stat-vars/union', {
        'dcids': dcids,
    },
                      compress=False,
                      post=True,
                      has_payload=False)['statVars']['statVars']
Beispiel #17
0
def child_fetch(dcid):
    response = fetch_data('/node/property-values', {
        'dcids': [dcid],
        'property': 'containedInPlace',
        'direction': 'in'
    },
                          compress=False,
                          post=True)
    places = response[dcid].get('in', [])
    result = collections.defaultdict(list)
    for place in places:
        for place_type in place['types']:
            if place_type in WANTED_PLACE_TYPES:
                result[place_type].append({
                    'name': place['name'],
                    'dcid': place['dcid'],
                })
                break
    for place in result:
        result[place].sort(key=lambda x: x['dcid'])
    return result
Beispiel #18
0
def get_stats_url_fragment(dcids):
    """Get stats information give multiple stats var dcids.

    The result is used as partial link to GNI.

    Args:
        dcids: A list of stats var dcids.
    Returns:
        An object keyed by stats dcid, with value being partial url that can
        be used by the /tools/timeline endpoint.
        {
            "Count_Person": "Person,count,gender,Female"
        }
    """
    data = dc.fetch_data('/node/triples', {
        'dcids': dcids,
    },
                         compress=False,
                         post=True)
    result = {}
    # Get all the constraint properties
    for dcid, triples in data.items():
        pvs = {}
        for triple in triples:
            if triple['predicate'] == 'constraintProperties':
                pvs[triple["objectId"]] = ''
        pop_type = ''
        mprop = ''
        for triple in triples:
            if triple['predicate'] == 'measuredProperty':
                mprop = triple['objectId']
            if triple['predicate'] == 'populationType':
                pop_type = triple['objectId']
            if triple['predicate'] in pvs:
                pvs[triple['predicate']] = triple['objectId']
        tokens = [pop_type, mprop]
        for p, v in pvs.items():
            tokens.extend([p, v])
        result[dcid] = ','.join(tokens)
    return result
Beispiel #19
0
def cached_name(dcids):
    """Returns display names for set of dcids.

    Args:
        dcids: ^ separated string of dcids. It must be a single string for the cache.

    Returns:
        A dictionary of display place names, keyed by dcid.
    """
    dcids = dcids.split('^')
    response = fetch_data('/node/property-values', {
        'dcids': dcids,
        'property': 'name',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    result = {}
    for dcid in dcids:
        values = response[dcid].get('out')
        result[dcid] = values[0]['value'] if values else ''
    return result