improved study parsing

This commit is contained in:
Dr Masroor Ehsan 2025-01-30 01:03:06 +06:00
parent a02adfc685
commit 9e969df68b

View File

@ -17,7 +17,7 @@
final class StudiesSync final class StudiesSync
{ {
public const string SYNC_AGENT = '$$_pacs_agent_$$'; public const SYNC_AGENT = '$$_pacs_agent_$$';
private Collection $study_ids; private Collection $study_ids;
private Collection $insert_queue; private Collection $insert_queue;
@ -147,6 +147,17 @@ public function transformData(mixed $orthanc_src): array
$inst_name = data_get($orthanc_src, 'MainDicomTags.InstitutionName'); $inst_name = data_get($orthanc_src, 'MainDicomTags.InstitutionName');
$patient_name = data_get($orthanc_src, 'PatientMainDicomTags.PatientName'); $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); $descr = $this->getStudyDescription($orthanc_src);
$study = [ $study = [
@ -160,7 +171,7 @@ public function transformData(mixed $orthanc_src): array
'patient_id' => data_get($orthanc_src, 'PatientMainDicomTags.PatientID'), 'patient_id' => data_get($orthanc_src, 'PatientMainDicomTags.PatientID'),
'patient_name' => $patient_name, 'patient_name' => $patient_name,
'patient_sex' => data_get($orthanc_src, 'PatientMainDicomTags.PatientSex'), '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'), 'accession_number' => data_get($orthanc_src, 'MainDicomTags.AccessionNumber'),
'referring_physician_name' => data_get($orthanc_src, 'MainDicomTags.ReferringPhysicianName'), 'referring_physician_name' => data_get($orthanc_src, 'MainDicomTags.ReferringPhysicianName'),
@ -187,8 +198,19 @@ public function transformData(mixed $orthanc_src): array
if (filled($dob)) { if (filled($dob)) {
try { try {
$study['patient_birthdate'] = Carbon::parse($dob); $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) { } 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 // 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(); $tags = collect();
foreach ($selectedInstances as $instance) { foreach ($selectedInstances as $instance) {