public void TestMultipleElementSequences()
        {
            var subDatasets = new List <DicomDataset>();

            for (var i = 0; i < 3; i++)
            {
                subDatasets.Add(new DicomDataset
                {
                    { DicomTag.ReferencedSOPClassUID, "ReferencedSOPClassUID-" + (i + 1) },
                    { DicomTag.ReferencedSOPInstanceUID, "ReferencedSOPInstanceUID-" + (i + 1) }
                });
            }

            var originalDataset = new DicomDataset
            {
                { DicomTag.ReferencedImageSequence, subDatasets.ToArray() }
            };

            var translatedDataset = new Dictionary <DicomTag, object>();

            foreach (DicomItem item in originalDataset)
            {
                object value = DicomTypeTranslaterReader.GetCSharpValue(originalDataset, item);
                translatedDataset.Add(item.Tag, value);
            }

            var reconstructedDataset = new DicomDataset();

            foreach (KeyValuePair <DicomTag, object> item in translatedDataset)
            {
                DicomTypeTranslaterWriter.SetDicomTag(reconstructedDataset, item.Key, item.Value);
            }

            Assert.True(DicomDatasetHelpers.ValueEquals(originalDataset, reconstructedDataset));
        }
        public void BsonRoundTrip_BlacklistedVrs_ConvertedCorrectly()
        {
            var ds = new DicomDataset
            {
                new DicomOtherByte(DicomTag.SelectorOBValue, byte.MinValue),
                new DicomOtherWord(DicomTag.SelectorOWValue, byte.MinValue),
                new DicomUnknown(DicomTag.SelectorUNValue, byte.MinValue)
            };

            // Ensure this test fails if we update the blacklist later
            Assert.True(
                ds.Select(x => x.ValueRepresentation).All(DicomTypeTranslater.DicomVrBlacklist.Contains) &&
                ds.Count() == DicomTypeTranslater.DicomVrBlacklist.Length);

            BsonDocument doc = DicomTypeTranslaterReader.BuildBsonDocument(ds);

            Assert.NotNull(doc);
            Assert.True(doc.All(x => x.Value.IsBsonNull));

            DicomDataset recoDs = DicomTypeTranslaterWriter.BuildDicomDataset(doc);

            Assert.AreEqual(3, recoDs.Count());
            foreach (DicomItem item in recoDs)
            {
                Assert.Zero(((DicomElement)item).Count);
            }
        }
        public void BsonRoundTrip_TagWithMultipleVrs_SameAfterConversion()
        {
            DicomTypeTranslater.SerializeBinaryData = true;

            var usItem = new DicomUnsignedShort(DicomTag.GrayLookupTableDataRETIRED, 0, 1, ushort.MaxValue);
            var owItem = new DicomOtherWord(DicomTag.GrayLookupTableDataRETIRED, 0, 1, ushort.MaxValue);

            Assert.True(usItem.Tag.DictionaryEntry.ValueRepresentations.Length == 3);
            Assert.True(owItem.Tag.DictionaryEntry.ValueRepresentations.Length == 3);

            var usDataset = new DicomDataset {
                usItem
            };
            var owDataset = new DicomDataset {
                owItem
            };

            BsonDocument usDocument = DicomTypeTranslaterReader.BuildBsonDocument(usDataset);
            BsonDocument owDocument = DicomTypeTranslaterReader.BuildBsonDocument(owDataset);

            Assert.NotNull(usDocument);
            Assert.NotNull(owDocument);

            DicomDataset recoUsDataset = DicomTypeTranslaterWriter.BuildDicomDataset(usDocument);
            DicomDataset recoOwDataset = DicomTypeTranslaterWriter.BuildDicomDataset(owDocument);

            Assert.True(DicomDatasetHelpers.ValueEquals(usDataset, recoUsDataset));
            Assert.True(DicomDatasetHelpers.ValueEquals(owDataset, recoOwDataset));
        }
        private static void VerifyBsonTripleTrip(DicomDataset ds)
        {
            BsonDocument bsonDoc     = DicomTypeTranslaterReader.BuildBsonDocument(ds);
            DicomDataset recoDataset = DicomTypeTranslaterWriter.BuildDicomDataset(bsonDoc);
            BsonDocument recoDoc     = DicomTypeTranslaterReader.BuildBsonDocument(recoDataset);

            Assert.AreEqual(bsonDoc, recoDoc);
            Assert.True(DicomDatasetHelpers.ValueEquals(ds, recoDataset));
        }
        public void TestWriteMultiplicity()
        {
            DicomTag stringMultiTag = DicomTag.SpecimenShortDescription;

            string[] values = { "this", "is", "a", "multi", "element", "" };

            var ds = new DicomDataset();

            DicomTypeTranslaterWriter.SetDicomTag(ds, stringMultiTag, values);

            Assert.AreEqual(1, ds.Count());
            Assert.AreEqual(6, ds.GetDicomItem <DicomElement>(stringMultiTag).Count);
            Assert.AreEqual("this\\is\\a\\multi\\element\\", ds.GetString(stringMultiTag));
        }
        public void BsonRoundTrip_StringEncoding_ConvertedCorrectly()
        {
            var ds = new DicomDataset
            {
                new DicomShortString(DicomTag.SelectorSHValue, Encoding.ASCII, "simples"),
                new DicomLongString(DicomTag.SelectorLOValue, Encoding.UTF8, "(╯°□°)╯︵ ┻━┻")
            };

            DicomDataset recoDs = DicomTypeTranslaterWriter.BuildDicomDataset(DicomTypeTranslaterReader.BuildBsonDocument(ds));

            foreach (DicomStringElement stringElement in recoDs.Select(x => x as DicomStringElement))
            {
                Assert.NotNull(stringElement);
                Assert.AreEqual(Encoding.UTF8, stringElement.Encoding);
            }
        }
        public void BsonRoundTrip_BlacklistPrivateDataset_ZeroAfterConversion()
        {
            DicomDataset ds = TranslationTestHelpers.BuildPrivateDataset();

            ds = new DicomDataset(ds.Where(x => DicomTypeTranslater.DicomVrBlacklist.Contains(x.ValueRepresentation)));
            BsonDocument doc         = DicomTypeTranslaterReader.BuildBsonDocument(ds);
            DicomDataset recoDataset = DicomTypeTranslaterWriter.BuildDicomDataset(doc);
            BsonDocument recoDoc     = DicomTypeTranslaterReader.BuildBsonDocument(recoDataset);

            Assert.AreEqual(doc, recoDoc);

            foreach (DicomElement element in recoDataset.Select(x => (DicomElement)x))
            {
                Assert.Zero(element.Buffer.Size);
            }
        }
예제 #8
0
        public void ProcessDocument(BsonDocument document)
        {
            string documentId = document["_id"].ToString();

            var headerDoc = document["header"] as BsonDocument;

            if (headerDoc == null)
            {
                LogUnprocessedDocument(documentId, new ApplicationException("Document did not contain a header field"));
                return;
            }

            var message = new DicomFileMessage
            {
                NationalPACSAccessionNumber = AccNoOrNull(headerDoc["NationalPACSAccessionNumber"]),
                DicomFilePath = (string)headerDoc["DicomFilePath"],
                DicomFileSize = headerDoc.Contains("DicomFileSize") ? (long)headerDoc["DicomFileSize"] : -1
            };

            try
            {
                // Rebuild the dataset from the document, then serialize it to JSON to send
                DicomDataset ds = DicomTypeTranslaterWriter.BuildDicomDataset(document);
                message.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds);

                // Add the header information
                message.StudyInstanceUID  = ds.GetValue <string>(DicomTag.StudyInstanceUID, 0);
                message.SeriesInstanceUID = ds.GetValue <string>(DicomTag.SeriesInstanceUID, 0);
                message.SOPInstanceUID    = ds.GetValue <string>(DicomTag.SOPInstanceUID, 0);
            }
            catch (Exception e)
            {
                LogUnprocessedDocument(documentId, e);
                return;
            }

            if (!message.VerifyPopulated())
            {
                LogUnprocessedDocument(documentId, new ApplicationException("Message was not valid"));
                return;
            }

            IMessageHeader header = MongoDocumentHeaders.RebuildMessageHeader(headerDoc["MessageHeader"].AsBsonDocument);

            lock (_oBufferLock)
                _messageBuffer.Add(new Tuple <DicomFileMessage, IMessageHeader>(message, header));
        }
예제 #9
0
        protected override void UpdateTag(DicomTag dicomTag, DicomDataset dataset, string cellValue)
        {
            var cSharpType = DicomTypeTranslater.GetNaturalTypeForVr(dicomTag.DictionaryEntry.ValueRepresentations,
                                                                     dicomTag.DictionaryEntry.ValueMultiplicity)?.CSharpType;

            object writeValue = cellValue;

            //if it's a supported type e.g. DateTime parse it
            if (cSharpType != null && _factory.IsSupported(cSharpType))
            {
                if (_factory.Dictionary[cSharpType].IsAcceptableAsType(cellValue, Ignore.Me))
                {
                    writeValue = _factory.Dictionary[cSharpType].Parse(cellValue);
                }
            }

            dataset.Remove(dicomTag);
            DicomTypeTranslaterWriter.SetDicomTag(dataset, dicomTag, writeValue);
        }
        public void TestSetDicomTagWithNullElement()
        {
            var dataset = new DicomDataset();

            // Test with a string element and a value element
            var asTag = DicomTag.SelectorASValue;
            var flTag = DicomTag.SelectorFLValue;

            DicomTypeTranslaterWriter.SetDicomTag(dataset, asTag, null);
            DicomTypeTranslaterWriter.SetDicomTag(dataset, flTag, null);

            Assert.True(dataset.Count() == 2);

            var asElement = dataset.GetDicomItem <DicomElement>(DicomTag.SelectorASValue);

            Assert.True(asElement.Buffer.Size == 0);

            var flElement = dataset.GetDicomItem <DicomElement>(DicomTag.SelectorFLValue);

            Assert.True(flElement.Buffer.Size == 0);
        }
예제 #11
0
        private async Task RunQuery()
        {
            _logger.Info("Using MaxDegreeOfParallelism: " + _parallelOptions.MaxDegreeOfParallelism);

            var totalProcessed       = 0;
            var failedToRebuildCount = 0;

            _logger.Debug("Performing query");
            DateTime start = DateTime.Now;

            using (IAsyncCursor <BsonDocument> cursor = await MongoQueryParser.GetCursor(_collection, _findOptionsBase, _queryString))
            {
                _logger.Info("Query completed in {0:g}. Starting checks with cursor", (DateTime.Now - start));
                _logger.Info("Batch size is: " + (_findOptionsBase.BatchSize.HasValue ? _findOptionsBase.BatchSize.ToString() : "unspecified"));

                start = DateTime.Now;

                //Note: Can only check for the cancellation request every time we start to process a new batch
                while (await cursor.MoveNextAsync() && !_tokenSource.IsCancellationRequested)
                {
                    _logger.Debug("Received new batch");

                    IEnumerable <BsonDocument> batch = cursor.Current;
                    var batchCount = 0;

                    var batchFailures = new List <Reporting.Failure>();
                    var oListLock     = new object();
                    var oLogLock      = new object();

                    Parallel.ForEach(batch, _parallelOptions, document =>
                    {
                        ObjectId documentId = document["_id"].AsObjectId;
                        DicomDataset ds;

                        try
                        {
                            ds = DicomTypeTranslaterWriter.BuildDicomDataset(document);
                        }
                        catch (Exception e)
                        {
                            // Log any documents we couldn't process due to errors in rebuilding the dataset
                            lock (oLogLock)
                                _logger.Log(LogLevel.Error, e,
                                            "Could not reconstruct dataset from document " + documentId);

                            Interlocked.Increment(ref failedToRebuildCount);

                            return;
                        }

                        // Validate the dataset against our rules
                        IList <Reporting.Failure> documentFailures = ProcessDataset(documentId, ds);

                        if (documentFailures.Any())
                        {
                            lock (oListLock)
                                batchFailures.AddRange(documentFailures);
                        }

                        Interlocked.Increment(ref batchCount);
                    });

                    batchFailures.ForEach(AddToReports);

                    totalProcessed += batchCount;
                    _logger.Debug($"Processed {totalProcessed} documents total");

                    DoneRows(batchCount);
                }
            }

            TimeSpan queryTime = DateTime.Now - start;

            _logger.Info("Processing finished or cancelled, total time elapsed: " + queryTime.ToString("g"));

            _logger.Info("{0} documents were processed in total", totalProcessed);

            if (failedToRebuildCount > 0)
            {
                _logger.Warn("{0} documents could not be reconstructed into DicomDatasets", failedToRebuildCount);
            }

            _logger.Info("Writing out reports...");
            CloseReports();
        }