Skip to content
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

Removing PendingStaging field from the files table #292

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 7 additions & 35 deletions docs/scripts_md/MRI.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,44 +74,16 @@ RETURNS: the `CandID` or (if none exists) undef

### getSessionID($subjectIDref, $studyDate, $dbhr, $objective, $noStagingCheck)

Gets (or creates) the session ID, given `CandID` and visit label (contained
inside the hash ref `$subjectIDref`). Unless `$noStagingCheck` is true, it
also determines whether staging is required using the `$studyDate`
(formatted YYYYMMDD) to determine whether staging is required based on a
simple algorithm:

> \- If there exists a session with the same visit label, then that is
> the session ID to use. If any dates (either existing MRI data or
> simply a date of visit) exist associated with that session, then
> if they are outside of some (arbitrary) time window, staging is
> required. If no dates exist, no staging is required.
>
> \- If no sessions exist, then if there is any other date associated
> with another session of the same subject within a time window,
> staging is required.
>
> \- Otherwise, staging is not required.
Gets (or creates) the session ID, given CandID and visitLabel (contained
inside the hashref `$subjectIDref`).

INPUTS:
- $subjectIDref : hash reference of subject IDs
- $studyDate : study date
- $dbhr : database handle reference
- $objective : the objective of the study
- $noStagingCheck: a no staging check flag

RETURNS: a list of two items, (`sessionID`, `requiresStaging`)

### checkMRIStudyDates($studyDateJD, $dbhr, @fileIDs)

This method tries to figure out if there may have been labelling problems which
would put the files in a staging area that does not actually exist.

INPUTS:
- $studyDateJD: study date
- $dbhr : database handle reference
- @fileIDs : array of `fileIDs` to check the study date
- $subjectIDref: hash reference of subject IDs
- $studyDate : study date
- $dbhr : database handle reference
- $objective : the objective of the study

RETURNS: 1 if the file requires staging, 0 otherwise
RETURNS: the session ID of the visit

### getObjective($subjectIDsref, $dbhr)

Expand Down
114 changes: 14 additions & 100 deletions uploadNeuroDB/NeuroDB/MRI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -193,42 +193,22 @@ sub getScannerCandID {

=head3 getSessionID($subjectIDref, $studyDate, $dbhr, $objective, $noStagingCheck)

Gets (or creates) the session ID, given C<CandID> and visit label (contained
inside the hash ref C<$subjectIDref>). Unless C<$noStagingCheck> is true, it
also determines whether staging is required using the C<$studyDate>
(formatted YYYYMMDD) to determine whether staging is required based on a
simple algorithm:

=over 3

- If there exists a session with the same visit label, then that is
the session ID to use. If any dates (either existing MRI data or
simply a date of visit) exist associated with that session, then
if they are outside of some (arbitrary) time window, staging is
required. If no dates exist, no staging is required.

- If no sessions exist, then if there is any other date associated
with another session of the same subject within a time window,
staging is required.

- Otherwise, staging is not required.

=back
Gets (or creates) the session ID, given CandID and visitLabel (contained
inside the hashref C<$subjectIDref>).

INPUTS:
- $subjectIDref : hash reference of subject IDs
- $studyDate : study date
- $dbhr : database handle reference
- $objective : the objective of the study
- $noStagingCheck: a no staging check flag
- $subjectIDref: hash reference of subject IDs
- $studyDate : study date
- $dbhr : database handle reference
- $objective : the objective of the study

RETURNS: a list of two items, (C<sessionID>, C<requiresStaging>)
RETURNS: the session ID of the visit

=cut

sub getSessionID {
my ($subjectIDref, $studyDate, $dbhr, $objective, $noStagingCheck) = @_;
my ($sessionID, $requiresStaging, $studyDateJD);
my ($subjectIDref, $studyDate, $dbhr, $objective) = @_;
my ($sessionID, $studyDateJD);
my ($query, $sth);
my $dbh = $$dbhr;

Expand All @@ -239,7 +219,6 @@ sub getSessionID {

##### if it finds an existing session it does this:
if($sth->rows > 0) {
$requiresStaging = 1 unless $noStagingCheck;
my $timepoint = $sth->fetchrow_hashref();
$sessionID = $timepoint->{'ID'};
$sth->finish();
Expand All @@ -253,17 +232,10 @@ sub getSessionID {
if(defined($studyDate) && $studyDate =~ /^(\d{4})(\d{2})(\d{2})/) {
# compute the julian date of the study
$studyDateJD = julian_day($1, $2, $3);
} else {
# no study date, so no staging
$requiresStaging = 0;
}
# staging not required if the study date matches the timepoint date of visit
if(defined($studyDateJD) and defined($timepointJD) and $studyDateJD == $timepointJD) {
$requiresStaging = 0;
}

# check dates of other files
if(defined($studyDateJD) and $requiresStaging == 1) {
if(defined($studyDateJD)) {
# get the set of files
$query = "SELECT FileID FROM files WHERE SessionID=$sessionID AND FileType='mnc' AND OutputType='native'";
$sth = $dbh->prepare($query);
Expand All @@ -273,16 +245,13 @@ sub getSessionID {
my @files = ();
while(my $filehr = $sth->fetchrow_hashref()) { push @files, $filehr->{'FileID'}; }
$sth->finish();

# run the check
$requiresStaging = checkMRIStudyDates($studyDateJD, $dbhr, @files);

}
}

##### if there is no existing session, which always happens if you create candidates based on incoming data
} else {
$requiresStaging = 0;


# determine the visit number and centerID for the next session
my $newVisitNo = 0;
my $centerID = 0;
Expand Down Expand Up @@ -329,7 +298,7 @@ sub getSessionID {
$subjectIDref->{'visitNo'} = $newVisitNo; # add visit number to subjectIDref

# check dates of other files
if(defined($studyDateJD) and !$noStagingCheck) {
if(defined($studyDateJD)) {
# get the set of sessions for the subject
$query = "SELECT ID FROM session WHERE CandID=$subjectIDref->{'CandID'} AND Active='Y'";
$sth = $dbh->prepare($query);
Expand All @@ -350,69 +319,14 @@ sub getSessionID {
while(my $filearray = $sth->fetchrow_array()) { push @files, $filearray[0]; }

$sth->finish();

# run the check - note it's backwards (!) because this
# time we're looking for mris in other studies which
# are confounding rather than mris in this study which
# are supporting
$requiresStaging = !checkMRIStudyDates($studyDateJD, $dbhr, @files);
} # end if sth->rows (files)
} # end if sth->rows (sessionIDs)
} # end if defined studyDateJD
}

return ($sessionID, $requiresStaging);
return ($sessionID);
}

=pod

=head3 checkMRIStudyDates($studyDateJD, $dbhr, @fileIDs)

This method tries to figure out if there may have been labelling problems which
would put the files in a staging area that does not actually exist.

INPUTS:
- $studyDateJD: study date
- $dbhr : database handle reference
- @fileIDs : array of C<fileIDs> to check the study date

RETURNS: 1 if the file requires staging, 0 otherwise

=cut

sub checkMRIStudyDates {
my ($studyDateJD, $dbhr, @fileIDs) = @_;

if(scalar(@fileIDs) == 0) {
carp "No fileIDs passed in to checkMRIStudyDates\n";
return 0;
}

my $requiresStaging = 1;
my $file = NeuroDB::File->new($dbhr);
my $studyDateID = $file->getParameterTypeID('study_date');

# check the other files
my $query = "SELECT DISTINCT Value FROM parameter_file WHERE ParameterTypeID=$studyDateID AND FileID IN (".join(',', @fileIDs).")";
my $sth = $${dbhr}->prepare($query);
$sth->execute();

if($sth->rows > 0) {
LOOP_FILES: {
while(my $row = $sth->fetchrow_hashref()) {
if($row->{'Value'} =~ /^(\d{4})(\d{2})(\d{2})/) {
my $eventJD = julian_day($1, $2, $3);
if($eventJD == $studyDateJD) {
$requiresStaging = 0;
last LOOP_FILES;
}
}
} # end while
} # end LOOP_FILES
} # end if $sth->rows (parameters)

return $requiresStaging;
}

=pod

Expand Down
6 changes: 2 additions & 4 deletions uploadNeuroDB/NeuroDB/MRIProcessingUtility.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1577,8 +1577,7 @@ sub setMRISession {
$message = "\n==> Getting session ID\n";
$this->{LOG}->print($message);
$this->spool($message, 'N', $upload_id, $notify_detailed);
my ($sessionID, $requiresStaging) =
NeuroDB::MRI::getSessionID(
my ($sessionID) = NeuroDB::MRI::getSessionID(
$subjectIDsref,
$tarchiveInfo->{'DateAcquired'},
$this->{dbhr},
Expand All @@ -1587,7 +1586,6 @@ sub setMRISession {
$message = "\nSessionID: $sessionID\n";
$this->{LOG}->print($message);
$this->spool($message, 'N', $upload_id, $notify_detailed);
# Staging: $requiresStaging\n";
############################################################
# Make sure MRI Scan Done is set to yes, because now #######
# there is data. ###########################################
Expand All @@ -1599,7 +1597,7 @@ sub setMRISession {
}
${$this->{'dbhr'}}->do($query);
}
return ($sessionID, $requiresStaging);
return ($sessionID);
}


Expand Down
10 changes: 4 additions & 6 deletions uploadNeuroDB/minc_insertion.pl
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ =head2 Methods

- Loads the created MINC file and then sets the appropriate parameter for
the loaded object (i.e ScannerID, SessionID,SeriesUID, EchoTime,
PendingStaging, CoordinateSpace , OutputType , FileType
,TarchiveSource and Caveat)
CoordinateSpace , OutputType , FileType,
TarchiveSource and Caveat)
- Extracts the correct acquition protocol
- Registers the scan into db by first changing the minc-path and setting extra
parameters
Expand Down Expand Up @@ -496,10 +496,9 @@ =head2 Methods
}

################################################################
####### Get the $sessionID and $requiresStaging ################
####### Get the $sessionID ####################################
################################################################
my ($sessionID, $requiresStaging) =
NeuroDB::MRI::getSessionID(
my ($sessionID) = NeuroDB::MRI::getSessionID(
$subjectIDsref,
$tarchiveInfo{'DateAcquired'},
\$dbh, $subjectIDsref->{'subprojectID'}
Expand Down Expand Up @@ -527,7 +526,6 @@ =head2 Methods
$file->setFileData('SessionID', $sessionID);
$file->setFileData('SeriesUID', $file->getParameter('series_instance_uid'));
$file->setFileData('EchoTime', $file->getParameter('echo_time'));
$file->setFileData('PendingStaging', $requiresStaging);
$file->setFileData('CoordinateSpace', 'native');
$file->setFileData('OutputType', 'native');
$file->setFileData('FileType', 'mnc');
Expand Down
8 changes: 2 additions & 6 deletions uploadNeuroDB/register_processed_data.pl
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ =head2 Methods
# ----- STEP 4: Determine using sourceFileID:
# - subject's identifiers
# - sessionID
# - requiresStaging
my ($sessionID,$requiresStaging,$subjectIDsref) = getSessionID($sourceFileID,$dbh);
my ($sessionID,$subjectIDsref) = getSessionID($sourceFileID,$dbh);
if (!defined($sessionID)) {
print LOG "\nERROR: could not determine sessionID based on sourceFileID "
. "$sourceFileID. Are you sure the sourceFile was registered "
Expand Down Expand Up @@ -388,10 +387,7 @@ sub getSessionID {
return undef;
}

# set requiresStaging to null as long as don't have any more information on this field
my $requiresStaging = 0;

return ($sessionID,$requiresStaging,\%subjectIDsref);
return ($sessionID, \%subjectIDsref);
}


Expand Down
2 changes: 1 addition & 1 deletion uploadNeuroDB/tarchiveLoader
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ if (!defined($subjectIDsref->{'visitLabel'})) {
\$dbh
);
}
my ($sessionID, $requiresStaging) =
my ($sessionID) =
NeuroDB::MRI::getSessionID(
$subjectIDsref, $tarchiveInfo{'DateAcquired'},
\$dbh, $subjectIDsref->{'subprojectID'}
Expand Down
2 changes: 1 addition & 1 deletion uploadNeuroDB/tarchive_validation.pl
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ =head2 Methods
################################################################
############ Get the SessionID #################################
################################################################
my ($sessionID, $requiresStaging) =
my ($sessionID) =
$utility->setMRISession($subjectIDsref, \%tarchiveInfo, $upload_id);

################################################################
Expand Down