collate

Aggregation SQL Query Builder

https://github.com/dssg/collate

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 7 committers (14.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.5%) to scientific vocabulary

Keywords

triage
Last synced: 9 months ago · JSON representation

Repository

Aggregation SQL Query Builder

Basic Info
  • Host: GitHub
  • Owner: dssg
  • License: other
  • Language: Python
  • Default Branch: master
  • Size: 398 KB
Statistics
  • Stars: 1
  • Watchers: 27
  • Forks: 3
  • Open Issues: 8
  • Releases: 0
Topics
triage
Created over 9 years ago · Last pushed over 3 years ago
Metadata Files
Readme Changelog Contributing License

README.rst

=======
collate
=======


.. image:: https://img.shields.io/pypi/v/collate.svg
        :target: https://pypi.python.org/pypi/collate

.. image:: https://travis-ci.org/dssg/collate.svg?branch=master
        :target: https://travis-ci.org/dssg/collate

.. image:: https://readthedocs.org/projects/collate/badge/?version=latest
        :target: https://collate.readthedocs.io/en/latest/?badge=latest
        :alt: Documentation Status

.. image:: https://pyup.io/repos/github/dssg/collate/shield.svg
     :target: https://pyup.io/repos/github/dssg/collate/
     :alt: Updates

.. image:: https://codecov.io/gh/dssg/collate/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/dssg/collate
    :alt: Code Coverage


Aggregated feature generation made easy.

* Free software for noncommercial use: `UChicago open source license `_.
* Documentation: https://collate.readthedocs.io.

**Note**: Collate is now a bundled component of `Triage `_, and future development will take place there. To utilize collate within your custom pipeline, you may still import it from there.

----

Overview
========

Collate allows you to easily specify and execute statements like “find the number of restaurants in a given zip code that have had food safety violations within the past year.”  The real power is that it allows you to vary both the spatial and temporal windows, choosing not just zip code and one year, but a range over multiple partitions and times. Specifying features is also easier and more efficient than writing raw sql. Collate will automatically generate and execute all the required SQL scripts to aggregate the data across many groups in an efficient manner. We mainly use the results as features in machine learning models.

Inputs
======

Take for example `food inspections data from the City of Chicago `_. The table looks like this:


============= =========== ===== =============== ========== =========== ===
inspection_id license_no  zip   inspection_date results    violations  ...
============= =========== ===== =============== ========== =========== ===
1966765       80273       60636 2016-10-18      No Entry               ...
1966314       2092894     60640 2016-10-11      Pass       …CORRECTED… ...
1966286       2215628     60661 2016-10-11      Pass w/ C… …HAZARDOUS… ...
1966220       2424039     60620 2016-10-07      Pass                   ...
============= =========== ===== =============== ========== =========== ===

There are two spatial levels in the data: the specific restaurant (by its license number) and the zip code. And there is a date.

An example of an aggregate feature is the number of failed inspections. In raw SQL this could be calculated, for each restaurant, as so::

    SELECT license_no, sum((results = 'Fail')::int) as failed_sum
    FROM food_inspections GROUP BY license_no;

In collate, this aggregated column would be defined as::

    Aggregate({"failed": "(results = 'Fail')::int"}, "sum", {'coltype':'aggregate', 'all': {'type': 'mean'}})

Note that the SQL query is split into two parts: the first argument to ``Aggregate``
is the computation to be performed and gives it a name (as a dictionary key), and
the second argument is the reduction function to perform. The third argument provides
a set of rules for how to handle imputation of null values in the resulting fields.

Splitting the SQL like this makes it easy to generate lots of composable features 
as the outer product of these two lists.  For example, you may also be interested 
in the proportion of inspections that resulted in a failure in addition to the 
total number. This is easy to specify with the average value of the `failed` 
computation::

    Aggregate({"failed": "(results = 'Fail')::int"}, ["sum","avg"], {'coltype':'aggregate', 'all': {'type': 'mean'}})


Aggregations in collate easily aggregate this single feature across different spatiotemporal groups, e.g.::

    Aggregate({"failed": "(results = 'Fail')::int"}, ["sum","avg"], {'coltype':'aggregate', 'all': {'type': 'mean'}})
    st = SpacetimeAggregation([fail],
	                           from_obj='food_inspections',
                               groups=['license_no','zip'],
                               intervals={"license_no":["2 year", "3 year"], "zip": ["1 year"]},
                               dates=["2016-01-01", "2015-01-01"],
                               date_column="inspection_date",
                               state_table='all_restaurants',
                               state_group='license_no',
                               schema='test_collate')

The ``SpacetimeAggregation`` object encapsulates the ``FROM`` section of the query
(in this case it's simply the inspections table), as well as the ``GROUP BY``
columns.  Not only will this create information about the individual restaurants
(grouping by ``license_no``), it also creates "neighborhood" columns that add
information about the region in which the restaurant is operating (by grouping by
``zip``). The ``state_table`` specified here should contain the comprehensive set of
``state_group`` entities and dates for which output should be generated for them,
regardless if they exist in the ``from_obj``.

Even more powerful is the sophisticated date range partitioning that the
``SpacetimeAggregation`` object provides.  It will create multiple queries in
order to create the summary statistics over the past 1, 2, or 3 years, looking
back from either Jan 1, 2015 or Jan 1 2016. Executing this set of queries with::

    st.execute(engine.connect()) # with a SQLAlchemy engine object

will create four new tables in the ``test_collate`` schema. The table
``food_inspections_license_no`` will contain four feature columns for each
license that describe the total number and proportion of failures over the past
two or three years, with a date column that states whether it was looking
before 2016 or 2015. Similarly, a ``food_inspections_zip`` table will have two
feature columns for every zip code in the database, looking at the total and
average number of failures in that neighborhood over the year prior to the date
in the date column. The ``food_inspections_aggregation`` table joins these results 
together to make it easier to look at both neighborhood and restaurant-level 
effects for any given restaurant. Finally, the ``food_inspections_aggregation_imputed``
table fills in null values using the imputation rules specified in the ``Aggregate``
constructor.

Imputation Rules
================

Imputation rules should be specified in the form of a dictionary::

    {
        'coltype': 'aggregate',
        'all': {'type': 'mean'},
        'max': {'type': 'constant', 'value': 137}
    }

The ``coltype`` key of this dictionary must be one of ``aggregate``, ``categorical``, 
or ``array_categorical`` and informs how the imputation rules are applied.

The other keys of the dictionary are the reduction functions used by the aggregate
(such as ``sum``, ``count``, ``avg``, etc.) or ``all`` as a catch-all. Function-specific
rules will take precedence over the catch-all rule. The values associated with these
keys are each a dictionary with a required ``type`` key specifying the rule type and
other rule-specific keys.

Currently available imputation rules:
    * ``mean``: The average value of the feature (for ``SpacetimeAggregation`` the mean is taken within-date).
    * ``constant``: Fill with a constant value from a required ``value`` parameter.
    * ``zero``: Fill with zero.
    * ``zero_noflag``: Fill with zero without generating an "imputed" flag. This option should be used only for cases where null values are explicitly known to be zero such as absence of an entity from an events table indicating that no such event has occurred.
    * ``null_category``: Only available for categorical features. Just flag null values with the null category column.
    * ``binary_mode``: Only available for aggregate column types. Takes the modal value for a binary feature.
    * ``error``: Raise an exception if any null values are encountered for this feature.

Outputs
=======

The main output of a collate aggregation is a database table with all of the aggregated features joined to a list of entities.


TODO: sample rows from the above aggregation.


Usage Examples
==============

Multiple quantities
~~~~~~~~~~~~~~~~~~~
TODO

Multiple functions
~~~~~~~~~~~~~~~~~~
TODO

Tuple quantity
~~~~~~~~~~~~~~
TODO

Date substitution
~~~~~~~~~~~~~~~~~
TODO

Categorical counts
~~~~~~~~~~~~~~~~~~
TODO

Naming of features
~~~~~~~~~~~~~~~~~~
TODO

More complicated from_obj
~~~~~~~~~~~~~~~~~~~~~~~~~
TODO

Technical details
=================

Owner

  • Name: Data Science for Social Good
  • Login: dssg
  • Kind: organization
  • Email: info@datascienceforsocialgood.org
  • Location: Pittsburgh, PA

GitHub Events

Total
Last Year

Committers

Last synced: over 2 years ago

All Time
  • Total Commits: 163
  • Total Committers: 7
  • Avg Commits per committer: 23.286
  • Development Distribution Score (DDS): 0.564
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Eric Potash e****c@k****t 71
Matt Bauman m****n@g****m 42
pyup.io bot g****t@p****o 39
Matt Bauman m****n@u****u 5
Tristan Crockett t****t@g****m 4
Kit Rodolfa s****k@g****m 1
Jesse London j****n@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 19
  • Total pull requests: 81
  • Average time to close issues: 5 months
  • Average time to close pull requests: 13 days
  • Total issue authors: 8
  • Total pull request authors: 8
  • Average comments per issue: 1.42
  • Average comments per pull request: 1.68
  • Merged pull requests: 44
  • Bot issues: 0
  • Bot pull requests: 6
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mbauman (6)
  • thcrock (4)
  • shaycrk (3)
  • rayidghani (2)
  • harnav (1)
  • nanounanue (1)
  • avishekrk (1)
  • potash (1)
Pull Request Authors
  • pyup-bot (51)
  • mbauman (10)
  • potash (9)
  • dependabot[bot] (6)
  • shaycrk (2)
  • jesteria (1)
  • kenben (1)
  • thcrock (1)
Top Labels
Issue Labels
enhancement (2) bug (1) question (1)
Pull Request Labels
dependencies (6)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 191 last-month
  • Total dependent packages: 1
  • Total dependent repositories: 1
  • Total versions: 4
  • Total maintainers: 1
pypi.org: collate

Aggregated feature generation made easy.

  • Versions: 4
  • Dependent Packages: 1
  • Dependent Repositories: 1
  • Downloads: 191 Last month
Rankings
Dependent packages count: 4.7%
Forks count: 16.8%
Average: 19.8%
Dependent repos count: 21.7%
Downloads: 23.9%
Stargazers count: 31.9%
Maintainers (1)
Last synced: 10 months ago

Dependencies

requirements.txt pypi
  • SQLAlchemy ==1.1.11
requirements_dev.txt pypi
  • PyYAML ==3.12 development
  • SQLAlchemy ==1.1.11 development
  • Sphinx ==1.6.3 development
  • bumpversion ==0.5.3 development
  • codecov ==2.0.9 development
  • coverage ==4.4.1 development
  • cryptography ==1.9 development
  • csvkit ==1.0.2 development
  • flake8 ==3.3.0 development
  • pandas * development
  • pip ==9.0.1 development
  • psycopg2 ==2.7.1 development
  • pytest ==3.1.3 development
  • pytest-cov ==2.5.1 development
  • testing.postgresql ==1.3.0 development
  • tox ==2.7.0 development
  • watchdog ==0.8.3 development
  • wheel ==0.29.0 development