age - dob calculation

This commit is contained in:
Dr Masroor Ehsan 2025-01-30 01:35:07 +06:00
parent 9e969df68b
commit d4c6e58e30

View File

@ -149,16 +149,28 @@ public function transformData(mixed $orthanc_src): array
$patient_name = data_get($orthanc_src, 'PatientMainDicomTags.PatientName'); $patient_name = data_get($orthanc_src, 'PatientMainDicomTags.PatientName');
$name_parts = tokenizeString($patient_name); $name_parts = tokenizeString($patient_name);
$patient_age = data_get($orthanc_src, 'RequestedTags.PatientAge'); $patient_age = data_get($orthanc_src, 'RequestedTags.PatientAge');
if (blank($patient_age)) { if (blank($patient_age) && ! empty($name_parts)) {
// try to get age from last part of patient name // try to get age from last part of patient name
if (! empty($name_parts)) { $last = end($name_parts);
$last = end($name_parts); if (preg_match('/^\d+[YMD]$/i', $last)) {
if (preg_match('/\d+Y/i', $last)) { $patient_age = $last;
$patient_age = strtoupper($last); /*
} // sanitize patient name
array_pop($name_parts);
$patient_name = implode(' ', $name_parts);
*/
} }
} }
if ($patient_age !== null) {
$age = strtoupper(ltrim($patient_age, '0'));
if (strlen($age) > 1) {
$patient_age = $age;
}
}
// $patient_name = trim($patient_name, '.^ ');
$descr = $this->getStudyDescription($orthanc_src); $descr = $this->getStudyDescription($orthanc_src);
$study = [ $study = [
'dicom_server_id' => $this->dicomServer->id, 'dicom_server_id' => $this->dicomServer->id,
@ -193,27 +205,40 @@ public function transformData(mixed $orthanc_src): array
$study['workflow_level'] = $stable_study $study['workflow_level'] = $stable_study
? WorkflowLevel::Unassigned->value ? WorkflowLevel::Unassigned->value
: WorkflowLevel::Received->value; : WorkflowLevel::Received->value;
$study['patient_birthdate'] = null;
$patient_birthdate = null;
$dob = data_get($orthanc_src, 'PatientMainDicomTags.PatientBirthDate'); $dob = data_get($orthanc_src, 'PatientMainDicomTags.PatientBirthDate');
if (filled($dob)) { if (filled($dob)) {
try { try {
$study['patient_birthdate'] = Carbon::parse($dob); $patient_birthdate = Carbon::parse($dob);
} catch (Exception $e) { } catch (Exception $e) {
Log::error('Failed to parse PatientMainDicomTags.PatientBirthDate: {dob}', ['dob' => $dob, 'exception' => $e->getMessage()]); Log::error('Failed to parse PatientMainDicomTags.PatientBirthDate: {dob}', ['dob' => $dob, 'exception' => $e->getMessage()]);
} }
} }
if (($study['patient_birthdate'] == null) && $patient_age !== null) { if ($patient_birthdate == null && $patient_age !== null) {
try { try {
// $age = (int) preg_replace('/[^0-9]/', '', $patient_age); // $age = (int) preg_replace('/[^0-9]/', '', $patient_age);
$age = (int) filter_var($patient_age, FILTER_SANITIZE_NUMBER_INT); $age_num = (int) filter_var($patient_age, FILTER_SANITIZE_NUMBER_INT);
$study['patient_birthdate'] = Carbon::now()->subYears($age); $now = now();
switch (strtoupper(substr($patient_age, -1))) {
case 'Y':
$patient_birthdate = $now->subYears($age_num);
break;
case 'M':
$patient_birthdate = $now->subMonths($age_num);
break;
case 'D':
$patient_birthdate = $now->subDays($age_num);
break;
}
} catch (Exception) { } catch (Exception) {
Log::error('Failed to parse patient_age: {age}', ['age' => $patient_age]); Log::error('Failed to parse patient_age: {age}', ['age' => $patient_age]);
} }
} }
$study['patient_birthdate'] = $patient_birthdate;
// check for priority in patient name or description // check for priority in patient name or description
if (preg_match('/\b(urgent|stat)\b/i', implode(' ', [$descr, $patient_name]))) { if (preg_match('/\b(urgent|stat)\b/i', implode(' ', [$descr, $patient_name]))) {
$this->setValue($study, 'priority', Priority::Stat->value); $this->setValue($study, 'priority', Priority::Stat->value);
@ -307,7 +332,7 @@ private function getStudyDicomTags(string $study_uuid): array
// randomly sample few instances for tags collection // randomly sample few instances for tags collection
$selectedInstances = count($instances) <= $this->maxInstances $selectedInstances = count($instances) <= $this->maxInstances
? $instances ? $instances
: array_rand(array_flip($instances), $this->maxInstances); : array_intersect_key($instances, array_flip(array_rand($instances, $this->maxInstances)));
$tags = collect(); $tags = collect();
foreach ($selectedInstances as $instance) { foreach ($selectedInstances as $instance) {