From 9e969df68b391092b6164d4f5c2f47f82b1e3b97 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Thu, 30 Jan 2025 01:03:06 +0600 Subject: [PATCH] improved study parsing --- app/Services/Pacs/Sync/StudiesSync.php | 32 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/Services/Pacs/Sync/StudiesSync.php b/app/Services/Pacs/Sync/StudiesSync.php index fd4659a..24b2bbb 100644 --- a/app/Services/Pacs/Sync/StudiesSync.php +++ b/app/Services/Pacs/Sync/StudiesSync.php @@ -17,7 +17,7 @@ final class StudiesSync { - public const string SYNC_AGENT = '$$_pacs_agent_$$'; + public const SYNC_AGENT = '$$_pacs_agent_$$'; private Collection $study_ids; private Collection $insert_queue; @@ -147,6 +147,17 @@ public function transformData(mixed $orthanc_src): array $inst_name = data_get($orthanc_src, 'MainDicomTags.InstitutionName'); $patient_name = data_get($orthanc_src, 'PatientMainDicomTags.PatientName'); + $name_parts = tokenizeString($patient_name); + $patient_age = data_get($orthanc_src, 'RequestedTags.PatientAge'); + if (blank($patient_age)) { + // try to get age from last part of patient name + if (! empty($name_parts)) { + $last = end($name_parts); + if (preg_match('/\d+Y/i', $last)) { + $patient_age = strtoupper($last); + } + } + } $descr = $this->getStudyDescription($orthanc_src); $study = [ @@ -160,7 +171,7 @@ public function transformData(mixed $orthanc_src): array 'patient_id' => data_get($orthanc_src, 'PatientMainDicomTags.PatientID'), 'patient_name' => $patient_name, 'patient_sex' => data_get($orthanc_src, 'PatientMainDicomTags.PatientSex'), - 'patient_age' => data_get($orthanc_src, 'RequestedTags.PatientAge'), + 'patient_age' => $patient_age, 'accession_number' => data_get($orthanc_src, 'MainDicomTags.AccessionNumber'), 'referring_physician_name' => data_get($orthanc_src, 'MainDicomTags.ReferringPhysicianName'), @@ -187,8 +198,19 @@ public function transformData(mixed $orthanc_src): array if (filled($dob)) { try { $study['patient_birthdate'] = Carbon::parse($dob); + } catch (Exception $e) { + Log::error('Failed to parse PatientMainDicomTags.PatientBirthDate: {dob}', ['dob' => $dob, 'exception' => $e->getMessage()]); + } + } + + if (($study['patient_birthdate'] == null) && $patient_age !== null) { + try { + // $age = (int) preg_replace('/[^0-9]/', '', $patient_age); + $age = (int) filter_var($patient_age, FILTER_SANITIZE_NUMBER_INT); + $study['patient_birthdate'] = Carbon::now()->subYears($age); + } catch (Exception) { - Log::error('Failed to parse PatientMainDicomTags.PatientBirthDate: {dob}', ['dob' => $dob]); + Log::error('Failed to parse patient_age: {age}', ['age' => $patient_age]); } } @@ -283,7 +305,9 @@ private function getStudyDicomTags(string $study_uuid): array } // randomly sample few instances for tags collection - $selectedInstances = count($instances) <= $this->maxInstances ? $instances : array_rand(array_flip($instances), $this->maxInstances); + $selectedInstances = count($instances) <= $this->maxInstances + ? $instances + : array_rand(array_flip($instances), $this->maxInstances); $tags = collect(); foreach ($selectedInstances as $instance) {