def accumulate(data):
    """Accumulate data by state returning a geojson object.

    The argument is expected to be structured as follows:
        [
            {
                "location_1": {
                    "city": "New York City, NY", # city, state pair
                },
                "key1": "value1",
                "key2": "value2",
                ...
            }
        ]

    The state is parsed from the `reporting_area` key.  If any error occurs,
    the record is skipped.  For the remaining keys, the
    value is coerced as a number and accumulated into the states' properties.
    Any error in the conversion causes the key to be skipped.  An additional
    key is added to each state, `num_records` indicating the number of records
    existed for that state.

    Finally, any state for which no record was encountered is filtered out of
    the returned value.

    :param list data: A list of dicts containing mappings of keys to counts
    :returns dict: A geojson object with ``properties`` containing accumulated
        values.  The top level ``FeatureCollection`` property contains a list
        of list of keys available in the individual features.
    """
    global _states
    if _states is None:
        _states = requests.get(states_source).json()
    geojson = copy.deepcopy(_states)

    # create a abbr -> properties mappin and remove unwanted properties
    states = {}
    for feature in geojson.get('features', []):
        abbr = feature['properties']['abbr'].upper()
        name = feature['properties']['name'].upper()
        feature['properties'].clear()
        feature['properties']['abbr'] = abbr
        feature['properties']['name'] = name
        states[name] = feature['properties']

    # store all properties encountered here
    all_props = set()

    # loop over records
    for record in data:
        m = (record.get('location_1', {}).get('human_address', {})
             .get('state', '').upper())
        if m in states:  # it is a valid state

            props = states[m]
            props['num_records'] = props.get('num_records', 0) + 1

            for key, value in record.iteritems():
                if key in ignored_properties:
                    continue

                try:
                    v = float(value)
                except Exception:
                    v = float('NaN')

                # check that it's a real value
                if math.isnan(v) or math.isinf(v):
                    continue

                if v < 0:  # maybe this is a valid case?
                    print('Encountered a negative value!')

                # accumulate the field as a property
                props[key] = props.get(key, 0) + v

                # add the property to the global list of properties found
                all_props.add(key)

    # store the global list of properties
    geojson['properties'] = {
        'values': list(all_props)
    }
    return geojson
dataset/ETHPy150Open Kitware/minerva/server/jobs/soda_import_worker.py/accumulate/original