private void RestoreExistingInstances(InferenceRequest inferenceRequest, Dictionary <string, InstanceStorageInfo> retrievedInstances) { Guard.Against.Null(inferenceRequest, nameof(inferenceRequest)); Guard.Against.Null(retrievedInstances, nameof(retrievedInstances)); _logger.Log(LogLevel.Debug, $"Restoring previously retrieved DICOM instances from {inferenceRequest.StoragePath}"); foreach (var file in _fileSystem.Directory.EnumerateFiles(inferenceRequest.StoragePath, "*.dcm", System.IO.SearchOption.AllDirectories)) { if (_dicomToolkit.HasValidHeader(file)) { var dicomFile = _dicomToolkit.Open(file); var instance = InstanceStorageInfo.CreateInstanceStorageInfo(dicomFile, inferenceRequest.StoragePath, _fileSystem); if (retrievedInstances.ContainsKey(instance.SopInstanceUid)) { continue; } retrievedInstances.Add(instance.SopInstanceUid, instance); _logger.Log(LogLevel.Debug, $"Restored previously retrieved instance {instance.SopInstanceUid}"); } else { _logger.Log(LogLevel.Warning, $"Unable to restore previously retrieved instance from {file}; file does not contain valid DICOM header."); } } }
private Dictionary <DicomTag, HashSet <string> > ExtractValues(List <DicomTag> dicomTagsToExtract, IReadOnlyList <string> files) { Guard.Against.Null(dicomTagsToExtract, nameof(dicomTagsToExtract)); Guard.Against.Null(files, nameof(files)); var bags = new Dictionary <DicomTag, HashSet <string> >(); foreach (var dicomTag in dicomTagsToExtract) { if (!bags.ContainsKey(dicomTag)) { bags.Add(dicomTag, new HashSet <string>()); } } foreach (var file in files) { DicomFile dicomFile = null; try { if (!_dicomToolkit.HasValidHeader(file)) { continue; } dicomFile = _dicomToolkit.Open(file); } catch (Exception ex) { _logger.Log(LogLevel.Debug, ex, $"Error opening file {file}."); continue; } foreach (var dicomTag in dicomTagsToExtract) { try { if (_dicomToolkit.TryGetString(dicomFile, dicomTag, out var value) && !string.IsNullOrWhiteSpace(value)) { bags[dicomTag].Add(value.Substring(0, Math.Min(value.Length, VALUE_LENGTH_LIMIT))); } } catch (Exception ex) { _logger.Log(LogLevel.Error, ex, $"Error extracting metadata from file {file}, DICOM tag {dicomTag}."); } } } return(bags); }