Skip to content

Commit

Permalink
Refactor remaining array configs to use ConfigOB instead of the DBI l…
Browse files Browse the repository at this point in the history
…ibrary (#770)

* use configOB for array configs

* use configOB for minc_to_bids script

* fix bug in delete script

* fix bugs with new perl

* use array for some bids config

* bug fix

* fix pod error + Nic's comment

* take care of Nic's comments

* recreate markdown files

* correct documentation of grep_FileIDs_to_deface in run_defacing_script

* Nic's review
  • Loading branch information
cmadjar authored Feb 17, 2023
1 parent 817b89f commit 4257108
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 136 deletions.
37 changes: 37 additions & 0 deletions docs/scripts_md/ConfigOB.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,43 @@ Get the compute\_snr\_modalities Config setting

RETURN: an array (possibly empty) of the modality IDs (i.e t1w, etc..) for which to compute the SNR

### getExcludedSeriesDescription()

Get the excluded\_series\_description Config setting

RETURN: an array (possibly empty) of the series description to exclude from import

### getModalitiesToDeface()

Get the modalities\_to\_deface Config setting

RETURN: an array (possibly empty) of the modalities to run the defacing pipeline on

### getBidsDatasetAuthors()

Get the bids\_dataset\_authors Config setting

RETURN: an array (possibly empty) of the authors to use for a BIDS dataset

### getBidsAcknowledgmentsText()

Get the bids\_acknowledgments\_text Config setting

RETURN: a string of the acknowledgment text to use for a BIDS dataset (or undef)

### getBidsReadmeText()

Get the bids\_readme\_text Config setting

RETURN: a string of the README text to use for a BIDS dataset (or undef)

### getBidsValidatorOptionsToIgnore()

Get the bids\_validator\_options\_to\_ignore Config setting

RETURN: an array of the BIDS validator options to ignore
to use when creating a BIDS dataset

# TO DO

Nothing planned.
Expand Down
12 changes: 0 additions & 12 deletions docs/scripts_md/DBI.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ INPUTS:

RETURNS: DBI database handler when connection is successful

### getConfigSetting($dbh, $name)

This method fetches the value (`$value`) stored in the `Config` table for a
specific config setting (`$name`) specified as an input.

INPUTS:
- $dbh : database handler
- $name: name of the config setting

RETURNS: value corresponding to the config setting in the `Config` table
of LORIS

# TO DO

Expand the package with more functions.
Expand Down
4 changes: 2 additions & 2 deletions docs/scripts_md/MRIProcessingUtility.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,17 @@ INPUTS:
RETURNS: acquisition protocol ID of the MINC file
### dicom\_to\_minc($study\_dir, $converter, $get\_dicom\_info, $exclude, $mail\_user, $upload\_id)
### dicom\_to\_minc($study\_dir, $converter, $get\_dicom\_info, $mail\_user, $upload\_id, @exclude)
Converts a DICOM study into MINC files.
INPUTS:
- $study\_dir : DICOM study directory to convert
- $converter : converter to be used
- $get\_dicom\_info : get DICOM information setting from the `Config` table
- $exclude : which files to exclude from the `dcm2mnc` command
- $mail\_user : mail of the user
- $upload\_id : upload ID of the study
- @exclude : which files to exclude from the `dcm2mnc` command
### get\_mincs($minc\_files, $upload\_id)
Expand Down
6 changes: 3 additions & 3 deletions docs/scripts_md/run_defacing_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ specified in the Config module of LORIS.

# METHODS

### grep\_FileIDs\_to\_deface($session\_id\_arr, $modalities\_to\_deface\_arr)
### grep\_FileIDs\_to\_deface($session\_id\_ref, @modalities\_to\_deface)

Queries the database for the list of acquisitions' FileID to be used to run the
defacing algorithm based on the provided list of SessionID and Scan\_type to
restrict the search.

INPUTS:
- $session\_id\_arr : array of SessionIDs to use when grepping FileIDs
- $modalities\_to\_deface\_arr: array of Scan\_type to use when grepping FileIDs
- $session\_id\_ref : array of SessionIDs to use when grepping FileIDs
- @modalities\_to\_deface: array of Scan\_type to use when grepping FileIDs

RETURNS: hash of matching FileIDs to be used to run the defacing algorithm
organized in a hash as follows:
Expand Down
4 changes: 2 additions & 2 deletions tools/delete_imaging_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2014,8 +2014,8 @@ sub getScanTypesToDelete {
return () if !defined $scanTypeList && !$keepDefaced;

if($keepDefaced) {
my $scanTypesRef = &NeuroDB::DBI::getConfigSetting(\$dbh, 'modalities_to_deface');
return defined $scanTypesRef ? (map { $_ => 1 } @$scanTypesRef) : ();
my @scanTypes = $configOB->getModalitiesToDeface();
return (map { $_ => 1 } @scanTypes);
}

my %types = map { $_=> 1 } split(/,/, $scanTypeList);
Expand Down
37 changes: 23 additions & 14 deletions tools/minc_to_bids_converter.pl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ =head2 METHODS
use NeuroDB::MRI;
use NeuroDB::ExitCodes;
use NeuroDB::File;
use NeuroDB::objectBroker::ConfigOB;


# # Set script's constants here
Expand Down Expand Up @@ -203,15 +204,25 @@ =head2 METHODS
my $dbh = &NeuroDB::DBI::connect_to_db(@Settings::db);
print "\n==> Successfully connected to database \n";

# Get settings from the ConfigSettings table
my $data_dir = &NeuroDB::DBI::getConfigSetting(\$dbh,'dataDirBasepath');
my $bin_dir = &NeuroDB::DBI::getConfigSetting(\$dbh,'MRICodePath');
my $authors = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_dataset_authors');
my $acknowledgments = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_acknowledgments_text');
my $readme_content = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_readme_text');
my $validator_ignore_opts = &NeuroDB::DBI::getConfigSetting(\$dbh, 'bids_validator_options_to_ignore');

unless (defined $authors && defined $acknowledgments && defined $readme_content) {
# new Moose database connection
my $db = NeuroDB::Database->new(
databaseName => $Settings::db[0],
userName => $Settings::db[1],
password => $Settings::db[2],
hostName => $Settings::db[3]
);
$db->connect();

# Get settings from the Config table
my $configOB = NeuroDB::objectBroker::ConfigOB->new(db => $db);
my $data_dir = $configOB->getDataDirPath();
my $bin_dir = $configOB->getMriCodePath();
my @authors = $configOB->getBidsDatasetAuthors();
my $acknowledgments = $configOB->getBidsAcknowledgmentsText();
my $readme_content = $configOB->getBidsReadmeText();
my @validator_ignore_opts = $configOB->getBidsValidatorOptionsToIgnore();

unless (@authors && defined $acknowledgments && defined $readme_content) {
print STDERR "\n ERROR: Some 'MINC to BIDS Converter Tool Options' are not set in the configuration module."
. " 'BIDS Dataset Authors', 'BIDS Dataset Acknowledgments' and 'BIDS Dataset README' need to be"
. " defined.\n\n";
Expand Down Expand Up @@ -259,7 +270,7 @@ =head2 METHODS
'BIDSVersion' => $BIDS_VERSION,
'Name' => $dataset_name,
'LORISScriptVersion' => $LORIS_SCRIPT_VERSION,
'Authors' => $authors,
'Authors' => @authors,
'HowToAcknowledge' => $acknowledgments,
'LORISReleaseVersion' => $loris_mri_version
);
Expand Down Expand Up @@ -290,11 +301,9 @@ =head2 METHODS
# specifications, not the same number of files per session etc...)
# =============================================================================
my $bids_validator_config_file = $dest_dir . "/.bids-validator-config.json";
if (!-e $bids_validator_config_file && defined $validator_ignore_opts) {
if (!-e $bids_validator_config_file && @validator_ignore_opts) {
print "\n******* Creating the .bids-validator-config.json file $bids_validator_config_file *******\n";
my $validator_ignore_string = ref($validator_ignore_opts) eq 'ARRAY'
? join(", ", @$validator_ignore_opts)
: $validator_ignore_opts;
my $validator_ignore_string = join(", ", @validator_ignore_opts);
my $bids_validator_config_content = <<TEXT;
{
"ignore": [$validator_ignore_string]
Expand Down
40 changes: 20 additions & 20 deletions tools/run_defacing_script.pl
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ =head1 METHODS

my $data_dir = $configOB->getDataDirPath();
my $ref_scan_type = $configOB->getDefacingRefScanType();
my @to_deface = $configOB->getModalitiesToDeface();


# -----------------------------------------------------------------
## Get config setting using the old database calls
# -----------------------------------------------------------------

my $to_deface = &NeuroDB::DBI::getConfigSetting(\$dbh, 'modalities_to_deface');
unless ($ref_scan_type && $to_deface) {
unless ($ref_scan_type && @to_deface) {
print STDERR "\n==> ERROR: you need to configure both the "
. "reference_scan_type_for_defacing & modalities_to_deface config "
. "settings in the imaging pipeline section of the Config module.\n"
Expand Down Expand Up @@ -235,14 +235,14 @@ =head1 METHODS
print "\n==> Fetching all FileIDs to deface.\n" if $verbose;
my @session_ids = defined $session_ids ? split(",", $session_ids) : ();

unless ($to_deface) {
unless (@to_deface) {
print "\nNo modalities were set to be defaced in the Config module. Ensure"
. " to select modalities to deface in the Config module under the imaging"
. " pipeline section (setting called modalities_to_deface. \n\n";
exit $NeuroDB::ExitCodes::SUCCESS;
}

my %files_hash = grep_FileIDs_to_deface(\@session_ids, $to_deface);
my %files_hash = grep_FileIDs_to_deface(\@session_ids, @to_deface);



Expand Down Expand Up @@ -293,15 +293,15 @@ =head1 METHODS

=pod
=head3 grep_FileIDs_to_deface($session_id_arr, $modalities_to_deface_arr)
=head3 grep_FileIDs_to_deface($session_id_ref, @modalities_to_deface)
Queries the database for the list of acquisitions' FileID to be used to run the
defacing algorithm based on the provided list of SessionID and Scan_type to
restrict the search.
INPUTS:
- $session_id_arr : array of SessionIDs to use when grepping FileIDs
- $modalities_to_deface_arr: array of Scan_type to use when grepping FileIDs
- $session_id_ref : array of SessionIDs to use when grepping FileIDs
- @modalities_to_deface: array of Scan_type to use when grepping FileIDs
RETURNS: hash of matching FileIDs to be used to run the defacing algorithm
organized in a hash as follows:
Expand All @@ -318,18 +318,18 @@ =head3 grep_FileIDs_to_deface($session_id_arr, $modalities_to_deface_arr)
=cut

sub grep_FileIDs_to_deface {
my ($session_id_arr, $modalities_to_deface_arr) = @_;
my ($session_id_ref, @modalities_to_deface) = @_;

# separate the special modalities specified in %SPECIAL_ACQUISITIONS from the
# standard scan types
my @special_scan_types = keys %SPECIAL_ACQUISITIONS_FILTER;
my @special_cases;
foreach my $special (@special_scan_types) {
# push the special modalities to a new array @special_cases
push @special_cases, grep(/$special/, @$modalities_to_deface_arr);
push @special_cases, grep(/$special/, @modalities_to_deface);
# remove the special modalities from the modalities array as they will be
# dealt with differently than standard modalities
@$modalities_to_deface_arr = grep(! /$special/, @$modalities_to_deface_arr);
@modalities_to_deface = grep(! /$special/, @modalities_to_deface);
}

# base query
Expand All @@ -342,8 +342,8 @@ sub grep_FileIDs_to_deface {

# add where clause for the different standard scan types to deface
my @where;
if (@$modalities_to_deface_arr) {
@where = map { "mst.Scan_type = ?" } @$modalities_to_deface_arr;
if (@modalities_to_deface) {
@where = map { "mst.Scan_type = ?" } @modalities_to_deface;
$query .= sprintf(" %s ", join(" OR ", @where));
}

Expand All @@ -358,20 +358,20 @@ sub grep_FileIDs_to_deface {

# add where clause for the session IDs specified to the script if -sessionIDs
# was set
if ($session_id_arr) {
@where = map { "f.SessionID = ?" } @$session_id_arr;
if ($session_id_ref) {
@where = map { "f.SessionID = ?" } @$session_id_ref;
$query .= sprintf(" AND (%s) ", join(" OR ", @where));
}

my $sth = $dbh->prepare($query);

# create array of parameters
my @bind_param = @$modalities_to_deface_arr;
my @bind_param = @modalities_to_deface;
foreach my $special_scan_type (@special_cases) {
push @bind_param, $special_scan_type;
push @bind_param, $SPECIAL_ACQUISITIONS_FILTER{$special_scan_type};
}
push @bind_param, @$session_id_arr;
push @bind_param, @$session_id_ref;

# execute the query
$sth->execute(@bind_param);
Expand Down Expand Up @@ -434,7 +434,7 @@ =head3 check_if_deface_files_already_in_db($session_files, $session_id)
sub check_if_deface_files_already_in_db {
my ($session_files, $session_id) = @_;

my @defaced_scan_types = map { $_ . '-defaced' } keys $session_files;
my @defaced_scan_types = map { $_ . '-defaced' } keys %{ $session_files };

# base query
my $query = "SELECT COUNT(*) "
Expand Down Expand Up @@ -554,7 +554,7 @@ sub deface_session {

# add multi-constrast modalities to cmd line & remove them from $session_files
foreach my $multi (@MULTI_CONTRAST_ACQUISITIONS_BASE_NAMES) {
my @scan_types = keys $session_files;
my @scan_types = keys %{ $session_files };
my @matching_types = grep (/$multi/i, @scan_types);
my @non_matching_types = grep (!/$multi/i, @scan_types);
my (@multi_files_list, @other_files);
Expand Down Expand Up @@ -623,7 +623,7 @@ sub fetch_defaced_files {
$defaced_images{$deface_ref}{Scan_type} = $$ref_file{Scan_type};

# for each files in $session_files, append the defaced images to the hash
foreach my $scan_type (keys $session_files) {
foreach my $scan_type (keys %{ $session_files }) {
my %files = %{ $$session_files{$scan_type} };
foreach my $fileID (keys %files) {
my $deface_file = $deface_dir . '/' . basename($files{$fileID});
Expand Down Expand Up @@ -663,7 +663,7 @@ sub register_defaced_files {
. " -coordinateSpace native "
. " -outputType defaced ";

foreach my $file (keys $defaced_images) {
foreach my $file (keys %{ $defaced_images }) {
my $input_fileID = $$defaced_images{$file}{InputFileID};
my $scan_type = $$defaced_images{$file}{Scan_type} . "-defaced";

Expand Down
41 changes: 0 additions & 41 deletions uploadNeuroDB/NeuroDB/DBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,47 +72,6 @@ sub connect_to_db
return $dbh;
}

=pod
=head3 getConfigSetting($dbh, $name)
This method fetches the value (C<$value>) stored in the C<Config> table for a
specific config setting (C<$name>) specified as an input.
INPUTS:
- $dbh : database handler
- $name: name of the config setting
RETURNS: value corresponding to the config setting in the C<Config> table
of LORIS
=cut
sub getConfigSetting
{
my ($dbh, $name) = @_;
my ($message, $query, $where) = '';
my $value = undef;

$where = " WHERE c.ConfigID=(Select cs.ID from ConfigSettings cs where cs.Name=?)";
$query = " SELECT c.Value FROM Config c";
$query = $query . $where;
my $sth = $$dbh->prepare($query);
$sth->execute($name);

if ( $sth->rows > 1 ){
# if more than one row returned, push data into an array that will be
# dereferenced into $value
my @values;
while (my $row = $sth->fetchrow_array()) {
push (@values, $row);
}
$value = \@values;
} elsif ( $sth->rows > 0 ) {
$value = $sth->fetchrow_array();
}
return $value;
}

1;

=pod
Expand Down
Loading

0 comments on commit 4257108

Please sign in to comment.