public async Task ImportAsync(DicomDataset dicom) { //var patient = ToPatient(dicom); //var study = ToStudy(dicom); //var series = ToSeries(dicom); //var image = ToImage(dicom); //image.ReferencedFileID = await _fileRepo.SaveAsync(dicom); //series.ImageCollection.Add(image); //study.SeriesCollection.Add(series); //study.Paitent = patient; //await _studyRepo.AddOrUpdateImageAsync(study); string studyUID = dicom.GetSingleValue <string>(DicomTag.StudyInstanceUID); DicomStudy study = await _studyRepo.GetAsync(studyUID); // Create one study if the studyUID doesn't exist. if (study is NotFoundStudy) { study = DicomStudy.CreateFromDicom(dicom); } else { study.AddImageItem(dicom); } }
public async Task <DicomStudy> Add(DicomStudy study) { var collection = _context.Studies; await collection.InsertOneAsync(study).ConfigureAwait(false); return(study); }
private void WriteStudyObjectToDb(DicomStudy study, PatientInformation patient) { if (study != null && patient != null) { AppSingleton.DatabaseService.SaveStudy(study, patient); } }
/// <summary> /// Read the structure set from a DICOM dataset. /// </summary> /// <param name="ds"></param> /// <returns></returns> public static RadiotherapyStruct Read(DicomDataset ds) { var patient = DicomPatient.Read(ds); var study = DicomStudy.Read(ds); var series = DicomRTSeries.Read(ds); var equipment = DicomEquipment.Read(ds); var structureSet = DicomRTStructureSet.Read(ds); // DicomRTStructureSetROI.RoiNumber is the primary key. We try and locate the first observation and roiContour data for eaach // RoiNumber encountered. var structures = DicomRTStructureSetROI.Read(ds); var contoursData = DicomRTContour.Read(ds); var observations = DicomRTObservation.Read(ds); var allContours = new List <RadiotherapyContour>(); foreach (var s in structures) { var roiContours = contoursData.FirstOrDefault(c => c.ReferencedRoiNumber == s.Key); var roiObservation = observations.FirstOrDefault(o => o.ReferencedRoiNumber == s.Key); // we insist both must exist if (roiContours != null && roiObservation != null) { allContours.Add(new RadiotherapyContour(roiContours, s.Value, roiObservation)); } } return(new RadiotherapyStruct(structureSet, patient, equipment, study, series, allContours)); }
static async Task InsertStudyToDbAsync(DicomStudy study) { var client = new MongoClient(connUrl); IMongoDatabase db = client.GetDatabase(dbName); var collection = db.GetCollection <DicomStudy>(collectionName); var findOnes = collection.Find(s => s.StudyUID == study.StudyUID); if (findOnes.Any()) { var one = findOnes.First(); foreach (var item in study.SeriesCollection) { if (one.SeriesCollection.Any(s => s.SeriesUID == item.SeriesUID) == false) { // one.SeriesCollection.Add(item); } } await collection.ReplaceOneAsync(s => s.StudyUID == study.StudyUID, one); } else { await collection.InsertOneAsync(study); } }
public void SaveStudy(DicomStudy study, PatientInformation patient) { if (study == null) { throw new ArgumentNullException(nameof(study)); } if (patient == null) { throw new ArgumentNullException(nameof(patient)); } if (string.IsNullOrWhiteSpace(patient.PatientKey)) { throw new ArgumentNullException("patient.PatientKey"); } string strNow = DatabaseDateString(DateTime.Now); var cmd = _conn.CreateCommand(); cmd.CommandText = "INSERT INTO Study(ID, StudyDate, StudyDescription, PatientID) VALUES (@ID, @studydate, @studydescription, @patientID)"; cmd.Prepare(); Guid studyID = Guid.NewGuid(); study.ID = studyID; cmd.Parameters.AddWithValue("@ID", DatabaseGuidString(studyID)); cmd.Parameters.AddWithValue("@studydate", DatabaseDateString(GetDicomStudyDate(study))); cmd.Parameters.AddWithValue("@studydescription", study.StudyDescription); cmd.Parameters.AddWithValue("@patientID", patient.PatientKey); cmd.ExecuteNonQuery(); }
/// <summary> /// Called when we open an existing structure set. Note we preserve the SeriesUID and the Series Description. (this is arguable if it /// did not originate from us) /// </summary> /// <param name="structureSet"></param> /// <param name="patient"></param> /// <param name="equipment"></param> /// <param name="study"></param> /// <param name="series"></param> /// <param name="contours"></param> public RadiotherapyStruct( DicomRTStructureSet structureSet, DicomPatient patient, DicomEquipment equipment, DicomStudy study, DicomRTSeries series, IList <RadiotherapyContour> contours) { RTSeries = series; Patient = patient; Equipment = equipment; Study = study; StructureSet = structureSet; Contours = contours; }
/// <summary> /// Add or update an image record (contains in <see cref="DicomStudy"/>) to database. /// </summary> /// <param name="study"></param> /// <returns></returns> public async Task AddOrUpdateImageAsync(DicomStudy study) { // Check image record. if (CheckImageLevel(study) == false) { return; } var collection = GetDbCollection(); await InsertImageAsync(study, collection); }
public List <DicomSeries> RetrieveStudySeriesDetails(DicomStudy study) { if (study == null) { throw new ArgumentNullException(nameof(study)); } List <DicomSeries> list = new List <DicomSeries>(); try { var cmd = _conn.CreateCommand(); cmd.CommandText = @"SELECT ID, SeriesDate, SeriesDescription, StudyID, Thumbnail FROM Series WHERE StudyID=@studyID"; cmd.Prepare(); cmd.Parameters.AddWithValue("@studyID", study.ID.ToString()); var reader = cmd.ExecuteReader(); string ID; string seriesDate; string seriesDescription; string studyID; byte[] thumbnail; while (reader.Read()) { ID = GetColumnValue <string>(reader, "ID"); seriesDate = GetColumnValue <string>(reader, "SeriesDate"); seriesDescription = GetColumnValue <string>(reader, "SeriesDescription"); studyID = GetColumnValue <string>(reader, "StudyID"); thumbnail = GetColumnValue <byte[]>(reader, "Thumbnail"); var series = CreateSeries(ID, seriesDate, seriesDescription, study, thumbnail); list.Add(series); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.Assert(false, e.Message); } return(list); }
public void SaveSeries(DicomSeries series, DicomStudy study, PatientInformation patient, string studyFolderPath) { if (series == null) { throw new ArgumentNullException(nameof(series)); } if (study == null) { throw new ArgumentNullException(nameof(study)); } if (patient == null) { throw new ArgumentNullException(nameof(patient)); } if (string.IsNullOrWhiteSpace(patient.PatientKey)) { throw new ArgumentNullException("patient.PatientKey"); } string strNow = DatabaseDateString(DateTime.Now); var pngFileList = System.IO.Directory.EnumerateFiles(studyFolderPath, series.SeriesFileName + "*.png").ToList(); string pngFile = pngFileList.Count == 1 ? pngFileList[0] : ""; byte[] thumbnail = Utilities.CreateThumbnailFromFile(pngFile, 64, 64); var cmd = _conn.CreateCommand(); cmd.CommandText = "INSERT INTO Series(ID, SeriesDate, SeriesDescription, StudyID, DrapeUsedAtCapture, Thumbnail) VALUES (@ID, @seriesdate, @seriesdescription, @studyID, @drapeusedatcapture, @thumbnail)"; cmd.Prepare(); Guid seriesID = Guid.NewGuid(); series.ID = seriesID; cmd.Parameters.AddWithValue("@ID", DatabaseGuidString(series.ID)); cmd.Parameters.AddWithValue("@seriesdate", DatabaseDateString(ParseDicomDateToDateTime(series.SeriesDate))); cmd.Parameters.AddWithValue("@seriesdescription", series.SeriesDescription); cmd.Parameters.AddWithValue("@studyID", DatabaseGuidString(study.ID)); cmd.Parameters.AddWithValue("@drapeusedatcapture", series.DrapeUsedAtCapture ? 1 : 0); cmd.Parameters.AddWithValue("@drapeusedatcapture", series.DrapeUsedAtCapture ? 1 : 0); cmd.Parameters.Add("@thumbnail", DbType.Binary, 20).Value = thumbnail; cmd.ExecuteNonQuery(); }
private async Task InsertImageAsync(DicomStudy study, IMongoCollection <DicomStudy> collection) { // Has study? var findOnes = collection.Find(s => s.StudyUID == study.StudyUID); if (findOnes.Any() == false) { await collection.InsertOneAsync(study); return; } // Has series? var one = findOnes.First(); var series = one.SeriesCollection.FirstOrDefault(s => s.SeriesUID == study.SeriesCollection.First().SeriesUID); if (series == null) { var addSeries = Builders <DicomStudy> .Update.Push(s => s.SeriesCollection, study.SeriesCollection.FirstOrDefault()); await collection.UpdateOneAsync(s => s.StudyUID == study.StudyUID, addSeries); return; } // Has image? //var theImage = study.SeriesCollection.First().ImageCollection.FirstOrDefault(); //var image = series.ImageCollection.FirstOrDefault(img => img.SOPInstanceUID == theImage.SOPInstanceUID); //if (image == null) //{ // var filter = Builders<DicomStudy>.Filter.Where(s => s.StudyUID == study.StudyUID) // & Builders<DicomStudy>.Filter.Where(d => d.SeriesCollection.Any(r => r.SeriesUID == series.SeriesUID)); // var updateSeries = Builders<DicomStudy>.Update.Push(s => s.SeriesCollection[-1].ImageCollection, theImage); // await collection.UpdateOneAsync(filter, updateSeries); // return; //} //// Need change image record? //if (image.Equals(theImage) == false) //{ // image.ImageNumber = theImage.ImageNumber; // image.ReferencedFileID = theImage.ReferencedFileID; // var imageFilter = Builders<DicomStudy>.Filter.Where(s => s.StudyUID == study.StudyUID) // & Builders<DicomStudy>.Filter.Where(d => d.SeriesCollection.Any(r => r.SeriesUID == series.SeriesUID)); // var updateImage = Builders<DicomStudy>.Update.Set(ss => ss.SeriesCollection[-1].ImageCollection, series.ImageCollection); // await collection.UpdateOneAsync(imageFilter, updateImage); //} }
/// <summary> /// This method will split dicom file to series and add to study object /// </summary> /// <param name="study"></param> /// <param name="dicomFile"></param> private void FindMatchingSeriesOrSplit(List <DicomStudy> studies, DicomFile dicomFile) { var seriesInstanceUID = dicomFile.Dataset.Get <string>(DicomTag.SeriesInstanceUID); var studyInstanceUID = dicomFile.Dataset.Get <string>(DicomTag.StudyInstanceUID); bool matchingStudyFound = false; //first add study to list foreach (var study in studies) { //check if study is in list if (study.StudyInstanceUid == studyInstanceUID) { matchingStudyFound = true; bool matchingSeriesFound = false; foreach (var series in study.Series) { if (series.SeriesInstanceUid == seriesInstanceUID) { matchingSeriesFound = true; //if series found then add the dicomfile to instance series.Instances.Add(dicomFile); } } //add series if matching series not found if (matchingSeriesFound == false) { DicomSeries dicomSeries = new DicomSeries(dicomFile); study.Series.Add(dicomSeries); } } } //check if study is in list if not add new study and add the series and instance if (matchingStudyFound == false) { DicomStudy dicomStudy = new DicomStudy(dicomFile); DicomSeries dicomSeries = new DicomSeries(dicomFile); dicomSeries.Instances.Add(dicomFile); dicomStudy.Series.Add(dicomSeries); studies.Add(dicomStudy); } }
/// <summary> /// Serialize the structure set to a DICOM dataset. /// </summary> /// <param name="ds"></param> /// <param name="s"></param> public static void Write(DicomDataset ds, RadiotherapyStruct s) { DicomPatient.Write(ds, s.Patient); DicomStudy.Write(ds, s.Study); DicomRTSeries.Write(ds, s.RTSeries); DicomEquipment.Write(ds, s.Equipment); DicomRTStructureSet.Write(ds, s.StructureSet); // For each ROI, gather the contours, observations and roi details into seperate arrays and serialize var rois = s.Contours.Select(c => c.StructureSetRoi); var contours = s.Contours.Select(c => c.DicomRtContour); var observations = s.Contours.Select(c => c.DicomRtObservation); DicomRTStructureSetROI.Write(ds, rois); DicomRTContour.Write(ds, contours); DicomRTObservation.Write(ds, observations); }
private void WriteSeriesObjectToDb(DicomSeries series, DicomStudy study, PatientInformation patient, string studyFolderPath) { if (series != null && study != null && patient != null && !string.IsNullOrWhiteSpace(studyFolderPath)) { AppSingleton.DatabaseService.SaveSeries(series, study, patient, studyFolderPath); var seriesImageFiles = System.IO.Directory.EnumerateFiles(studyFolderPath, series.SeriesFileName + "*.*"); foreach (string filename in seriesImageFiles) { if (filename.EndsWith(".txt") || filename.EndsWith(".png")) { continue; } AppSingleton.DatabaseService.SaveSeriesImageFile(series, studyFolderPath, filename); } } }
private DateTime GetDicomStudyDate(DicomStudy study) { if (study == null) { throw new ArgumentNullException(nameof(study)); } if (!string.IsNullOrWhiteSpace(study.StudyDateLongForm)) { DateTime studyDate; if (!DateTime.TryParse(study.StudyDateLongForm, out studyDate)) { studyDate = ParseDicomDateToDateTime(study.StudyDate); } return(studyDate); } else { return(ParseDicomDateToDateTime(study.StudyDate)); } }
private bool CheckImageLevel(DicomStudy study) { var theImage = study.SeriesCollection.FirstOrDefault()?.ImageCollection.FirstOrDefault(); return(theImage != null); }
public async Task Update(DicomStudy study) { var collection = _context.Studies; await collection.ReplaceOneAsync(s => s.StudyUID == study.StudyUID, study); }
public DicomSeries CreateSeries(string ID, string seriesDate, string seriesDescription, DicomStudy study, byte[] thumbnail) { DateTime seriesDateTime; DateTime.TryParse(seriesDate, out seriesDateTime); var series = new DicomSeries(study.PatientID.ToString(), study, null) { ID = Guid.Parse(ID), SeriesDate = seriesDateTime.ToShortDateString(), SeriesTime = seriesDateTime.ToShortTimeString(), SeriesDescription = seriesDescription, }; series.DicomSeriesDescription.Thumbnail = Convert.ToBase64String(thumbnail); return(series); }
private static DicomStudy ToStudy(DicomDataset record) { // This cannot work. return(DicomStudy.CreateFromDicom(record)); }
public async Task AddOrUpdateSeriesAsync(DicomStudy study) { await InsertStudyToDbAsync(study); }