Пример #1
0
        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);
        }
Пример #2
0
        public override void HandleInstance(InstanceStorageInfo value)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            ;
            if (!value.CalledAeTitle.Equals(_configuration.AeTitle))
            {
                throw new InstanceNotSupportedException(value);
            }
            ;


            if (!_dicomToolkit.TryGetString(value.InstanceStorageFullPath, _grouping, out string key) ||
                string.IsNullOrWhiteSpace(key))
            {
                _logger.Log(LogLevel.Error, "Instance missing required DICOM key for grouping by {0}, ignoring", _grouping.ToString());
                return;
            }

            InstanceCollection collection = null;

            lock (SyncRoot)
            {
                if (_instances.TryGetValue(key, out InstanceCollection val))
                {
                    collection = val;
                }
                else
                {
                    collection = new InstanceCollection(key);
                    _instances.Add(key, collection);
                    _logger.Log(LogLevel.Debug, "New collection created for {0}", key);
                }
                collection.AddInstance(value);
            }
            _logger.Log(LogLevel.Debug, "Instance received and added with key {0}", key);
        }