public void TestConvertColorArrayValid(string colorArrayString) { var expectedRGBColors = new[] { new RGBColorOption(0xFF, null, null), new RGBColorOption(0xFF, 0x80, null), new RGBColorOption(0xFF, 0x80, 0x40) }; var actualRGBColors = RTConverters.GetRGBColorArrayFromString(colorArrayString); Assert.AreEqual(expectedRGBColors, actualRGBColors); }
public void TestConvertStringArray(string stringArrayString) { var expectedStrings = new[] { "External", "parotid_l", "parotid_r" }; var actualStrings = RTConverters.GetStringArrayFromString(stringArrayString); Assert.AreEqual(expectedStrings, actualStrings); }
public void TestConvertColorInvalid(string colorString) { var exception = Assert.Throws <ArgumentException>(() => RTConverters.ConvertColor(colorString)); Assert.True(exception.Message.Contains("Cannot parse ")); Assert.True(exception.Message.Contains(" is not a hexadecimal string")); }
public void TestConvertBoolArrayInvalid() { var exception = Assert.Throws <ArgumentException>(() => RTConverters.GetBoolArrayFromString(" [ True, F, 1 ] ")); Assert.True(exception.Message.Contains("Cannot parse array at index: 2")); }
public void TestConvertBoolArrayLastMissingValid(string boolArrayString) { var expectedBools = new bool?[] { true, false, null }; var actualBools = RTConverters.GetBoolArrayFromString(boolArrayString); Assert.AreEqual(expectedBools, actualBools); }
public void TestConvertBoolValid( bool expected, string value) { var actual = RTConverters.ConvertBool(value); Assert.AreEqual(expected, actual); }
public void TestConvertColorArrayInvalid() { var exception = Assert.Throws <ArgumentException>(() => RTConverters.GetRGBColorArrayFromString(" [ #ff, FF80HH , #ff8040 ] ")); Assert.True(exception.Message.Contains("Cannot parse array at index: 2")); }
public void TestConvertColorValid( byte r, byte?g, byte?b, string colorString) { var expectedRGBColour = new RGBColorOption(r, g, b); var actualRGBColor = RTConverters.ConvertColor(colorString); Assert.AreEqual(expectedRGBColour, actualRGBColor); }
private string StatusRegToString(ushort statusReg) { List <int> bitList = RTConverters.UInt16ToIntList(statusReg); string res = ""; List <String> bitStrings = new List <string>(); int tmp = statusReg; tmp &= ~RTConst.STAT_BATT_MASK; statusReg = (ushort)tmp; if (statusReg == 0) { return("(none)"); } bitList = RTConverters.UInt16ToIntList(statusReg); if (bitList.Contains(RTConst.STAT_MEMFULL_LSH)) { bitStrings.Add("mem full"); } if (bitList.Contains(RTConst.STAT_ETA_ALARM_LSH)) { bitStrings.Add("ETA alarm"); } if (bitList.Contains(RTConst.STAT_BIN_ALARM_LSH)) { bitStrings.Add("BIN alarm"); } if (bitList.Contains(RTConst.STAT_MKT_ALARM_LSH)) { bitStrings.Add("MKT alarm"); } if (bitList.Contains(RTConst.STAT_SHL_ALARM_LSH)) { bitStrings.Add("SHL alarm"); } int i = 0; int last = bitStrings.Count - 1; foreach (string s in bitStrings) { res += s; if (i < last) { res += ", "; } } return(res); }
private string TryGetTempConversion(ushort temp) { try { return(RTConverters.FixedToString(temp)); } catch { } return("(invalid)"); }
private void AddRegItem(uint addr, string regName, ushort regValue, string noteStr) { string [] strArr = new string[] { RTConverters.HexUInt16((ushort)addr), regName, RTConverters.HexUInt16(regValue), regValue.ToString(), noteStr }; RegList.Items.Add(new ListViewItem(strArr)); }
/// <summary> /// Handle options, if no errors. /// </summary> /// <param name="opts">Command line options</param> /// <returns>Exit code.</returns> static int RunOptionsAndReturnExitCode(Options opts) { try { RTConverters.ConvertNiftiToDicom(opts.InFile, opts.ReferenceSeries, opts.StructureNames, opts.StructureColors, opts.FillHoles, opts.ROIInterpretedTypes, opts.OutFile, opts.ModelNameAndVersion, opts.Manufacturer, opts.Interpreter); Console.WriteLine($"Successfully written {opts.OutFile}"); } catch (Exception e) { Console.Error.WriteLine($"Error while processing: {e}"); return(1); } return(0); }
private void ErrorItem(uint addr, string regStr, string errorStr) { RegList.Items.Add(new ListViewItem(new string[] { RTConverters.HexUInt16((ushort)addr), regStr, "0", "0", errorStr })); }
private void ReadRegs() { SetupGrid(); ushort[] regs; uint curAddr; string tmp = ""; curAddr = RTRegs.CONTROL; try { regs = mTag.ReadMultipleRegs(curAddr, 2); tmp = ""; if ((regs[0] & (RTConst.CTL_LOGEN_MASK | RTConst.CTL_DLYEN_MASK)) != 0) { tmp = "Enabled: "; if ((regs[0] & RTConst.CTL_LOGEN_MASK) != 0) { tmp += "logging"; } if ((regs[0] & RTConst.CTL_DLYEN_MASK) != 0) { if ((regs[0] & RTConst.CTL_LOGEN_MASK) != 0) { tmp += ", "; } tmp += "start delay"; } } else { tmp = "Logging not enabled"; } AddRegItem(curAddr++, "Control", regs[0], tmp); AddRegItem(curAddr, "Sampling delay", regs[1], "Seconds"); } catch (Exception e) { ErrorItem(curAddr++, "Control enable", e.Message); ErrorItem(curAddr, "Sampling delay", e.Message); } curAddr = RTRegs.BIN_ENABLE; try { regs = mTag.ReadMultipleRegs(curAddr, 3); AddRegItem(curAddr++, "BIN enable", regs[0], "Enabled: " + RTConverters.IntListToString(RTConverters.UInt16ToIntList(regs[0]))); AddRegItem(curAddr++, "BIN sample store", regs[1], "Enabled: " + RTConverters.IntListToString(RTConverters.UInt16ToIntList(regs[1]))); AddRegItem(curAddr, "BIN timestamp store", regs[2], "Enabled: " + RTConverters.IntListToString(RTConverters.UInt16ToIntList(regs[2]))); } catch (Exception e) { ErrorItem(curAddr++, "BIN enable", e.Message); ErrorItem(curAddr++, "BIN sample store", e.Message); ErrorItem(curAddr, "BIN timestamp store", e.Message); } curAddr = RTRegs.SAMPLES_NUM; try { regs = mTag.ReadMultipleRegs(curAddr, 1); AddRegItem(curAddr, "Sample count", regs[0], ""); } catch (Exception e) { ErrorItem(curAddr, "Sample count", e.Message); } curAddr = RTRegs.BIN_SAMPLETIME_BASE; try { regs = mTag.ReadMultipleRegs(curAddr, 1); AddRegItem(curAddr, "Sample time[0]", regs[0], "Seconds"); } catch (Exception e) { ErrorItem(curAddr, "Sample time[0]", e.Message); } curAddr = RTRegs.BIN_THRESHOLD_BASE; try { regs = mTag.ReadMultipleRegs(curAddr, 1); AddRegItem(curAddr, "BIN threshold[0]", regs[0], "Celsius: " + TryGetTempConversion(regs[0])); } catch (Exception e) { ErrorItem(curAddr, "BIN threshold[0]", e.Message); } curAddr = RTRegs.BIN_HLIMIT_BASE; try { regs = mTag.ReadMultipleRegs(curAddr, 1); AddRegItem(curAddr, "BIN high limit[0]", regs[0], "Celsius: " + TryGetTempConversion(regs[0])); } catch (Exception e) { ErrorItem(curAddr, "BIN high limit[0]", e.Message); } curAddr = RTRegs.BIN_ALARM; try { regs = mTag.ReadMultipleRegs(curAddr, 1); AddRegItem(curAddr, "BIN alarms", regs[0], "Set: " + RTConverters.IntListToString(RTConverters.UInt16ToIntList(regs[0]))); } catch (Exception e) { ErrorItem(curAddr, "BIN alarms", e.Message); } curAddr = RTRegs.STATUS; try { ushort batt; regs = mTag.ReadMultipleRegs(curAddr, 1); batt = (ushort)(regs[0] & RTConst.STAT_BATT_MASK); //AddRegItem(curAddr, "Status", regs[0], "Set: " + RTConverters.IntListToString(RTConverters.UInt16ToIntList(regs[0]))); AddRegItem(curAddr, "Status", regs[0], "Set: " + StatusRegToString(regs[0])); AddRegItem(curAddr, "STAT: battery", batt, "Level = " + batt.ToString()); } catch (Exception e) { ErrorItem(curAddr, "Status", e.Message); } }
/// <summary> /// Test Nifti file to DICOM-RT translation. /// </summary> /// <param name="niftiFilename">Source Nifti file.</param> /// <param name="referenceSeries">Reference DICOM series folder.</param> /// <param name="structureNames">List of structure names for DICOM-RT.</param> /// <param name="structureColors">List of structure colours for DICOM-RT.</param> /// <param name="fillHoles">List of fill hole flags for DICOM-RT.</param> /// <param name="roiInterpretedType">List of roiInterpretedType for DICOM-RT.</param> /// <param name="debugFolderName">If present, create a full set of debug images.</param> /// <param name="testVolumesMatch">If true, check the volumes match.</param> /// <param name="expectedNiftiFilename">Expect volume to match Nifti file.</param> public static void DoTestNiftiToDicom( string niftiFilename, string referenceSeries, string[] structureNames, RGBColorOption?[] structureColors, bool?[] fillHoles, ROIInterpretedType[] roiInterpretedTypes, string debugFolderName, bool testVolumesMatch, string expectedNiftiFilename) { var outputFileName = Path.GetRandomFileName() + ".dcm"; RTConverters.ConvertNiftiToDicom( niftiFilename, referenceSeries, structureNames, structureColors, fillHoles, roiInterpretedTypes, outputFileName, "modelX:1", "manufacturer", "interpreter"); // Open the newly created DICOM-RT file var dicomRTFile = DicomFile.Open(outputFileName); // Get the medical volume from the reference var acceptanceTests = new ModerateGeometricAcceptanceTest(string.Empty, string.Empty); var referenceMedicalVolumes = MedIO.LoadAllDicomSeriesInFolderAsync(referenceSeries, acceptanceTests).Result; Assert.AreEqual(1, referenceMedicalVolumes.Count); var referenceMedicalVolume = referenceMedicalVolumes.First().Volume; var referenceIdentifiers = referenceMedicalVolume.Identifiers.First(); // Extract the RTStruct from the DICOM-RT file var reloaded = RtStructReader.LoadContours( dicomRTFile.Dataset, referenceMedicalVolume.Volume.Transform.DicomToData, referenceIdentifiers.Series.SeriesInstanceUid, referenceIdentifiers.Study.StudyInstanceUid); Assert.IsNotNull(reloaded); var reloadedRTStruct = reloaded.Item1; Assert.AreEqual(referenceIdentifiers.Patient.Id, reloadedRTStruct.Patient.Id); Assert.AreEqual(referenceIdentifiers.Study.StudyInstanceUid, reloadedRTStruct.Study.StudyInstanceUid); Assert.AreEqual(DicomRTSeries.RtModality, reloadedRTStruct.RTSeries.Modality); // Load the nifti file var sourceVolume = MedIO.LoadNiftiAsByte(expectedNiftiFilename); // Split this volume from segment id to a set of mask volumes var labels = VolumeMetadataMapper.MultiLabelMapping(sourceVolume, structureNames.Length); // Make tuples of mask volume, color, names, fill holes. var volumesWithMetadata = VolumeMetadataMapper.MapVolumeMetadata(labels, structureNames, structureColors, fillHoles, roiInterpretedTypes).ToArray(); Assert.AreEqual(structureNames.Length, reloadedRTStruct.Contours.Count); for (int i = 0; i < reloadedRTStruct.Contours.Count; i++) { var contourVolumes = new Volume3D <byte>( referenceMedicalVolume.Volume.DimX, referenceMedicalVolume.Volume.DimY, referenceMedicalVolume.Volume.DimZ, referenceMedicalVolume.Volume.SpacingX, referenceMedicalVolume.Volume.SpacingY, referenceMedicalVolume.Volume.SpacingZ, referenceMedicalVolume.Volume.Origin, referenceMedicalVolume.Volume.Direction); contourVolumes.Fill(reloadedRTStruct.Contours[i].Contours, (byte)1); var v = volumesWithMetadata[i]; if (!string.IsNullOrWhiteSpace(debugFolderName)) { for (var z = 0; z < referenceMedicalVolume.Volume.DimZ; z++) { var actualFileName = Path.Combine(debugFolderName, "actual", $"slice_{v.name}_{z}.png"); var actualSlice = contourVolumes.Slice(SliceType.Axial, z); actualSlice.SaveBinaryMaskToPng(actualFileName); var expectedFileName = Path.Combine(debugFolderName, "expected", $"slice_{v.name}_{z}.png"); var expectedSlice = v.volume.Slice(SliceType.Axial, z); expectedSlice.SaveBinaryMaskToPng(expectedFileName); } } if (testVolumesMatch) { VolumeAssert.AssertVolumesMatch(v.volume, contourVolumes, $"Loaded mask {i}"); } Assert.AreEqual(v.name, reloadedRTStruct.Contours[i].StructureSetRoi.RoiName, $"Loaded mask name {i}"); var expectedColor = Tuple.Create(v.color.R, v.color.G, v.color.B); Assert.AreEqual(expectedColor, reloadedRTStruct.Contours[i].DicomRtContour.RGBColor, $"Loaded color {i}"); } }
public void TestConvertBoolInvalid(string value) { var exception = Assert.Throws <ArgumentException>(() => RTConverters.ConvertBool(value)); Assert.True(exception.Message.Contains("Cannot parse bool ")); }