From a65ca6b5d3b5957c6598dd651e479de1ea545cc6 Mon Sep 17 00:00:00 2001 From: Maxime Mulder Date: Fri, 25 Apr 2025 02:03:31 +0000 Subject: [PATCH] delete unused deprecated code --- python/lib/database_lib/mri_scanner.py | 158 ------------------ python/lib/database_lib/mri_upload_db.py | 88 ---------- python/lib/database_lib/notification.py | 93 ----------- python/lib/database_lib/project_cohort_rel.py | 60 ------- python/lib/database_lib/session_db.py | 129 -------------- python/lib/database_lib/site.py | 51 ------ python/lib/database_lib/tarchive.py | 89 ---------- python/lib/database_lib/tarchive_series.py | 57 ------- python/lib/database_lib/visit_windows.py | 49 ------ python/lib/dicom_archive.py | 122 -------------- python/lib/imaging.py | 43 ----- python/lib/imaging_io.py | 123 -------------- python/lib/imaging_upload.py | 94 ----------- python/lib/log.py | 114 ------------- python/lib/session.py | 102 ----------- python/lib/utilities.py | 37 ---- 16 files changed, 1409 deletions(-) delete mode 100644 python/lib/database_lib/mri_scanner.py delete mode 100644 python/lib/database_lib/mri_upload_db.py delete mode 100644 python/lib/database_lib/notification.py delete mode 100644 python/lib/database_lib/project_cohort_rel.py delete mode 100644 python/lib/database_lib/session_db.py delete mode 100644 python/lib/database_lib/site.py delete mode 100644 python/lib/database_lib/tarchive.py delete mode 100644 python/lib/database_lib/tarchive_series.py delete mode 100644 python/lib/database_lib/visit_windows.py delete mode 100644 python/lib/dicom_archive.py delete mode 100644 python/lib/imaging_io.py delete mode 100644 python/lib/imaging_upload.py delete mode 100644 python/lib/log.py diff --git a/python/lib/database_lib/mri_scanner.py b/python/lib/database_lib/mri_scanner.py deleted file mode 100644 index 46ca46af2..000000000 --- a/python/lib/database_lib/mri_scanner.py +++ /dev/null @@ -1,158 +0,0 @@ -"""This class performs database queries for the mri_scanner table""" - -import datetime - -from typing_extensions import deprecated - -from lib.candidate import Candidate - -__license__ = "GPLv3" - - -@deprecated('Use `lib.scanner` instead') -class MriScanner: - """ - This class performs database queries for imaging dataset stored in the mri_scanner table. - - :Example: - - from lib.mri_scanner import MriScanner - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - mri_scanner_db_obj = MriScanner(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the MriScanner class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.scanner.get_or_create_scanner` instead') - def determine_scanner_information(self, manufacturer, software_version, serial_number, scanner_model, - center_id, project_id): - """ - Select a ScannerID based on the scanner information gathered from the headers of the - DICOM archive. If a ScannerID is not found for the scanner a new entry will be inserted - into the mri_scanner table. - - :param manufacturer: scanner manufacturer - :type manufacturer: str - :param software_version: scanner software version - :type software_version: str - :param serial_number: scanner serial number - :type serial_number: str - :param scanner_model: scanner model - :type scanner_model: str - :param center_id: Center ID of the scanner - :type center_id: int - :param project_id: ID of the scanner's project - :type project_id: int - - :return: scanner ID - :rtype: int - """ - - query = 'SELECT ID AS ScannerID ' \ - ' FROM mri_scanner ' \ - ' WHERE Manufacturer = %s ' \ - ' AND Software = %s ' \ - ' AND Serial_number = %s ' \ - ' AND Model = %s ' - - arguments = (manufacturer, software_version, serial_number, scanner_model) - - results = self.db.pselect(query=query, args=arguments) - if results: - return results[0]['ScannerID'] - - # if could not find a scanner ID, register a new scanner in mri_scanner - scanner_id = self.register_new_scanner( - manufacturer, software_version, serial_number, scanner_model, center_id, project_id - ) - return scanner_id - - @deprecated('Use `lib.scanner.get_or_create_scanner` instead') - def register_new_scanner(self, manufacturer, software_version, serial_number, scanner_model, center_id, project_id): - """ - Inserts a new entry in the mri_scanner table after having created a new candidate to - associate to that scanner. - - :param manufacturer: scanner manufacturer - :type manufacturer: str - :param software_version: scanner software version - :type software_version: str - :param serial_number: scanner serial number - :type serial_number: str - :param scanner_model: scanner model - :type scanner_model: str - :param center_id: Center ID of the scanner - :type center_id: int - :param project_id: ID of the scanner's project - :type project_id: int - - :return scanner_id: ScannerID of the new entry in the mri_scanner table - :rtype scanner_id: int - """ - - # create a new candidate for the scanner - candidate = Candidate(self.verbose) - new_cand_id = candidate.generate_cand_id(self.db) - column_names = ( - 'CandID', 'PSCID', 'RegistrationCenterID', 'Date_active', - 'UserID', 'Entity_type', 'RegistrationProjectID', 'Date_registered', - ) - values = ( - new_cand_id, 'scanner', center_id, datetime.datetime.now(), - 'imaging.py', 'Scanner', project_id, datetime.datetime.now() - ) - - candidate_id = self.db.insert( - table_name='candidate', - column_names=column_names, - values=values, - get_last_id=True, - ) - - # create the new scanner ID - scanner_id = self.db.insert( - table_name='mri_scanner', - column_names=('Manufacturer', 'Model', 'Serial_number', 'Software', 'CandidateID'), - values=(manufacturer, scanner_model, serial_number, software_version, candidate_id), - get_last_id=True - ) - - return scanner_id - - @deprecated('Use `lib.db.models.mri_scanner.DbMriScanner.candidate` instead') - def get_scanner_candid(self, scanner_id): - """ - Select a ScannerID CandID based on the scanner ID in mri_scanner. - - :param scanner_id: scanner ID in the mri_scanner table - :type scanner_id: int - - :return: scanner CandID - :rtype: int - """ - query = ''' - SELECT CandID - FROM mri_scanner - JOIN candidate ON (candidate.ID=mri_scanner.CandidateID) - WHERE ID = %s - ''' - results = self.db.pselect(query=query, args=(scanner_id,)) - return results[0]['CandID'] if results else None diff --git a/python/lib/database_lib/mri_upload_db.py b/python/lib/database_lib/mri_upload_db.py deleted file mode 100644 index cb76ebcc0..000000000 --- a/python/lib/database_lib/mri_upload_db.py +++ /dev/null @@ -1,88 +0,0 @@ -"""This class performs database queries for the mri_upload table""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.mri_upload.DbMriUpload` instead') -class MriUploadDB: - """ - This class performs database queries for imaging dataset stored in the mri_upload table. - - :Example: - - from lib.mri_upload import MriUploadDB - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - mri_upload_db_obj = MriUploadDB(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the MriUplaodDB class. - - :param db : Database class object - :type db : object - :param verbose : whether to be verbose - :type verbose : bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.models.mri_upload.DbMriUpload` instead') - def update_mri_upload(self, upload_id, fields, values): - """ - Update the `isTarchiveValidated` field of the upload with the value provided - to the function. - - :param upload_id: `UploadID` associated to the upload - :type upload_id: int - :param fields : list with fields to be updated in the `mri_upload` table - :type fields : tuple - :param values : list of values to use for the update query - :type values : tuple - """ - - query = 'UPDATE mri_upload SET ' - - query += ', '.join(map(lambda x: x + ' = %s', fields)) - - query += ' WHERE UploadID = %s' - - args = values + (upload_id,) - - self.db.update(query=query, args=args) - - @deprecated('Use `lib.db.models.mri_upload.DbMriUpload` instead') - def create_mri_upload_dict(self, where_field, where_value): - """ - Create a dictionary out of the entry available in the `mri_upload` table. - - :param where_field: name of the field to query - :type where_field: str - :param where_value: field value to use to query the `mri_upload` table - :type where_value: str - - :return: list of mri_upload found - :rtype: list - """ - - query = "SELECT * FROM mri_upload" - - if where_field == "UploadID": - query += " WHERE UploadID = %s" - elif where_field == "TarchiveID": - query += " WHERE TarchiveID = %s" - elif where_field == "SessionID": - query += " WHERE SessionID = %s" - results = self.db.pselect(query, (where_value,)) - - return results if results else None diff --git a/python/lib/database_lib/notification.py b/python/lib/database_lib/notification.py deleted file mode 100644 index c7be0cc0d..000000000 --- a/python/lib/database_lib/notification.py +++ /dev/null @@ -1,93 +0,0 @@ -"""This class performs database queries for the notification_spool table""" - -import datetime - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.notification_spool` instead') -class Notification: - """ - This class performs database queries for imaging pipeline notification_spool table. - - :Example: - - from lib.notification import Notification - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - notification_db_obj = Notification(db, verbose) - - ... - """ - - def __init__(self, db, verbose, notification_type, notification_origin, process_id): - """ - Constructor method for the Notification class. - - :param db: Database class object - :type db: object - :param verbose: whether to be verbose - :type verbose: bool - :param notification_type: notification type to use for the notification_spool table - :type notification_type: str - :param notification_origin: notification origin to use for the notification_spool table - :type notification_origin: str - :param process_id: process ID to use for the notification_spool table - :type process_id: int - """ - - self.db = db - self.verbose = verbose - - self.notification_type = notification_type - self.notification_origin = notification_origin - self.process_id = process_id - - @deprecated('Use `lib.logging.register_notification` instead') - def write_to_notification_spool(self, message, is_error, is_verbose, center_id=None): - """ - Insert a row in the notification_spool table. - - :param message: message to be inserted in the notification_spool table - :type message: str - :param is_error: whether the notification is an error or not ('Y' or 'N') - :type is_error: str - :param is_verbose: whether the notification is verbose or not ('Y' or 'N') - :type is_verbose: str - :param center_id: the CenterID associated with the notification when applicable - :type center_id: int - :return: - """ - - type_id = self.db.grep_id_from_lookup_table( - id_field_name='NotificationTypeID', - table_name='notification_types', - where_field_name='Type', - where_value=self.notification_type, - insert_if_not_found=True - ) - - col_names = ( - 'NotificationTypeID', 'TimeSpooled', 'Message', 'Origin', - 'ProcessID', 'Error', 'Verbose' - ) - values = ( - type_id, datetime.datetime.now(), message, self.notification_origin, - self.process_id, is_error, is_verbose - ) - - if center_id: - col_names = col_names + ('CenterID',) - values = values + (center_id,) - - self.db.insert( - table_name='notification_spool', - column_names=col_names, - values=values - ) diff --git a/python/lib/database_lib/project_cohort_rel.py b/python/lib/database_lib/project_cohort_rel.py deleted file mode 100644 index 393fe65ee..000000000 --- a/python/lib/database_lib/project_cohort_rel.py +++ /dev/null @@ -1,60 +0,0 @@ -"""This class performs project_cohort_rel table related database queries and common checks""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.project_cohort.DbProjectCohort` instead') -class ProjectCohortRel: - """ - This class performs database queries for project_cohort_rel table. - - :Example: - - from lib.database_lib.project_cohort_rel import ProjectCohortRel - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - project_cohort_rel_obj = ProjectCohortRel(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the ProjectCohortRel class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.models.project_cohort.DbProjectCohort` instead') - def create_proj_cohort_rel_dict(self, project_id, cohort_id): - """ - Get the project/cohort rel information for a given project ID and cohort ID. - - :param project_id: ID of the Project - :type project_id: int - :param cohort_id: ID of the cohort - :type cohort_id: int - - :return: dictionary of the project/cohort rel - :rtype: dict - """ - - query = "SELECT * FROM project_cohort_rel" \ - " JOIN Project USING (ProjectID)" \ - " JOIN cohort USING (CohortID)" \ - " WHERE ProjectID=%s AND CohortID=%s" - results = self.db.pselect(query=query, args=(project_id, cohort_id)) - - return results[0] if results else None diff --git a/python/lib/database_lib/session_db.py b/python/lib/database_lib/session_db.py deleted file mode 100644 index 69cdf5c64..000000000 --- a/python/lib/database_lib/session_db.py +++ /dev/null @@ -1,129 +0,0 @@ -"""This class performs session table related database queries and common checks""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.session.DbSession` instead') -class SessionDB: - """ - This class performs database queries for session table. - - :Example: - - from lib.database_lib.session_db import SessionDB - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - session_obj = SessionDB(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the SessionDB class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.queries.try_get_candidate_with_cand_id_visit_label` instead') - def create_session_dict(self, cand_id, visit_label): - """ - Queries the session table for a particular candidate ID and visit label and returns a dictionary - with the session information. - - :param cand_id: CandID - :type cand_id: int - :param visit_label: Visit label of the session - :type visit_label: str - - :return: dictionary of the information present in the session table for that candidate/visit - :rtype: dict - """ - - query = "SELECT * FROM session" \ - " JOIN psc USING (CenterID)" \ - " JOIN candidate ON (candidate.ID=session.CandidateID)" \ - " WHERE CandID=%s AND LOWER(Visit_label)=LOWER(%s) AND Active='Y'" - results = self.db.pselect(query=query, args=(cand_id, visit_label)) - - return results[0] if results else None - - @deprecated('Use `lib.db.queries.site.try_get_site_with_psc_id_visit_label` instead') - def get_session_center_info(self, pscid, visit_label): - """ - Get site information for a given visit. - - :param pscid: candidate site ID (PSCID) - :type pscid: str - :param visit_label: visit label - :type visit_label: str - - :return: dictionary of site information for the visit/candidate queried - :rtype: dict - """ - - query = "SELECT * FROM session" \ - " JOIN psc USING (CenterID)" \ - " JOIN candidate ON (candidate.ID = session.CandidateID)" \ - " WHERE PSCID=%s AND Visit_label=%s" - results = self.db.pselect(query=query, args=(pscid, visit_label)) - - return results[0] if results else None - - @deprecated('Use `lib.get_subject_session.get_candidate_next_visit_number` instead') - def determine_next_session_site_id_and_visit_number(self, cand_id): - """ - Determines the next session site and visit number based on the last session inserted for a given candidate. - - :param cand_id: candidate ID - :type cand_id: int - - :return: a dictionary with 'newVisitNo' and 'CenterID' keys/values - :rtype: dict - """ - query = "SELECT IFNULL(MAX(VisitNo), 0) + 1 AS newVisitNo, CenterID" \ - " FROM session WHERE CandID = %s GROUP BY CandID, CenterID" - results = self.db.pselect(query=query, args=(cand_id,)) - - if results: - return results[0] - - query = "SELECT 1 AS newVisitNo, RegistrationCenterID AS CenterID FROM candidate WHERE CandID = %s" - results = self.db.pselect(query=query, args=(cand_id,)) - - return results[0] if results else None - - @deprecated('Use `lib.db.models.session.DbSession` instead') - def insert_into_session(self, fields, values): - """ - Insert a new row in the session table using fields list as column names and values as values. - - :param fields: column names of the fields to use for insertion - :type fields: list - :param values: values for the fields to insert - :type values: list - - :return: ID of the new session registered - :rtype: int - """ - - session_id = self.db.insert( - table_name="session", - column_names=fields, - values=values, - get_last_id=True - ) - - return session_id diff --git a/python/lib/database_lib/site.py b/python/lib/database_lib/site.py deleted file mode 100644 index ca83741ef..000000000 --- a/python/lib/database_lib/site.py +++ /dev/null @@ -1,51 +0,0 @@ -"""This class performs database queries for the site (psc) table""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.site.DbSite` instead') -class Site: - """ - This class performs database queries on the psc (site) table. - - :Example: - - from lib.site import Site - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - site_db_obj = Site(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the Site class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.queries.site.get_all_sites` instead') - def get_list_of_sites(self): - """ - Returns a list of dictionaries storing the list of sites present in the psc table. - - :return: list of dictionaries with the list of sites present in the psc table - :rtype: list - """ - - results = self.db.pselect(query='SELECT * FROM psc') - - return results if results else None diff --git a/python/lib/database_lib/tarchive.py b/python/lib/database_lib/tarchive.py deleted file mode 100644 index 7254c73c8..000000000 --- a/python/lib/database_lib/tarchive.py +++ /dev/null @@ -1,89 +0,0 @@ -"""This class performs DICOM archive related database queries and common checks""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.dicom_archive.DbDicomArchive` instead') -class Tarchive: - """ - This class performs database queries for DICOM archives. - - :Example: - - from lib.tarchive import Tarchive - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - tarchive = Tarchive(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the Tarchive class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.queries.dicom_archive.try_get_dicom_archive_with_*` instead') - def create_tarchive_dict(self, archive_location=None, tarchive_id=None): - """ - Create dictionary with DICOM archive information selected from the tarchive table. - - :param archive_location: relative location of the DICOM archive (without data directory path) - :type archive_location: str - :param tarchive_id : TarchiveID of the DICOM archive in the tarchive table - :type tarchive_id : int - - :return: dictionary with the DICOM archive information selected from the tarchive table - :rtype: dict - """ - - query = 'SELECT * FROM tarchive WHERE ' - args = None - - if archive_location: - query += ' ArchiveLocation LIKE %s ' - args = ('%' + archive_location + '%',) - elif tarchive_id: - query += ' TarchiveID = %s ' - args = (tarchive_id,) - - results = self.db.pselect(query=query, args=args) - - return results[0] if results else None - - @deprecated('Use `lib.db.models.dicom_archive.DbDicomArchive` instead') - def update_tarchive(self, tarchive_id, fields, values): - """ - Updates the tarchive table for a given TarchiveID. - - :param tarchive_id: TarchiveID row to update in the tarchive table - :type tarchive_id: int - :param fields: tarchive table fields to update - :type fields: tuple - :param values: values to use to update the tarchive table fields - :type values: tuple - """ - - query = "UPDATE tarchive SET " - - query += ", ".join(map(lambda x: x + " = %s", fields)) - - query += " WHERE TarchiveID = %s" - - args = values + (tarchive_id,) - - self.db.update(query=query, args=args) diff --git a/python/lib/database_lib/tarchive_series.py b/python/lib/database_lib/tarchive_series.py deleted file mode 100644 index df0716d02..000000000 --- a/python/lib/database_lib/tarchive_series.py +++ /dev/null @@ -1,57 +0,0 @@ -"""This class performs tarchive_series related database queries and common checks""" - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.dicom_archive_series.DbDicomArchiveSeries` instead') -class TarchiveSeries: - """ - This class performs database queries for tarchive_series table. - - :Example: - - from lib.tarchive_series import TarchiveSeries - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - tarchive = TarchiveSeries(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the TarchiveSeries class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.queries.dicom_archive.try_get_dicom_archive_series_with_series_uid_echo_time` instead') - def get_tarchive_series_from_series_uid_and_echo_time(self, series_uid, echo_time): - """ - Create dictionary with DICOM archive information selected from the tarchive table. - - :param series_uid: SeriesUID field to restrict the search on - :type series_uid: str - :param echo_time: EchoTime field to restrict the search on - :type echo_time: float - - :return: dictionary with the tarchive series information selected from the tarchive table - :rtype: dict - """ - - query = "SELECT * FROM tarchive_series WHERE SeriesUID = %s AND EchoTime = %s" - results = self.db.pselect(query=query, args=(series_uid, echo_time)) - - return results[0] if results else None diff --git a/python/lib/database_lib/visit_windows.py b/python/lib/database_lib/visit_windows.py deleted file mode 100644 index d4aacd3c9..000000000 --- a/python/lib/database_lib/visit_windows.py +++ /dev/null @@ -1,49 +0,0 @@ -"""This class performs database queries for the Visit_Windows table""" - - -from typing_extensions import deprecated - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.visit_window.DbVisitWindow` instead') -class VisitWindows: - """ - This class performs database queries for the VisitWindows table. - - :Example: - - from lib.visit_windows import VisitWindows - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - visit_windows_db_obj = VisitWindows(db, verbose) - - ... - """ - - def __init__(self, db, verbose): - """ - Constructor method for the VisitWindows class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - - @deprecated('Use `lib.db.queries.visit.try_get_visit_window_with_visit_label` instead') - def check_visit_label_exists(self, visit_label: str) -> bool: - """ - Check if a visit label exists in the Visit_Windows database table. - """ - - query = 'SELECT Visit_label FROM Visit_Windows WHERE BINARY Visit_label = %s' - results = self.db.pselect(query=query, args=(visit_label,)) - return bool(results) diff --git a/python/lib/dicom_archive.py b/python/lib/dicom_archive.py deleted file mode 100644 index 2f32035fb..000000000 --- a/python/lib/dicom_archive.py +++ /dev/null @@ -1,122 +0,0 @@ -"""This class gather functions for DICOM archive handling.""" - -from typing_extensions import deprecated - -import lib.utilities as utilities -from lib.database_lib.tarchive import Tarchive -from lib.database_lib.tarchive_series import TarchiveSeries - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.dicom_archive.DbDicomArchive` instead') -class DicomArchive: - """ - This class gather functions that interact with the database and allow session - creation or to fetch DICOM archive information directly from the database. - - :Example: - - from lib.database import Database - from lib.dicom_archive import DicomArchive - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - dicom_archive_obj = DicomArchive(db, verbose) - - # disconnect from the database - db.disconnect() - """ - - def __init__(self, db, verbose): - """ - Constructor method for the DicomArchive class. - - :param db: Database class object - :type db: object - :param verbose: whether to be verbose - :type verbose: bool - """ - self.db = db - self.verbose = verbose - - self.tarchive_db_obj = Tarchive(db, verbose) - self.tar_series_db_obj = TarchiveSeries(db, verbose) - - self.tarchive_info_dict = dict() - - @deprecated('Use `lib.db.queries.dicom_archive.try_get_dicom_archive_with_archive_location` instead') - def populate_tarchive_info_dict_from_archive_location(self, archive_location): - """ - Populate the DICOM archive information dictionary (self.tarchive_info_dict) with information found in - the tarchive table for a given archive location. - - :param archive_location: location of the DICOM archive (relative path) - :type archive_location: str - """ - self.tarchive_info_dict = self.tarchive_db_obj.create_tarchive_dict(archive_location=archive_location) - - @deprecated('Use `lib.db.queries.dicom_archive.try_get_dicom_archive_with_id` instead') - def populate_tarchive_info_dict_from_tarchive_id(self, tarchive_id): - """ - Populate the DICOM archive information dictionary (self.tarchive_info_dict) with information found in - the tarchive table for a given TarchiveID. - - :param tarchive_id: TarchiveID of the DICOM archive - :type tarchive_id: int - """ - self.tarchive_info_dict = self.tarchive_db_obj.create_tarchive_dict(tarchive_id=tarchive_id) - - @deprecated('Use `lib.db.queries.dicom_archive.try_get_dicom_archive_series_with_series_uid_echo_time` instead') - def populate_tarchive_info_dict_from_series_uid_and_echo_time(self, series_uid, echo_time): - """ - Populate the DICOM archive information dictionary (self.tarchive_info_dict) with information found in - the tarchive table for a given TarchiveID. - - :param series_uid: SeriesUID to use to find entries in the tarchive_series table - :type series_uid: str - :param echo_time: Echo time to use to find entries in the tarchive_series table - :type echo_time: float - """ - tarchive_series_info_dict = self.tar_series_db_obj.get_tarchive_series_from_series_uid_and_echo_time( - series_uid, echo_time - ) - - if "TarchiveID" in tarchive_series_info_dict.keys(): - tarchive_id = tarchive_series_info_dict["TarchiveID"] - self.populate_tarchive_info_dict_from_tarchive_id(tarchive_id=tarchive_id) - - @deprecated( - 'Use `lib.dcm2bids_imaging_pipeline_lib.dicom_validation_pipeline._validate_dicom_archive_md5sum` instead' - ) - def validate_dicom_archive_md5sum(self, tarchive_path): - """ - This function validates that the md5sum of the DICOM archive on the filesystem is the same - as the md5sum of the registered entry in the tarchive table. - - :param tarchive_path: path to the DICOM archive to be validated against the database - :type tarchive_path: str - - :return result: dictionary with the result of the validation - :rtype result: dict - """ - - # compute the md5sum of the tarchive file - tarchive_file_md5sum = utilities.compute_md5_hash(tarchive_path) - - # grep the md5sum stored in the database - tarchive_db_md5sum = self.tarchive_info_dict['md5sumArchive'].split()[0] - - # check that the two md5sum are the same - result = dict() - if tarchive_db_md5sum == tarchive_file_md5sum: - result['success'] = True - result['message'] = f"checksum for target: {tarchive_file_md5sum}; " \ - f"checksum from database: {tarchive_db_md5sum}" - else: - result['success'] = False - result['message'] = "ERROR: DICOM archive seems corrupted or modified. Upload will exit now." - - return result diff --git a/python/lib/imaging.py b/python/lib/imaging.py index 196c06b10..e87becf4d 100644 --- a/python/lib/imaging.py +++ b/python/lib/imaging.py @@ -8,7 +8,6 @@ import nibabel as nib from nilearn import image, plotting -from typing_extensions import deprecated from lib.database_lib.candidate_db import CandidateDB from lib.database_lib.config import Config @@ -18,7 +17,6 @@ from lib.database_lib.mri_protocol_checks import MriProtocolChecks from lib.database_lib.mri_protocol_violated_scans import MriProtocolViolatedScans from lib.database_lib.mri_scan_type import MriScanType -from lib.database_lib.mri_scanner import MriScanner from lib.database_lib.mri_violations_log import MriViolationsLog from lib.database_lib.parameter_file import ParameterFile from lib.database_lib.parameter_type import ParameterType @@ -73,7 +71,6 @@ def __init__(self, db, verbose, config_file=None): self.mri_prot_check_db_obj = MriProtocolChecks(db, verbose) self.mri_prot_viol_scan_db_obj = MriProtocolViolatedScans(db, verbose) self.mri_scan_type_db_obj = MriScanType(db, verbose) - self.mri_scanner_db_obj = MriScanner(db, verbose) self.mri_viol_log_db_obj = MriViolationsLog(db, verbose) self.param_type_db_obj = ParameterType(db, verbose) self.param_file_db_obj = ParameterFile(db, verbose) @@ -787,46 +784,6 @@ def get_violations(self, checks_list, header, severity, scan_param_dict): 'MriProtocolChecksGroupID': hdr_checks_list[0]['MriProtocolChecksGroupID'] } - @deprecated('Use `lib.scanner.get_or_create_scanner` instead') - def get_scanner_id(self, manufacturer, software_version, serial_nb, model_name, center_id, project_id): - """ - Get the scanner ID based on the scanner information provided as input. - - :param manufacturer: Scanner manufacturer - :type manufacturer: str - :param software_version: Scanner software version - :type software_version: str - :param serial_nb: Scanner serial number - :type serial_nb: str - :param model_name: Scanner model name - :type model_name: str - :param center_id: ID of the scanner's center - :type center_id: int - :param project_id: ID of the scanner's project - :type project_id: int - """ - return self.mri_scanner_db_obj.determine_scanner_information( - manufacturer, - software_version, - serial_nb, - model_name, - center_id, - project_id - ) - - @deprecated('Use `lib.db.models.DbScanner.candidate` instead') - def get_scanner_candid(self, scanner_id): - """ - Select a ScannerID CandID based on the scanner ID in mri_scanner. - - :param scanner_id: scanner ID in the mri_scanner table - :type scanner_id: int - - :return: scanner CandID - :rtype: int - """ - return self.mri_scanner_db_obj.get_scanner_candid(scanner_id) - def determine_intended_for_field_for_fmap_json_files(self, tarchive_id): """ Determine what should go in the IntendedFor field of the fieldmap's JSON side car file. diff --git a/python/lib/imaging_io.py b/python/lib/imaging_io.py deleted file mode 100644 index 070141a9f..000000000 --- a/python/lib/imaging_io.py +++ /dev/null @@ -1,123 +0,0 @@ -import datetime -import os -import shutil -import sys -import tarfile -import tempfile - -from typing_extensions import deprecated - -from lib.exitcode import COPY_FAILURE - -"""Set of io functions.""" - -__license__ = "GPLv3" - - -@deprecated('Use `lib.logging` and `lib.util.fs` instead') -class ImagingIO: - def __init__(self, log_obj, verbose): - self.log_obj = log_obj - self.verbose = verbose - - @deprecated('Use `lib.util.fs.extract_archive` instead') - def extract_archive(self, location, prefix, tmp_dir): - """ - Extract Archive in the temporary directory - - :return: extracted directory path - :rtype: str - """ - - now = datetime.datetime.now() - upload_prefix = f'{prefix}_DIR_{now.strftime("%Y-%m-%d_%Hh%Mm%Ss")}_' - extract_location = tempfile.mkdtemp(prefix=upload_prefix, dir=tmp_dir) - tar_file = tarfile.open(location) - tar_file.extractall(extract_location) - tar_file.close() - return extract_location - - @deprecated('Use `lib.util.fs.remove_directory` instead') - def remove_dir(self, dir): - """ - Removes a directory and its content - """ - - if os.path.exists(dir): - try: - shutil.rmtree(dir) - except PermissionError as err: - self.log_info(f"Could not delete {dir}. Error was: {err}", is_error=True, is_verbose=False) - - @deprecated('Use `lib.util.fs.copy_file` instead') - def copy_file(self, old_file_path, new_file_path): - """ - Move a file on the file system. - - :param old_file_path: where to move the file from - :type old_file_path: str - :param new_file_path: where to move the file to - :type new_file_path: str - """ - - self.log_info(f'Moving {old_file_path} to {new_file_path}') - shutil.copytree(old_file_path, new_file_path, dirs_exist_ok=True) - if not os.path.exists(new_file_path): - message = f'Could not copy {old_file_path} to {new_file_path}' - self.log_error_and_exit(message, COPY_FAILURE, is_error=True) - - @deprecated('Use `lib.logging.log_*` instead') - def log_info(self, message, is_error=False, is_verbose=True, to_file=True, to_table=True): - """ - Function to log information that need to be logged in the notification_spool table and in the log - file produced by the script executed. - - :param message: message to log - :type message: str - :param to_file: log message to a file - :type to_file: bool - :param to_table: log message to a table - :type to_table: bool - :param is_error: whether the message to log is an error or not - :type is_error: bool - :param to_file: whether to log to a file - :type to_file: bool - :param to_table: whether to log to the notification table - :type to_table: bool - """ - log_msg = f"==> {message}" - - is_error_str = 'Y' if is_error else 'N' - is_verbose_str = 'Y' if is_verbose else 'N' - - if to_file: - self.log_obj.write_to_log_file(f"{log_msg}\n") - if to_table: - self.log_obj.write_to_notification_table(log_msg, is_error_str, is_verbose_str) - - if self.verbose: - print(f"{log_msg}\n") - - @deprecated('Use `lib.logging.log_error_exit` instead') - def log_error_and_exit(self, message, exit_code, callback = None): - """ - Function to commonly executes all logging information when the script needs to be - interrupted due to an error. It will log the error in the log file created by the - script being executed, add an entry with the error in the notification_spool table - and print the error to the user in the terminal. - - :param message: message to log before exit - :type message: str - :param exit_code: exit code to use to exit the script - :type exit_code: int - :param callback: function to execute before exiting - :type callback: function - """ - - err_msg = f"[ERROR ] {message}" - self.log_obj.write_to_log_file(f"{err_msg}\n") - self.log_obj.write_to_notification_table(err_msg, 'Y', 'N') - print(f"\n{err_msg}\n") - if callback: - callback() - sys.exit(exit_code) diff --git a/python/lib/imaging_upload.py b/python/lib/imaging_upload.py deleted file mode 100644 index 3e21587c8..000000000 --- a/python/lib/imaging_upload.py +++ /dev/null @@ -1,94 +0,0 @@ -"""This class gather functions for mri upload handling.""" - -from typing_extensions import deprecated - -from lib.database_lib.mri_upload_db import MriUploadDB - -__license__ = "GPLv3" - - -@deprecated('Use `lib.db.models.mri_upload.DbMriUpload` instead') -class ImagingUpload: - """ - This class gather functions that interact with the database and allow mri_upload - updates or to fetch mri_upload information directly from the database. - - :Example: - - from lib.imaging_upload import ImagingUpload - from lib.database import Database - - # database connection - db = Database(config.mysql, verbose) - db.connect() - - imaging_upload_obj = ImagingUpload(db, verbose) - - # disconnect from the database - db.disconnect() - """ - - def __init__(self, db, verbose): - """ - Constructor method for the MriUpload class. - - :param db : Database class object - :type db : object - :param verbose: whether to be verbose - :type verbose: bool - """ - - self.db = db - self.verbose = verbose - self.mri_upload_db_obj = MriUploadDB(db, verbose) - - self.imaging_upload_dict = dict() - - @deprecated('Use `lib.db.queries.mri_upload.try_get_mri_upload_with_id` instead') - def create_imaging_upload_dict_from_upload_id(self, upload_id): - """ - Fill in the imaging upload dictionary with the information found for a given upload ID in the mri_upload table. - - :param upload_id: UploadID to use to query mri_upload - :type upload_id: str - """ - - results = self.mri_upload_db_obj.create_mri_upload_dict('UploadID', upload_id) - self.imaging_upload_dict = results[0] if results else None - - @deprecated('Use `lib.db.models.dicom_archive.DbDicomArchive.mri_uploads` instead') - def create_imaging_upload_dict_from_tarchive_id(self, tarchive_id): - """ - Fill in the imaging upload dictionary with information found for a given TarchiveID in the mri_upload table. - - :param tarchive_id: TarchiveID to use to query mri_upload - :type tarchive_id: str - - :return: message if 0 row or more than one row were found in the mri_upload table for the given Tarchive - :rtype: bool, str - """ - - results = self.mri_upload_db_obj.create_mri_upload_dict('TarchiveID', tarchive_id) - - if len(results) > 1: - return False, f"Found {len(results)} rows in mri_upload for 'TarchiveID' {tarchive_id}" - elif len(results) == 1: - self.imaging_upload_dict = results[0] - return True, None - else: - return False, f"Did not find an entry in mri_upload associated with 'TarchiveID' {tarchive_id}" - - @deprecated('Use `lib.db.models.mri_upload.DbMriUpload` instead') - def update_mri_upload(self, upload_id, fields, values): - """ - Calls the MriUpload database lib to update the mri_upload table. - - :param upload_id: UploadID to update - :type upload_id: int - :param fields: Fields that need to be updated in the mri_upload table - :type fields: tuple - :param fields: Values to use to update the fields that need to be updated in the mri_upload table - :type fields: tuple - """ - - self.mri_upload_db_obj.update_mri_upload(upload_id, fields, values) diff --git a/python/lib/log.py b/python/lib/log.py deleted file mode 100644 index 91a769b32..000000000 --- a/python/lib/log.py +++ /dev/null @@ -1,114 +0,0 @@ -"""""" - -import os - -from typing_extensions import deprecated - -from lib.database_lib.notification import Notification - -__license__ = "GPLv3" - - -@deprecated('Use `lib.logging` instead') -class Log: - """ - Class that handles the log edition of the imaging pipeline. - """ - - def __init__(self, db, data_dir, script_name, log_file_basename, script_options, verbose): - """ - Initialize the Log class and creates the log file in which all messages created - by the script being run will be stored. - - :param db: database class object - :type db: object - :param data_dir: path to the imaging data_dir - :type data_dir: str - :param script_name: name of the script creating this log - :type script_name: str - :param log_file_basename: the basename to use for the log file name - :type log_file_basename: str - :param script_options: dictionary with all the script options to be logged - :type script_options: dict - :param verbose: whether to be verbose - :type verbose: bool - """ - self.db = db - self.verbose = verbose - - self.script_name = script_name - self.script_options = script_options - self.log_dir = os.path.join(data_dir, "logs", script_name) - if not os.path.isdir(self.log_dir): - os.makedirs(self.log_dir) - self.log_file = os.path.join(self.log_dir, f"{log_file_basename}.log") - - # set notification_db_obj to None until we get a confirmed UploadID to insert proper notification into the - # database related to the UploadID - self.notification_db_obj = None - - self.create_log_header() - - @deprecated('Use `lib.env.Env.init_notifier` instead') - def initiate_notification_db_obj(self, upload_id): - """ - Instantiate the notification_db_obj to be able to write in the notification table. This can only be done - once we know the upload_id, hence the separate function to initiate the database object. - - :param upload_id: UploadID that will be used as the ProcessID for the notification table - :type upload_id: int - """ - self.notification_db_obj = Notification( - self.db, - self.verbose, - notification_type=f"PYTHON {self.script_name.replace('_', ' ').upper()}", - notification_origin=f"{self.script_name}.py", - process_id=upload_id - ) - - def write_to_notification_table(self, message, is_error, is_verbose): - """ - Writes a message into the notification table. - - :param message: message to be logged in the notification table - :type message: str - :param is_error: 'Y' or 'N' to be inserted into the notification table column 'Error' - :type is_error: str - :param is_verbose: 'Y' or 'N' to be inserted into the notification table column 'Verbose' - :type is_verbose: str - """ - # if notification_db_obj initiated, write message to notification table - if self.notification_db_obj: - self.notification_db_obj.write_to_notification_spool(message, is_error, is_verbose) - - def write_to_log_file(self, message): - """ - Function that writes a message at the end of the log file. - - :param message: the message to be written in the log file - :type message: str - """ - - f = open(self.log_file, "a") - f.write(message) - f.close() - - def create_log_header(self): - """ - Function that creates the header of the log file with the script name information - as well as the options that were provided to the script. - """ - - run_info = os.path.basename(self.log_file[:-13]) - message = f""" ----------------------------------------------------------------- - {run_info.replace("_", " ").upper()} ----------------------------------------------------------------- - -Script run with the following options set -""" - for key in self.script_options: - if self.script_options[key]["value"]: - message += f" --{key}: {self.script_options[key]['value']}\n" - - self.write_to_log_file(f"{message}\n\n") diff --git a/python/lib/session.py b/python/lib/session.py index a727bc2fd..827c7c9a2 100644 --- a/python/lib/session.py +++ b/python/lib/session.py @@ -1,11 +1,6 @@ """This class gather functions for session handling.""" -from typing_extensions import deprecated - from lib.database_lib.candidate_db import CandidateDB -from lib.database_lib.project_cohort_rel import ProjectCohortRel -from lib.database_lib.session_db import SessionDB -from lib.database_lib.site import Site __license__ = "GPLv3" @@ -60,10 +55,7 @@ def __init__(self, db, verbose, cand_id=None, visit_label=None, self.db = db self.verbose = verbose - self.proj_cohort_rel_db_obj = ProjectCohortRel(db, verbose) self.candidate_db_obj = CandidateDB(db, verbose) - self.session_db_obj = SessionDB(db, verbose) - self.site_db_obj = Site(db, verbose) self.cand_id = str(cand_id) self.visit_label = visit_label @@ -71,8 +63,6 @@ def __init__(self, db, verbose, cand_id=None, visit_label=None, self.project_id = project_id self.cohort_id = cohort_id - self.proj_cohort_rel_info_dict = dict() - self.session_info_dict = dict() self.session_id = None def create_session(self): @@ -136,95 +126,3 @@ def get_session_info_from_loris(self): ) return loris_session_info[0] if loris_session_info else None - - @deprecated('Use `lib.db.queries.site.try_get_site_with_psc_id_visit_label` instead') - def get_session_center_info(self, pscid, visit_label): - """ - Get the session center information based on the PSCID and visit label of a session. - - :param pscid: candidate site ID (PSCID) - :type pscid: str - :param visit_label: visit label - :type visit_label: str - - :return: dictionary of site information for the visit/candidate queried - :rtype: dict - """ - return self.session_db_obj.get_session_center_info(pscid, visit_label) - - @deprecated('Use `lib.db.queries.try_get_candidate_with_cand_id_visit_label` instead') - def create_session_dict(self, cand_id, visit_label): - """ - Creates the session information dictionary based on a candidate ID and visit label. This will populate - self.session_info_dict based on the result returned from the database query. - - :param cand_id: CandID - :type cand_id: int - :param visit_label: Visit label of the session - :type visit_label: str - """ - self.session_info_dict = self.session_db_obj.create_session_dict(cand_id, visit_label) - if self.session_info_dict: - self.cand_id = self.session_info_dict['CandID'] - self.visit_label = self.session_info_dict['Visit_label'] - self.center_id = self.session_info_dict['CenterID'] - self.project_id = self.session_info_dict['ProjectID'] - self.cohort_id = self.session_info_dict['CohortID'] - self.session_id = self.session_info_dict['ID'] - - @deprecated('Use `lib.db.models.session.DbSession` instead') - def insert_into_session(self, session_info_to_insert_dict): - """ - Insert a new row in the session table using fields list as column names and values as values. - - :param session_info_to_insert_dict: dictionary with the column names and values to use for insertion - :type session_info_to_insert_dict: dict - - :return: ID of the new session registered - :rtype: int - """ - self.session_id = self.session_db_obj.insert_into_session( - fields=list(session_info_to_insert_dict.keys()), - values=list(session_info_to_insert_dict.values()) - ) - - return self.session_id - - @deprecated('Use `lib.get_subject_session.get_candidate_next_visit_number` instead') - def get_next_session_site_id_and_visit_number(self, cand_id): - """ - Determines the next session site and visit number based on the last session inserted for a given candidate. - - :param cand_id: candidate ID - :type cand_id: int - - :return: a dictionary with 'newVisitNo' and 'CenterID' keys/values - :rtype: dict - """ - return self.session_db_obj.determine_next_session_site_id_and_visit_number(cand_id) - - @deprecated('Use `lib.db.queries.site.get_all_sites` instead') - def get_list_of_sites(self): - """ - Get the list of sites available in the psc table. - - :return: list of sites - :rtype: list - """ - - return self.site_db_obj.get_list_of_sites() - - @deprecated('Use `lib.db.models.project_cohort.DbProjectCohort` instead') - def create_proj_cohort_rel_info_dict(self, project_id, cohort_id): - """ - Populate self.proj_cohort_rel_info_dict with the content returned from the database for the ProjectID and - CohortID. - - :param project_id: ID of the Project - :type project_id: int - :param cohort_id: ID of the Cohort - :type cohort_id: int - """ - self.proj_cohort_rel_info_dict = self.proj_cohort_rel_db_obj.create_proj_cohort_rel_dict( - project_id, cohort_id - ) diff --git a/python/lib/utilities.py b/python/lib/utilities.py index 71444e91b..7e198b608 100644 --- a/python/lib/utilities.py +++ b/python/lib/utilities.py @@ -15,10 +15,8 @@ import numpy import requests import scipy.io -from typing_extensions import deprecated import lib.exitcode -import lib.util.crypto __license__ = "GPLv3" @@ -213,32 +211,6 @@ def update_set_file_path_info(set_file, with_fdt_file): return True -@deprecated('Use `lib.util.crypto.compute_file_blake2b_hash` instead.') -def compute_blake2b_hash(file_path): - """ - Compute the blake2b hash of a file and returns it. - :param file_path: path to the file on which to compute the blake2b hash - :type file_path: str - :return: the blake2b hash of the file - :rtype: str - """ - if os.path.exists(file_path): - return lib.util.crypto.compute_file_blake2b_hash(file_path) - - -@deprecated('Use `lib.util.crypto.compute_file_md5_hash` instead.') -def compute_md5_hash(file_path): - """ - Compute the md5 hash of a file and returns it. - :param file_path: path to the file on which to compute the md5 hash - :type file_path: str - :return: the md5 hash of the file - :rtype: str - """ - if os.path.exists(file_path): - return lib.util.crypto.compute_file_md5_hash(file_path) - - def create_processing_tmp_dir(template_prefix): """ Creates a temporary directory with a name based on the concatenation of the @@ -265,15 +237,6 @@ def create_processing_tmp_dir(template_prefix): return tmp_dir -@deprecated('Use `lib.util.fs.remove_empty_subdirectories` instead') -def remove_empty_folders(path_abs): - - walk = list(os.walk(path_abs)) - for path, _, _ in walk[::-1]: - if len(os.listdir(path)) == 0: - os.rmdir(path) - - def assemble_hed_service(data_dir, event_tsv_path, event_json_path): # Using HED Tool Rest Services to assemble the HED Tags # https://hed-examples.readthedocs.io/en/latest/HedToolsOnline.html#hed-restful-services