-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate mri_scanner
to the new database abstraction
#1232
Open
maximemulder
wants to merge
1
commit into
aces:main
Choose a base branch
from
maximemulder:sqlalchemy_mri_scanner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session as Database | ||
|
||
from lib.db.models.mri_scanner import DbMriScanner | ||
|
||
|
||
def try_get_scanner_with_info( | ||
db: Database, | ||
manufacturer: str, | ||
software_version: str, | ||
serial_number: str, | ||
model: str, | ||
) -> Optional[DbMriScanner]: | ||
""" | ||
Get an MRI scanner from the database using the provided information, or return `None` if no | ||
scanner is found. | ||
""" | ||
|
||
return db.execute(select(DbMriScanner) | ||
.where(DbMriScanner.manufacturer == manufacturer) | ||
.where(DbMriScanner.model == model) | ||
.where(DbMriScanner.serial_number == serial_number) | ||
.where(DbMriScanner.software_version == software_version) | ||
).scalar_one_or_none() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import random | ||
from datetime import datetime | ||
|
||
from sqlalchemy.orm import Session as Database | ||
|
||
from lib.db.models.candidate import DbCandidate | ||
from lib.db.models.mri_scanner import DbMriScanner | ||
from lib.db.queries.candidate import try_get_candidate_with_cand_id | ||
from lib.db.queries.mri_scanner import try_get_scanner_with_info | ||
from lib.env import Env | ||
|
||
|
||
def get_or_create_scanner( | ||
env: Env, | ||
manufacturer: str, | ||
model: str, | ||
serial_number: str, | ||
software_version: str, | ||
site_id: int, | ||
project_id: int, | ||
) -> DbMriScanner: | ||
""" | ||
Get an MRI scanner from the database using the provided information, or create it if it does | ||
not already exist. | ||
""" | ||
|
||
mri_scanner = try_get_scanner_with_info(env.db, manufacturer, model, serial_number, software_version) | ||
|
||
if mri_scanner is not None: | ||
return mri_scanner | ||
|
||
cand_id = generate_new_cand_id(env.db) | ||
now = datetime.now() | ||
|
||
candidate = DbCandidate( | ||
cand_id = cand_id, | ||
psc_id = 'scanner', | ||
registration_site_id = site_id, | ||
registration_project_id = project_id, | ||
user_id = 'imaging.py', | ||
entity_type = 'Scanner', | ||
date_active = now, | ||
date_registered = now, | ||
active = True, | ||
) | ||
|
||
env.db.add(candidate) | ||
env.db.commit() | ||
|
||
mri_scanner = DbMriScanner( | ||
manufacturer = manufacturer, | ||
model = model, | ||
serial_number = serial_number, | ||
software_version = software_version, | ||
candidate_id = candidate.id, | ||
) | ||
|
||
env.db.add(mri_scanner) | ||
env.db.commit() | ||
|
||
return mri_scanner | ||
|
||
|
||
# TODO: Move this function to a more appropriate place. | ||
def generate_new_cand_id(db: Database) -> int: | ||
""" | ||
Generate a new random CandID that is not already in the database. | ||
""" | ||
|
||
while True: | ||
cand_id = random.randint(100000, 999999) | ||
candidate = try_get_candidate_with_cand_id(db, cand_id) | ||
if candidate is None: | ||
return cand_id | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually I'd like to move this function to
lib.candidate
, but this is future work. I think with the changes in the incremental BIDS importer it might become possible.