def add_document_and_foreign_keys( database: Database, ) -> Tuple[Document, Patient, Session, SensorInfo]: session = add_session(database) patient = add_patient(database) sensor_info = add_sensor_info(database) with database.session_scope() as s: document = Document( session_id=session.session_id, patient_id=patient.patient_id, sensor_serial_number=sensor_info.sensor_serial_number, distractor_type=Config.DEFAULT_DISTRACTOR, ) s.add(document) return document, patient, session, sensor_info
#!/usr/bin/env python import argparse import pandas as pd from pathlib import Path from cranio.model import Database, session_scope, Base, Measurement from cranio.utils import logger, configure_logging parser = argparse.ArgumentParser() parser.add_argument('path', help='Path to SQLite file (.db)', type=str) if __name__ == '__main__': configure_logging() args = parser.parse_args() path = Path(args.path) database = Database(drivername='sqlite', database=str(path)) database.create_engine() with session_scope(database) as s: for table_name, table in Base.metadata.tables.items(): path_out = path.parent / (table_name + '.csv') logger.info(f'Read table {table_name} from {database.url} to {path_out}') df = pd.read_sql_table(table_name, con=database.engine) df.to_csv(path_out, sep=';', index=False)
def select_most_recently_used_patient(self, database: Database): with database.session_scope() as s: patient = (s.query(Patient).join(Document).join(Session).order_by( Session.started_at.desc()).first()) if patient is not None: self.select_patient(patient_id=patient.patient_id)
def enter_info_to_database(cls, database: Database) -> SensorInfo: """ Enter copy of self.sensor_info to a database. """ logger.debug(f'Enter sensor info: {str(cls.sensor_info)}') database.insert(cls.sensor_info, insert_if_exists=False) return cls.sensor_info
def add_sensor_info(database: Database) -> SensorInfo: with database.session_scope() as s: sensor_info = SensorInfo(sensor_serial_number='pytest', turns_in_full_turn=3) s.add(sensor_info) return sensor_info
def add_patient(database: Database) -> Patient: with database.session_scope() as s: patient = Patient(patient_id=generate_unique_id()) s.add(patient) return patient
def add_session(database: Database) -> Session: with database.session_scope() as s: session = Session() s.add(session) return session
def database_fixture(): database = Database(drivername='sqlite') database.create_engine() database.init() yield database database.clear()