public void Test_Sequence() { var subDatasets = new List <DicomDataset> { new DicomDataset { new DicomShortString(DicomTag.CodeValue, "CPELVD") } }; var dicomDataset = new DicomDataset { { DicomTag.ProcedureCodeSequence, subDatasets.ToArray() } }; object result = DicomTypeTranslaterReader.GetCSharpValue(dicomDataset, DicomTag.ProcedureCodeSequence); object flat = DicomTypeTranslater.Flatten(result); Console.WriteLine(flat); StringAssert.Contains("CPELVD", (string)flat); StringAssert.Contains("(0008,0100)", (string)flat); }
private void Validate(SeriesMessage message, BsonDocument document) { Assert.False(message == null || document == null); BsonElement element; Assert.True(document.TryGetElement("header", out element)); var docHeader = (BsonDocument)element.Value; Assert.AreEqual(_seriesMessageProps.Count - 3, docHeader.ElementCount); Assert.AreEqual(message.DirectoryPath, docHeader["DirectoryPath"].AsString); Assert.AreEqual(message.NationalPACSAccessionNumber, docHeader["NationalPACSAccessionNumber"].AsString); Assert.AreEqual(message.ImagesInSeries, docHeader["ImagesInSeries"].AsInt32); DicomDataset dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); Assert.NotNull(dataset); BsonDocument datasetDocument = DicomTypeTranslaterReader.BuildBsonDocument(dataset); document.Remove("_id"); document.Remove("header"); Assert.AreEqual(datasetDocument, document); }
public void TestLargeDocumentSplitOk() { GlobalOptions options = MongoDbPopulatorTestHelper.GetNewMongoDbPopulatorOptions(); options.MongoDbPopulatorOptions.MongoDbFlushTime = int.MaxValue / 1000; var adapter = new MongoDbAdapter("ImageProcessor", options.MongoDatabases.ExtractionStoreOptions, "largeDocumentTest"); var processor = new ImageMessageProcessor(options.MongoDbPopulatorOptions, adapter, 2, null); var mockModel = Mock.Of <IModel>(); processor.Model = mockModel; var dataset = new DicomDataset { new DicomUnlimitedText(DicomTag.SelectorUTValue, new string('x', 15 * 1024 * 1024)) }; var largeMessage = new DicomFileMessage { SeriesInstanceUID = "", StudyInstanceUID = "", SOPInstanceUID = "", NationalPACSAccessionNumber = "", DicomFilePath = "", DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(dataset) }; processor.AddToWriteQueue(largeMessage, new MessageHeader(), 1); processor.AddToWriteQueue(largeMessage, new MessageHeader(), 2); }
/// <summary> /// Serializes originalDataset to JSON, deserializes, and re-serializes. /// Verifies that both datasets are equal, and both json serializations are equal! /// </summary> private void VerifyJsonTripleTrip(DicomDataset originalDataset, bool expectFail = false) { string json = DicomTypeTranslater.SerializeDatasetToJson(originalDataset, _jsonDicomConverter); _logger.Debug($"Initial json:\n{json}"); DicomDataset recoDataset = DicomTypeTranslater.DeserializeJsonToDataset(json, _jsonDicomConverter); string json2 = DicomTypeTranslater.SerializeDatasetToJson(recoDataset, _jsonDicomConverter); _logger.Debug($"Final json:\n{json}"); if (expectFail) { Assert.AreNotEqual(json, json2); } else { Assert.AreEqual(json, json2); } // NOTE: Group length elements have been retired from the standard, and have never been included in any JSON conversion. // Remove them here to allow comparison between datasets. originalDataset.RemoveGroupLengths(); if (expectFail) { Assert.False(DicomDatasetHelpers.ValueEquals(originalDataset, recoDataset)); } else { Assert.True(DicomDatasetHelpers.ValueEquals(originalDataset, recoDataset)); } }
public void TestDecimalStringSerialization() { string[] testValues = { ".123", ".0", "5\0", " 0000012.", "00", "00.0", "-.123" }; string[] expectedValues = { "0.123", "0.0", "5", "12.0", "0", "0.0", "-0.123" }; var ds = new DicomDataset { new DicomDecimalString(DicomTag.PatientWeight, "") }; string json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.True(json.Equals("{\"00101030\":{\"vr\":\"DS\"}}")); string convType = _jsonDicomConverter.GetType().Name; for (var i = 0; i < testValues.Length; i++) { ds.AddOrUpdate(DicomTag.PatientWeight, testValues[i]); switch (convType) { case "SmiLazyJsonDicomConverter": json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); string expected = testValues[i].TrimEnd('\0'); Assert.AreEqual($"{{\"00101030\":{{\"vr\":\"DS\",\"val\":\"{expected}\"}}}}", json); break; case "SmiStrictJsonDicomConverter": json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.True(json.Equals("{\"00101030\":{\"vr\":\"DS\",\"Value\":[" + expectedValues[i] + "]}}")); break; case "JsonDicomConverter": if (i == 2 || i == 3) { Assert.Throws <ArgumentException>(() => DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter)); } else { json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.True(json.Equals("{\"00101030\":{\"vr\":\"DS\",\"Value\":[" + expectedValues[i] + "]}}")); } break; default: Assert.Fail($"No test case for {convType}"); break; } } if (_jsonDicomConverter.GetType().Name != "SmiLazyJsonDicomConverter") { return; } // Test all in a single element ds.AddOrUpdate(DicomTag.PatientWeight, testValues); VerifyJsonTripleTrip(ds); }
/// <summary> /// Returns the DBMS specific datatype for storing a dicom tag of the supplied <paramref name="keyword"/> (must be a dicom tag name). /// </summary> /// <param name="keyword"></param> /// <param name="tt"></param> /// <returns></returns> public static string GetDataTypeForTag(string keyword, ITypeTranslater tt) { var tag = DicomDictionary.Default.FirstOrDefault(t => t.Keyword == keyword); if (tag == null) { throw new NotSupportedException("Keyword '" + keyword + "' is not a valid Dicom Tag."); } var type = DicomTypeTranslater.GetNaturalTypeForVr(tag.ValueRepresentations, tag.ValueMultiplicity); return(tt.GetSQLDBTypeForCSharpType(type)); }
private void WriteValue(CsvWriter writer, DicomCFindResponse response, DicomTag tag) { var val = DicomTypeTranslaterReader.GetCSharpValue(response.Dataset, tag); if (val == null) { writer.WriteField(""); } else { writer.WriteField(DicomTypeTranslater.Flatten(val)); } }
private DicomFileMessage GetTestDicomFileMessage(Test testCase = Test.Normal, int numberOfRandomTagsPerDicom = 0) { var msg = new DicomFileMessage() { DicomFilePath = "Path/To/The/File.dcm", NationalPACSAccessionNumber = "1234", SOPInstanceUID = "1.2.3.4", SeriesInstanceUID = "1.2.3.4", StudyInstanceUID = "1.2.3.4", }; DicomDataset ds; Random r = new Random(123); using (var generator = new DicomDataGenerator(r, null, "CT")) ds = generator.GenerateTestDataset(new Person(r), r); ds.AddOrUpdate(DicomTag.AccessionNumber, "1234"); ds.AddOrUpdate(DicomTag.SOPInstanceUID, "1.2.3.4"); ds.AddOrUpdate(DicomTag.SeriesInstanceUID, "1.2.3.4"); ds.AddOrUpdate(DicomTag.StudyInstanceUID, "1.2.3.4"); switch (testCase) { case Test.Normal: ds.AddOrUpdate(DicomTag.PatientID, "010101"); break; case Test.NoPatientTag: ds.Remove(DicomTag.PatientID); break; case Test.EmptyInPatientTag: ds.AddOrUpdate(DicomTag.PatientID, string.Empty); break; case Test.ProperlyFormatedChi: ds.AddOrUpdate(DicomTag.PatientID, "0101010101"); break; default: throw new ArgumentOutOfRangeException("testCase"); } msg.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); return(msg); }
public void SetupSuite() { Globals = GetNewMongoDbPopulatorOptions(); _mongoTestClient = MongoClientHelpers.GetMongoClient(Globals.MongoDatabases.DicomStoreOptions, "MongoDbPopulatorTests"); _mongoTestClient.DropDatabase(TestDbName); TestDatabase = _mongoTestClient.GetDatabase(TestDbName); Globals.MongoDbPopulatorOptions.SeriesQueueConsumerOptions = new ConsumerOptions() { QueueName = "TEST.SeriesQueue", QoSPrefetchCount = 5, AutoAck = false }; Globals.MongoDbPopulatorOptions.ImageQueueConsumerOptions = new ConsumerOptions() { QueueName = "TEST.MongoImageQueue", QoSPrefetchCount = 50, AutoAck = false }; var dataset = new DicomDataset { new DicomUniqueIdentifier(DicomTag.SOPInstanceUID, "1.2.3.4"), new DicomCodeString(DicomTag.Modality, "SR") }; string serialized = DicomTypeTranslater.SerializeDatasetToJson(dataset); TestImageMessage = new DicomFileMessage { DicomFilePath = "Path/To/File", NationalPACSAccessionNumber = "123", SeriesInstanceUID = "TestSeriesInstanceUID", StudyInstanceUID = "TestStudyInstanceUID", SOPInstanceUID = "TestSOPInstanceUID", DicomDataset = serialized }; TestSeriesMessage = new SeriesMessage { DirectoryPath = "Path/To/Series", ImagesInSeries = 123, NationalPACSAccessionNumber = "123", SeriesInstanceUID = "TestSeriesInstanceUID", StudyInstanceUID = "TestStudyInstanceUID", DicomDataset = serialized }; }
private DicomFileMessage GetFileMessageForDataset(DicomDataset dicomDataset) { var root = TestContext.CurrentContext.TestDirectory; var f = Path.GetRandomFileName(); var msg = new DicomFileMessage(root, Path.Combine(root, f + ".dcm")); msg.NationalPACSAccessionNumber = "ABC"; msg.SeriesInstanceUID = dicomDataset.GetString(DicomTag.SeriesInstanceUID); msg.StudyInstanceUID = dicomDataset.GetString(DicomTag.StudyInstanceUID); msg.SOPInstanceUID = dicomDataset.GetString(DicomTag.SOPInstanceUID); msg.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(dicomDataset); return(msg); }
public DicomFileMessage GetDicomFileMessage(DicomDataset ds, string fileSystemRoot, string file) { var toReturn = new DicomFileMessage(fileSystemRoot, file); toReturn.NationalPACSAccessionNumber = "999"; toReturn.StudyInstanceUID = "999"; toReturn.SeriesInstanceUID = "999"; toReturn.SOPInstanceUID = "999"; ds.Remove(DicomTag.PixelData); toReturn.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); return(toReturn); }
public void ExampleUsage_Types() { var tag = DicomDictionary.Default["PatientAddress"]; DatabaseTypeRequest type = DicomTypeTranslater.GetNaturalTypeForVr(tag.DictionaryEntry.ValueRepresentations, tag.DictionaryEntry.ValueMultiplicity); Assert.AreEqual(typeof(string), type.CSharpType); Assert.AreEqual(64, type.MaxWidthForStrings); TypeTranslater tt = new MicrosoftSQLTypeTranslater(); Assert.AreEqual("varchar(64)", tt.GetSQLDBTypeForCSharpType(type)); tt = new OracleTypeTranslater(); Assert.AreEqual("varchar2(64)", tt.GetSQLDBTypeForCSharpType(type)); }
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)); }
public void TestBlankAndNullSerialization() { var dataset = new DicomDataset { new DicomDecimalString(DicomTag.SelectorDSValue, default(string)), new DicomIntegerString(DicomTag.SelectorISValue, ""), new DicomFloatingPointSingle(DicomTag.SelectorFLValue, default(float)), new DicomFloatingPointDouble(DicomTag.SelectorFDValue) }; string json = DicomTypeTranslater.SerializeDatasetToJson(dataset, _jsonDicomConverter); _logger.Debug(json); DicomDataset recoDataset = DicomTypeTranslater.DeserializeJsonToDataset(json, _jsonDicomConverter); Assert.True(DicomDatasetHelpers.ValueEquals(dataset, recoDataset)); }
public DicomFileMessage GetDicomFileMessage(string fileSystemRoot, FileInfo fi) { var toReturn = new DicomFileMessage(fileSystemRoot, fi); toReturn.NationalPACSAccessionNumber = "999"; toReturn.StudyInstanceUID = "999"; toReturn.SeriesInstanceUID = "999"; toReturn.SOPInstanceUID = "999"; toReturn.DicomFileSize = fi.Length; var ds = DicomFile.Open(fi.FullName).Dataset; ds.Remove(DicomTag.PixelData); toReturn.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); return(toReturn); }
/// <summary> /// Creates a new <see cref="DicomFileMessage"/> by reading <paramref name="ds"/> tags /// </summary> /// <param name="ds"></param> /// <param name="dicomFilePath">The full path that <paramref name="ds"/> was read from</param> /// <param name="fileSize">File size if known otherwise null</param> /// <returns></returns> /// <exception cref="ApplicationException">If <paramref name="ds"/> is missing required UIDS or serializing the dataset went wrong</exception> protected DicomFileMessage DicomFileToMessage(DicomDataset ds, string dicomFilePath, long?fileSize) { var IDs = new string[3]; try { // Pre-fetch these to ensure they exist before we go further IDs[0] = ds.GetValue <string>(DicomTag.StudyInstanceUID, 0); IDs[1] = ds.GetValue <string>(DicomTag.SeriesInstanceUID, 0); IDs[2] = ds.GetValue <string>(DicomTag.SOPInstanceUID, 0); if (IDs.Any(string.IsNullOrWhiteSpace)) { throw new DicomDataException("A required ID tag existed but its value was invalid"); } } catch (DicomDataException dde) { throw new ApplicationException("File opened but had a missing ID", dde); } string serializedDataset; try { serializedDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); } catch (Exception e) { throw new ApplicationException("Failed to serialize dataset", e); } return(new DicomFileMessage(_filesystemRoot, dicomFilePath) { //TODO(Ruairidh 04/07) Where are these used? StudyInstanceUID = IDs[0], SeriesInstanceUID = IDs[1], SOPInstanceUID = IDs[2], DicomDataset = serializedDataset, DicomFileSize = fileSize ?? -1 }); }
public void TestLargeMessageNack() { GlobalOptions options = MongoDbPopulatorTestHelper.GetNewMongoDbPopulatorOptions(); options.MongoDbPopulatorOptions.MongoDbFlushTime = int.MaxValue / 1000; var adapter = new MongoDbAdapter("ImageProcessor", options.MongoDatabases.ExtractionStoreOptions, "largeDocumentTest"); var processor = new ImageMessageProcessor(options.MongoDbPopulatorOptions, adapter, 1, null); var mockModel = Mock.Of <IModel>(); processor.Model = mockModel; var dataset = new DicomDataset { new DicomUnlimitedText(DicomTag.SelectorUTValue, new string('x', 16 * 1024 * 1024)) }; string json = DicomTypeTranslater.SerializeDatasetToJson(dataset); var largeMessage = new DicomFileMessage { SeriesInstanceUID = "", StudyInstanceUID = "", SOPInstanceUID = "", NationalPACSAccessionNumber = "", DicomFilePath = "", DicomDataset = json }; Assert.Throws <ApplicationException>(() => processor.AddToWriteQueue(largeMessage, new MessageHeader(), 1)); dataset = new DicomDataset { // Should be ok, getting close to the threshold new DicomUnlimitedText(DicomTag.SelectorUTValue, new string('x', 15 * 1024 * 1024 + 512)) }; json = DicomTypeTranslater.SerializeDatasetToJson(dataset); largeMessage.DicomDataset = json; processor.AddToWriteQueue(largeMessage, new MessageHeader(), 2); Assert.True(processor.AckCount == 1); }
public void BsonRoundTrip_MaskedTags_ConvertedCorrectly() { const string rawJson = "{\"60000010\":{\"vr\":\"US\",\"val\":[128]},\"60000011\":{\"vr\":\"US\",\"val\":[614]},\"60000040\":" + "{\"vr\":\"CS\",\"val\":\"G\"},\"60000050\":{\"vr\":\"SS\",\"val\":[0,0]},\"60000100\":{\"vr\":\"US\",\"val\":[1]}," + "\"60000102\":{\"vr\":\"US\",\"val\":[0]},\"60020010\":{\"vr\":\"US\",\"val\":[512]},\"60020011\":{\"vr\":\"US\",\"val\":[613]}," + "\"60020040\":{\"vr\":\"CS\",\"val\":\"H\"},\"60020050\":{\"vr\":\"SS\",\"val\":[1,2]},\"60020100\":{\"vr\":\"US\",\"val\":[3]}," + "\"60020102\":{\"vr\":\"US\",\"val\":[4]}}"; DicomDataset maskDataset = DicomTypeTranslater.DeserializeJsonToDataset(rawJson); Assert.AreEqual(12, maskDataset.Count()); foreach (DicomItem item in maskDataset.Where(x => x.Tag.DictionaryEntry.Keyword == DicomTag.OverlayRows.DictionaryEntry.Keyword)) { _logger.Debug("{0} {1} - Val: {2}", item.Tag, item.Tag.DictionaryEntry.Keyword, maskDataset.GetString(item.Tag)); } VerifyBsonTripleTrip(maskDataset); }
protected override void ProcessMessageImpl(IMessageHeader header, DicomFileMessage message, ulong tag) { DicomDataset dataset; try { dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); } catch (Exception e) { ErrorAndNack(header, tag, "Could not rebuild DicomDataset from message", e); return; } var toQueue = new QueuedImage(header, tag, message, dataset); lock (_oQueueLock) _imageQueue.Enqueue(toQueue); }
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 TestMaskedTagSerialization() { //Example: OverlayRows element has a masked tag of (60xx,0010) _logger.Info("DicomTag.OverlayRows.DictionaryEntry.MaskTag: " + DicomTag.OverlayRows.DictionaryEntry.MaskTag); const string rawJson = "{\"60000010\":{\"vr\":\"US\",\"val\":[128]},\"60000011\":{\"vr\":\"US\",\"val\":[614]},\"60000040\":" + "{\"vr\":\"CS\",\"val\":\"G\"},\"60000050\":{\"vr\":\"SS\",\"val\":[0,0]},\"60000100\":{\"vr\":\"US\"," + "\"val\":[1]},\"60000102\":{\"vr\":\"US\",\"val\":[0]},\"60020010\":{\"vr\":\"US\",\"val\":[512]}," + "\"60020011\":{\"vr\":\"US\",\"val\":[614]},\"60020040\":{\"vr\":\"CS\",\"val\":\"G\"},\"60020050\":" + "{\"vr\":\"SS\",\"val\":[0,0]},\"60020100\":{\"vr\":\"US\",\"val\":[1]},\"60020102\":{\"vr\":\"US\",\"val\":[0]}}"; DicomDataset maskDataset = DicomTypeTranslater.DeserializeJsonToDataset(rawJson); foreach (DicomItem item in maskDataset.Where(x => x.Tag.DictionaryEntry.Keyword == DicomTag.OverlayRows.DictionaryEntry.Keyword)) { _logger.Debug("{0} {1} - Val: {2}", item.Tag, item.Tag.DictionaryEntry.Keyword, maskDataset.GetString(item.Tag)); } VerifyJsonTripleTrip(maskDataset); }
public void TestIdentifierSwapForGuid(DatabaseType dbType) { var db = GetCleanedServer(dbType); var mapTbl = db.ExpectTable("Map"); //the declaration of what the guid namer table should be var options = new IdentifierMapperOptions(); options.MappingConnectionString = db.Server.Builder.ConnectionString; options.MappingTableName = mapTbl.GetFullyQualifiedName(); options.SwapColumnName = "priv"; options.ReplacementColumnName = "pub"; options.MappingDatabaseType = dbType; var swapper = new ForGuidIdentifierSwapper(); swapper.Setup(options); swapper.Setup(options); swapper.Setup(options); //this isn't just for the lols, this will test both the 'create it mode' and the 'discover it mode' var consumer = new IdentifierMapperQueueConsumer(Mock.Of <IProducerModel>(), swapper); var msg = GetTestDicomFileMessage(); string reason; consumer.SwapIdentifier(msg, out reason); var newDs = DicomTypeTranslater.DeserializeJsonToDataset(msg.DicomDataset); var guidAllocated = newDs.GetValue <string>(DicomTag.PatientID, 0); var dt = mapTbl.GetDataTable(); Assert.AreEqual(1, dt.Rows.Count); //e.g. '841A2E3E-B7C9-410C-A5D1-816B95C0E806' Assert.AreEqual(36, guidAllocated.Length); }
public void ImageProcessor_FailInModalityBatch_AcksWrittenDocuments() { _testOptions.MongoDbPopulatorOptions.FailedWriteLimit = 1; _testOptions.MongoDbPopulatorOptions.MongoDbFlushTime = int.MaxValue / 1000; var testModalities = new[] { "MR", "MR", "MR", "SR", "SR" }; var testAdapter = new MongoTestAdapter(); var processor = new ImageMessageProcessor(_testOptions.MongoDbPopulatorOptions, testAdapter, testModalities.Length + 1, null); var mockModel = new Mock <IModel>(); mockModel.Setup(x => x.BasicAck(It.Is <ulong>(y => y == ulong.MaxValue), It.IsAny <bool>())) .Callback(() => throw new Exception("BasicAck called with delivery tag for CT message")); processor.Model = mockModel.Object; var ds = new DicomDataset(); var msg = new DicomFileMessage { DicomFilePath = "", NationalPACSAccessionNumber = "" }; for (var i = 0; i < testModalities.Length; ++i) { string modality = testModalities[i]; ds.AddOrUpdate(DicomTag.Modality, modality); msg.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); processor.AddToWriteQueue(msg, null, (ulong)i); } ds.AddOrUpdate(DicomTag.Modality, "CT"); msg.DicomDataset = DicomTypeTranslater.SerializeDatasetToJson(ds); Assert.Throws <ApplicationException>(() => processor.AddToWriteQueue(msg, new MessageHeader(), ulong.MaxValue)); Assert.AreEqual(5, processor.AckCount); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); olvFileTags.ClearObjects(); try { var dicom = DicomFile.Open(_fileInfo.FullName, FileReadOption.ReadAll); try { using var renderedImage = new DicomImage(dicom.Dataset).RenderImage().AsSharpImage(); using MemoryStream ms = new(); renderedImage.Save(ms, renderedImage.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); ms.Seek(0, SeekOrigin.Begin); DicomImage = new(ms); pictureBox1.Image = DicomImage; } catch (Exception) { //no picture splitContainer1.Panel1Collapsed = true; } foreach (DicomItem item in dicom.Dataset) { var value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(dicom.Dataset, item)); olvFileTags.AddObject(new TagValueNode(item.Tag, value)); } } catch (Exception exception) { MessageBox.Show(exception.ToString(), "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private bool IsValidLength(IDataLoadEventListener listener, DicomTag tag, string value) { lock (_oDictLock) { if (!_maxTagLengths.ContainsKey(tag)) { DatabaseTypeRequest type = DicomTypeTranslater.GetNaturalTypeForVr( tag.DictionaryEntry.ValueRepresentations, tag.DictionaryEntry.ValueMultiplicity); _maxTagLengths.Add(tag, type.MaxWidthForStrings ?? -1); } //we don't think it's a string or the string is a fine length if (_maxTagLengths[tag] <= 0 || value.Length <= _maxTagLengths[tag]) { return(true); } listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Found value '" + value + "' that was too long for it's VR (" + tag + "). Max length was " + _maxTagLengths[tag] + " supplied value was length " + value.Length)); } return(false); }
private void Validate(DicomFileMessage message, MessageHeader header, BsonDocument document) { Assert.False(message == null || document == null); BsonElement element; Assert.True(document.TryGetElement("header", out element)); var docHeader = (BsonDocument)element.Value; Assert.AreEqual(_imageMessageProps.Count - 3, docHeader.ElementCount); ValidateHeader(message, header, docHeader); DicomDataset dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); Assert.NotNull(dataset); BsonDocument datasetDocument = DicomTypeTranslaterReader.BuildBsonDocument(dataset); document.Remove("_id"); document.Remove("header"); Assert.AreEqual(datasetDocument, document); }
public void JsonSerialization_SerializeBinaryFalse_ContainsEmptyTags() { if (_jsonDicomConverter.GetType().Name != "SmiLazyJsonDicomConverter") { Assert.Pass("Only applicable for SmiLazyJsonDicomConverter"); } var ds = new DicomDataset { new DicomOtherByte(DicomTag.SelectorOBValue, byte.MinValue), new DicomOtherWord(DicomTag.SelectorOWValue, byte.MinValue), new DicomUnknown(DicomTag.SelectorUNValue, byte.MinValue) }; DicomTypeTranslater.SerializeBinaryData = false; string json = DicomTypeTranslater.SerializeDatasetToJson(ds); Assert.DoesNotThrow(() => JToken.Parse(json)); DicomDataset recoDs = DicomTypeTranslater.DeserializeJsonToDataset(json); Assert.AreEqual(ds.Count(), recoDs.Count()); AssertBlacklistedNulls(recoDs); }
public override void AddToWriteQueue(SeriesMessage message, IMessageHeader header, ulong deliveryTag) { // Only time we are not processing is if we are shutting down anyway if (IsStopping) { return; } if (Model == null) { throw new ApplicationException("Model needs to be set before messages can be processed"); } DicomDataset dataset; try { dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); } catch (Exception e) { throw new ApplicationException("Could not deserialize json to dataset", e); } BsonDocument datasetDoc; try { datasetDoc = DicomTypeTranslaterReader.BuildBsonDocument(dataset); } catch (Exception e) { throw new ApplicationException("Exception converting dataset to BsonDocument", e); } BsonDocument bsonHeader = MongoDocumentHeaders.SeriesDocumentHeader(message); BsonDocument document = new BsonDocument() .Add("header", bsonHeader) .AddRange(datasetDoc); int docByteLength = document.ToBson().Length; if (docByteLength > MaxDocumentSize) { throw new ApplicationException($"BsonDocument was larger than the max allowed size (have {docByteLength}, max is {MaxDocumentSize})"); } var forceProcess = false; lock (LockObj) { ToProcess.Enqueue(new Tuple <BsonDocument, ulong>(document, deliveryTag)); if (ToProcess.Count >= MaxQueueSize) { forceProcess = true; } } if (!forceProcess) { return; } Logger.Debug("SeriesMessageProcessor: Max queue size reached, calling ProcessQueue"); ProcessQueue(); }
private static void RunBatch(IPersonCollection identifiers, ProgramOptions opts, Random r, DataTable[] batches, IBulkCopy[] uploaders) { Stopwatch swGeneration = new Stopwatch(); Stopwatch swReading = new Stopwatch(); Stopwatch swUploading = new Stopwatch(); try { using (var dicomGenerator = GetDataGenerator(opts, identifiers, r, out _)) { for (int i = 0; i < opts.NumberOfStudies; i++) { swGeneration.Start(); var p = identifiers.People[r.Next(identifiers.People.Length)]; var ds = dicomGenerator.GenerateStudyImages(p, out Study s); swGeneration.Stop(); foreach (DicomDataset dataset in ds) { var rows = new DataRow[batches.Length]; for (int j = 0; j < batches.Length; j++) { rows[j] = batches[j].NewRow(); } swReading.Start(); foreach (DicomItem item in dataset) { var column = DicomTypeTranslaterReader.GetColumnNameForTag(item.Tag, false); var value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(dataset, item)); foreach (DataRow row in rows) { if (row.Table.Columns.Contains(column)) { row[column] = value ?? DBNull.Value; } } } for (int j = 0; j < batches.Length; j++) { batches[j].Rows.Add(rows[j]); } swReading.Stop(); } //every 100 and last batch if (i % 100 == 0 || i == opts.NumberOfStudies - 1) { swUploading.Start(); for (var j = 0; j < uploaders.Length; j++) { uploaders[j].Upload(batches[j]); batches[j].Rows.Clear(); } swUploading.Stop(); Console.WriteLine($"{DateTime.Now} Done {i} studies"); } } } } finally { for (var i = 0; i < uploaders.Length; i++) { uploaders[i].Dispose(); batches[i].Dispose(); } } Console.WriteLine("Total time Generating Dicoms:" + swGeneration.Elapsed); Console.WriteLine("Total time Reading Dicoms:" + swReading.Elapsed); Console.WriteLine("Total time Uploading Records:" + swUploading.Elapsed); }
public void TestLongIntegerStringSerialization() { var ds = new DicomDataset(); string[] testValues = { "123", // Normal value "-123", // Normal value "00000000", // Technically allowed "+123", // Strange, but allowed "00001234123412" // A value we can fix and parse }; // Values which will be 'fixed' by the SmiStrictJsonDicomConverter string[] expectedValues = { "123", "-123", "0", "123", "1234123412" }; string convType = _jsonDicomConverter.GetType().Name; for (var i = 0; i < testValues.Length; i++) { ds.AddOrUpdate(new DicomIntegerString(DicomTag.SelectorAttributePrivateCreator, testValues[i])); switch (convType) { case "SmiLazyJsonDicomConverter": string json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.AreEqual($"{{\"00720056\":{{\"vr\":\"IS\",\"val\":\"{testValues[i]}\"}}}}", json); break; case "SmiStrictJsonDicomConverter": case "JsonDicomConverter": json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.AreEqual(("{\"00720056\":{\"vr\":\"IS\",\"Value\":[" + expectedValues[i] + "]}}"), json); break; default: Assert.Fail($"No test case for {convType}"); break; } } // Value larger than an Int32 ds.AddOrUpdate(new DicomIntegerString(DicomTag.SelectorAttributePrivateCreator, "10001234123412")); switch (convType) { case "JsonDicomConverter": // Default converter will try and force it into an int -> OverflowException Assert.Throws <OverflowException>(() => DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter)); break; case "SmiStrictJsonDicomConverter": // Strict converter will try and 'fix' it, before throwing a FormatException Assert.Throws <FormatException>(() => DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter)); break; case "SmiLazyJsonDicomConverter": // Our converter doesn't enforce types, so this should pass string json = DicomTypeTranslater.SerializeDatasetToJson(ds, _jsonDicomConverter); Assert.AreEqual("{\"00720056\":{\"vr\":\"IS\",\"val\":\"10001234123412\"}}", json); break; default: Assert.Fail($"No test case for {convType}"); break; } }