autofit.Aggregator#

class autofit.Aggregator(session: ~sqlalchemy.orm.session.Session, filename: ~typing.Optional[str] = None, predicate: ~autofit.database.query.query.abstract.AbstractQuery = <NullPredicate SELECT id FROM fit>, offset=0, limit=None, order_bys=None, top_level_only=True)[source]#

Bases: AbstractAggregator

Query results from an intermediary SQLite database.

Results can be scraped from a directory structure and stored in the database.

Parameters:
  • session – A session for communicating with the database.

  • filename – The path to the database file. If None, the database is in memory.

  • predicate – A predicate to filter the results by.

  • offset – The number of results to skip

  • limit – The maximum number of results to return

  • order_bys – A list of attributes to order the results by

  • top_level_only – If True, only return the top level fits

Methods

add_directory

Recursively search a directory for autofit results and add them to this database.

child_values

Retrieve the value associated with each fit with the given parameter name

from_database

Create an instance from a sqlite database file.

grid_searches

Filter to only grid searches and return an aggregator with grid search specific functionality.

map

order_by

Order the results by a given attribute of the search.

query

Apply a query on the model.

values

Retrieve the value associated with each fit with the given parameter name

Attributes

fits

Lazily query the database for a list of Fit objects that match the aggregator's predicate.

info

Query info associated with the fit in the info dictionary

model

Facilitates query construction.

search

An object facilitating queries on fit attributes such as: name unique_tag path_prefix is_complete is_grid_search

order_by(item: Attribute, reverse=False) Aggregator[source]#

Order the results by a given attribute of the search. Can be applied multiple times with the first application taking precedence.

Parameters:
  • item – An attribute of the search

  • reverse – If True reverse the results

Return type:

An aggregator with ordering applied

Examples

aggregator = aggregator.order_by(

aggregator.search.unique_tag

)

property search: FitQuery#

An object facilitating queries on fit attributes such as: name unique_tag path_prefix is_complete is_grid_search

property info#

Query info associated with the fit in the info dictionary

property fits: List[Fit]#

Lazily query the database for a list of Fit objects that match the aggregator’s predicate.

property model: Query#

Facilitates query construction. If the Fit class has an attribute with the given name then a predicate is generated based on that attribute. Otherwise the query is assumed to apply to the best fit instance.

Return type:

A query

__call__(predicate) Aggregator[source]#

Concise query syntax

query(predicate: AbstractQuery) Aggregator[source]#

Apply a query on the model.

Parameters:

predicate – A predicate constructed to express which models should be included.

Return type:

A list of objects that match the predicate

Examples

>>>
>>> aggregator = Aggregator.from_database(
>>>     "my_database.sqlite"
>>> )
>>>
>>> lens = aggregator.galaxies.lens
>>>
>>> aggregator.filter((lens.bulge == SersicCore) & (lens.disk == Sersic))
>>> aggregator.filter((lens.bulge == SersicCore) | (lens.disk == Sersic))
add_directory(directory: str, auto_commit=True, reference: Optional[dict] = None, completed_only: bool = False)[source]#

Recursively search a directory for autofit results and add them to this database.

Any pickles found in the pickles file are implicitly added to the fit object.

Warning

If a directory is added twice then that will result in duplicate entries in the database.

Parameters:
  • auto_commit – If True the session is committed writing the new objects to the database

  • directory – A directory containing autofit results embedded in a file structure

  • reference – A dictionary mapping the names of objects in the model to their class path.

  • completed_only – If true only searches that have completed are added

classmethod from_database(filename: str, completed_only: bool = False, top_level_only: bool = True) Aggregator[source]#

Create an instance from a sqlite database file.

If no file exists then one is created with the schema of the database.

Parameters:
  • completed_only – If True only completed fits are returned

  • filename – The name of the database file.

  • top_level_only – If True only top level fits are returned

Return type:

An aggregator connected to the database specified by the file.

grid_searches() GridSearchAggregator[source]#

Filter to only grid searches and return an aggregator with grid search specific functionality.

Grid searches are initially implicitly ordered by their id