labbookdb.db package

Submodules

labbookdb.db.add module

labbookdb.db.add.add_generic(db_path, parameters, session=None, engine=None)[source]

Adds new entries based on a LabbookDB-syntax parameter dictionary.

Parameters:
  • db_path (str) – Path to database to open if session and engine parameters are not already passed, can be relative or use tilde to specify the user $HOME.
  • parameters (str or dict) – A LabbookDB-style dictionary (or JSON interpretable as dictionary), where keys are “CATEGORY” and other strings specifying the attribute names for the object to be created, and values are the class name (for “CATEGORY”) and either the values to assign (verbatim: string, int, or float) or LabbookDB-syntax strings specifying a related entry, or a list of LabbookDB-syntax strings specifying related entries, or a list of LabbookDB-style dictionaries specifying new entries to be created and linked.
  • session (sqlalchemy.orm.session.Session, optional) – Session instance, as created with labbookdb.db.add.load_session().
  • engine (sqlalchemy.engine.Engine, optional) – Engine instance correponding to the Session instance under session, as created with labbookdb.db.add.load_session().
Returns:

  • myobject (object) – LabbookDB object with SQLAlchemy-compatible attributes (e.g. as found under labbookdb.db.common_classes).
  • object_id (int) – Value of myobject.id attribute.

labbookdb.db.add.add_to_db(session, engine, myobject)[source]

Add an object to session and return the .id attribute value.

Parameters:
  • session (sqlalchemy.orm.session.Session) – Session instance, as created with labbookdb.db.add.load_session().
  • engine (sqlalchemy.engine.Engine) – Engine instance correponding to the Session instance under session, as created with labbookdb.db.add.load_session().
  • myobject (object) – LabbookDB object with SQLAlchemy-compatible attributes (e.g. as found under labbookdb.db.common_classes).
Returns:

object_id – Value of myobject.id attribute

Return type:

int

labbookdb.db.add.append_parameter(db_path, entry_identification, parameters)[source]

Assigns a value to a given parameter of a given entry.

Parameters:
  • db_path (str) – A string especifying the database path
  • entry_identification (str) – A LabbookDB syntax string specifying an instance of an object for which to update a parameter. Example strings: “Animal:external_ids.AnimalExternalIdentifier:database.ETH/AIC&#&identifier.5701” , “Cage:id.14”
  • parameters (str or dict) – A LabbookDB-style dictionary (or JSON interpretable as dictionary), where keys are strings giving the names of attributes of the class selected by entry_identification, and values are either the values to assign (verbatim: string, int, or float) or LabbookDB-syntax strings specifying a related entry, or a list of LabbookDB-syntax strings specifying related entries, or a list of LabbookDB-style dictionaries specifying new entries to be created and linked.
labbookdb.db.add.commit_and_close(session, engine)[source]

Commit and close session and dispose of engine. Nonfatal for sqlalchemy.exc.IntegrityError with print notification.

Parameters:
  • session (sqlalchemy.orm.session.Session, optional) – Session instance, as created with labbookdb.db.add.load_session().
  • engine (sqlalchemy.engine.Engine, optional) – Engine instance correponding to the Session instance under session, as created with labbookdb.db.add.load_session().

Return the .id attribute value for existing entries matched by a string following the LabbookDB-syntax.

Parameters:
  • session (sqlalchemy.orm.session.Session) – Session instance, as created with labbookdb.db.add.load_session().
  • engine (sqlalchemy.engine.Engine) – Engine instance correponding to the Session instance under session, as created with labbookdb.db.add.load_session().
  • parameters (str) – LabbookDB-syntax string specifying an existing entry.
Returns:

  • ids (list of int) – .id attribute values for the entries matched by the LabbookDB-syntax string.
  • sql_query (sqlalchemy.orm.query.Query) – Query corresponding to the LabbookDB-syntax string

Examples

>>> from labbookdb.db import add
>>> session, engine = add.load_session("lala.db")
>>> add.get_related_ids(s,e,"Animal:external_ids.AnimalExternalIdentifier:database.ETH/AIC/cdb&#&identifier.275511")
BaseException: No entry was found with a value of "275511" on the "identifier" column of the "AnimalExternalIdentifier" CATEGORY, in the database.
>>> add.get_related_ids(s,e,"Animal:external_ids.AnimalExternalIdentifier:database.ETH/AIC/cdb&&identifier.275511")
BaseException: No entry was found with a value of "ETH/AIC/cdb" on the "database" column of the "AnimalExternalIdentifier" CATEGORY, in the database.

Notes

Recursivity :
This function calls itself recursively in order to get the .id attribute values of related entries (and related entries of related entries, etc.) specified in the LabbookDB-syntax string. Multiple constraints are separated by double ampersands which may be separated by none or up to two hashtags, to specify at which level the constrains tshould be applied to. One hashtag is removed on each recursion step, and the constraint is only evaluated when there are no hashtags left. “Animal:external_ids.AnimalExternalIdentifier:database.ETH/AIC/cdb&#&identifier.275511” will look for both the database and the identifier attributes in the AnimalExternalIdentifier class, while “Animal:external_ids.AnimalExternalIdentifier:database.ETH/AIC/cdb&#&identifier.275511” will look for the database attribute on the AnimalExternalIdentifier class, and for the identifier attribute on the Animal class.
labbookdb.db.add.instructions(kind)[source]

Print helpful instructions for common error-causing input issues

Parameters:kind ({"table_identifier",}) – Shorthand for the instructin message to be printed.
labbookdb.db.add.load_session(db_path)[source]

Load and return a new SQLalchemy session and engine.

Parameters:db_path (str) – Path to desired database location, can be relative or use tilde to specify the user $HOME.
Returns:
  • session (sqlalchemy.orm.session.Session) – Session instance.
  • engine (sqlalchemy.engine.Engine) – Engine instance.

labbookdb.db.base_classes module

class labbookdb.db.base_classes.Biopsy(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

animal_id
extraction_protocol
extraction_protocol_id
fluorescent_microscopy
id
sample_location
start_date
type
class labbookdb.db.base_classes.FMRIScannerSetup(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
coil
id
resonator
scanner
support
class labbookdb.db.base_classes.Genotype(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
construct
id
zygosity
class labbookdb.db.base_classes.Irregularity(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

description
id
class labbookdb.db.base_classes.Measurement(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

animal_id
cage_id
date
id
irregularities
operator
operator_id
type
class labbookdb.db.base_classes.MeasurementUnit(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
id
long_name
siunitx
class labbookdb.db.base_classes.Operator(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

affiliation
code
email
full_name
id
validate_email(key, address)[source]
class labbookdb.db.base_classes.OpticFiberImplant(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

angle
cannula_diameter
code
ferrule_diameter
id
length
long_code
manufacturer
manufacturer_code
numerical_apperture
transmittance
class labbookdb.db.base_classes.OrthogonalStereotacticTarget(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
depth
id
leftright
pitch
posteroanterior
qualitative_depth_reference
reference
roll
superoinferior
yaw
class labbookdb.db.base_classes.Protocol(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

authors
code
id
name
type
class labbookdb.db.base_classes.StimulationEvent(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

duration
frequency
id
onset
pulse_width
strength
target
trial_type
unit
unit_id
wavelength
class labbookdb.db.base_classes.Virus(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

addgene_identifier
capsid
code
concentration
credit
id
plasmid_summary
source

labbookdb.db.common_classes module

class labbookdb.db.common_classes.AnesthesiaProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
bolus
bolus_to_maintenance_delay
code
id
induction
maintenance
name
recovery_bolus
respiration
type
class labbookdb.db.common_classes.Animal(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

biopsies
birth_date
cage_stays
death_date
death_reason
ear_punches
external_ids
genotypes
id
license
maximal_severtity
measurements
observations
operations
sex
treatments
class labbookdb.db.common_classes.AnimalExternalIdentifier(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

animal_id
database
id
identifier
class labbookdb.db.common_classes.Arena(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
id
measurements
name
shape
wall_color
x_dim
y_dim
z_dim
class labbookdb.db.common_classes.BrainBiopsy(**kwargs)[source]

Bases: labbookdb.db.base_classes.Biopsy

animal_id
data_path
extraction_protocol
extraction_protocol_id
fluorescent_microscopy
id
sample_location
sectioning_protocol
sectioning_protocol_id
start_date
type
class labbookdb.db.common_classes.BrainExtractionProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
code
fixation_solution
fixation_solution_id
fixation_solution_volume
flushing_solution
flushing_solution_id
flushing_solution_volume
id
name
perfusion_flow
perfusion_system
peristaltic_frequency
post_extraction_fixation_time
storage_solution
storage_solution_id
storage_solution_volume
storage_time
type
class labbookdb.db.common_classes.Cage(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

environmental_enrichment
handling_habituations
id
id_local
location
measurements
stays
treatments
class labbookdb.db.common_classes.CageStay(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

cage
cage_id
id
report_animals()[source]
single_caged
start_date
class labbookdb.db.common_classes.DNAExtraction(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
id
protocol
protocol_id
source
source_id
class labbookdb.db.common_classes.DNAExtractionProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
centrifugation
centrifugation_id
code
cooling
cooling_id
digestion
digestion_buffer
digestion_buffer_id
digestion_buffer_volume
digestion_id
id
inactivation
inactivation_id
lysis
lysis_buffer
lysis_buffer_id
lysis_buffer_volume
lysis_id
mass_unit
mass_unit_id
name
proteinase
proteinase_id
proteinase_volume
sample_mass
type
volume_unit
volume_unit_id
class labbookdb.db.common_classes.DrinkingMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
cage_id
consumption
date
end_amount
id
irregularities
operator
operator_id
reference_date
start_amount
type
class labbookdb.db.common_classes.Evaluation(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

author
author_id
id
measurement_id
path
class labbookdb.db.common_classes.FMRIMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

anesthesia
anesthesia_id
animal_id
cage_id
data_path
date
id
irregularities
operator
operator_id
scanner_setup
scanner_setup_id
stimulations
temperature
type
class labbookdb.db.common_classes.FluorescentMicroscopyMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
biopsy_id
cage_id
data
date
exposure
id
imaged_wavelength
irregularities
light_source
operator
operator_id
stimulation_wavelength
type
class labbookdb.db.common_classes.ForcedSwimTestMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
cage_id
data_path
date
evaluations
id
irregularities
operator
operator_id
recording_bracket
temperature
type
class labbookdb.db.common_classes.HandlingHabituation(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

cage
cage_id
date
id
protocol
protocol_id
class labbookdb.db.common_classes.HandlingHabituationProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
code
group_picking_up
id
individual_picking_up
name
session_duration
transparent_tube
type
class labbookdb.db.common_classes.Incubation(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

duration
duration_unit
duration_unit_id
id
movement
revolutions_per_minute
temperature
temperature_unit
temperature_unit_id
class labbookdb.db.common_classes.Ingredient(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

concentration
concentration_unit
concentration_unit_id
id
substance
substance_id
class labbookdb.db.common_classes.Observation(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

animal_id
behaviour
date
id
operator
operator_id
physiology
severtity
unit
unit_id
value
class labbookdb.db.common_classes.OpenFieldTestMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
arena_id
cage_id
center_luminostiy
corner_luminostiy
data_path
date
edge_luminostiy
evaluations
id
irregularities
operator
operator_id
type
class labbookdb.db.common_classes.Operation(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

anesthesia
anesthesia_id
animal_id
date
id
irregularities
operator
operator_id
protocols
class labbookdb.db.common_classes.OpticFiberImplantProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
code
id
name
optic_fiber_implant
optic_fiber_implant_id
stereotactic_target
stereotactic_target_id
type
class labbookdb.db.common_classes.SectioningProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
blade_frequency
blade_speed
code
id
name
slice_thickness
start_bregma_distance
start_depth
start_interaural_distance
start_lambda_distance
start_midline_distance
system
type
class labbookdb.db.common_classes.Solution(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
contains
id
name
supplier
supplier_product_code
class labbookdb.db.common_classes.StimulationProtocol(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
events
id
name
class labbookdb.db.common_classes.Substance(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

code
concentration
concentration_unit
concentration_unit_id
id
long_name
name
pubchem_sid
supplier
supplier_product_code
class labbookdb.db.common_classes.SucrosePreferenceMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
cage_id
concentration_unit
concentration_unit_id
date
id
irregularities
operator
operator_id
reference_date
sucrose_bottle_position
sucrose_concentration
sucrose_end_amount
sucrose_start_amount
type
water_end_amount
water_start_amount
class labbookdb.db.common_classes.Treatment(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Base

end_date
id
protocol
protocol_id
start_date
class labbookdb.db.common_classes.TreatmentProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

authors
code
dose
dose_unit
dose_unit_id
frequency
id
name
rate
rate_unit
rate_unit_id
route
solution
solution_id
type
class labbookdb.db.common_classes.VirusInjectionProtocol(**kwargs)[source]

Bases: labbookdb.db.base_classes.Protocol

amount
authors
code
id
name
stereotactic_target
stereotactic_target_id
type
virus
virus_diffusion_time
virus_id
virus_injection_speed
class labbookdb.db.common_classes.WeightMeasurement(**kwargs)[source]

Bases: labbookdb.db.base_classes.Measurement

animal_id
cage_id
date
id
irregularities
operator
operator_id
type
weight
weight_unit
weight_unit_id

labbookdb.db.query module

labbookdb.db.query.add_all_columns(cols, class_name)[source]
labbookdb.db.query.animal_info(identifier, database, db_path=None)[source]

Return the __str__ attribute of an Animal object query filterd by the id column OR by arguments of the external_id objects.

Parameters:
  • db_path (string) –
  • to a LabbookDB formatted database. (Path) –
  • identifier (int or string) –
  • identifier of the animal (The) –
  • database (string or None, optional) –
  • specified gives a constraint on the AnimalExternalIdentifier.database column AND truns the identifier attribute into a constraint on the AnimalExternalIdentifier.identifier column. If unspecified, the identfier argument is used as a constraint on the Animal.id column. (If) –
labbookdb.db.query.cage_info(db_path, identifier)[source]

Return the __str__ attribute of an Animal object query filterd by the id column OR by arguments of the external_id objects.

Parameters:
  • db_path (string) –
  • to a LabbookDB formatted database. (Path) –
  • identifier (int or string) –
  • identifier of the animal (The) –
  • database (string or None, optional) –
  • specified gives a constraint on the AnimalExternalIdentifier.database colun AND truns the identifier attribute into a constraint on the AnimalExternalIdentifier.identifier column. If unspecified, the identfier argument is used as a constraint on the Animal.id column. (If) –
labbookdb.db.query.commit_and_close(session, engine)[source]
labbookdb.db.query.get_df(db_path, col_entries=[], default_join='inner', filters=[], join_entries=[], join_types=[])[source]

Return a dataframe from a complex query of a LabbookDB-style database

Parameters:
  • db_path (string) – Path to database file.
  • col_entries (list) – A list of tuples containing the columns to be queried: * 1-tuples indicate all attributes of a class are to be queried * 2-tuples indicate only the attribute specified by the second element, of the class specified by the first element is to be queried * 3-tuples indicate that an aliased class of the type given by the second element is to be created and named according to the first and second elements, separated by an underscore. The attribute given by the third element will be queried; if the thid element is empty, all attributes will be queried
  • join_entries (list) – A list of tuples specifying the desired join operations: * 1-tuples give the join * 2-tuples give the class to be joined on the first element, and the explicit relationship (attribute of another class) on the second element If any of the elements contains a period, the expression will be evaluated as a class (preceeding the period) attribute (after the period)$
  • filters (list) – A list of lists giving filters for the query. In each sub-list the first and second elements give the class and attribute to be matched. Every following element specifies a possible value for the class attribute (implemented as inclusive disjunction). If the attribute name ends in “date” the function computes datetime objects from the subsequent strings containing numbers separated by commas.
  • documentation (!!!incomplete) –

Examples

>>> col_entries=[
                ("Animal","id"),
                ("Treatment",),
                ("FMRIMeasurement",),
                ("TreatmentProtocol","code"),
                ("Cage","id"),
                ("Cage","Treatment",""),
                ("Cage","TreatmentProtocol","code")
                ]
>>> join_entries=[
                ("Animal.treatments",),
                ("FMRIMeasurement",),
                ("Treatment.protocol",),
                ("Animal.cage_stays",),
                ("CageStay.cage",),
                ("Cage_Treatment","Cage.treatments"),
                ("Cage_TreatmentProtocol","Cage_Treatment.protocol")
                ]
>>> filters = [["Cage_Treatment","start_date","2016,4,25,19,30"]]
>>> reference_df = get_df("~/syncdata/meta.db", col_entries=col_entries, join_entries=join_entries, filters=filters)
labbookdb.db.query.get_for_protocolize(db_path, class_name, code)[source]

Return a dataframe containing a specific entry from a given class name, joined with its related tables up to three levels down.

labbookdb.db.query.load_session(db_path)[source]

labbookdb.db.utils module

labbookdb.db.utils.arange_by_date(attribute)[source]
labbookdb.db.utils.dt_format(dt)[source]

Module contents