public static IEnumerable <(string ChannelId, DicomFolderContents Content)> DecompressSegmentationData(Stream inputStream) { var files = DicomCompressionHelpers.DecompressPayload(inputStream); var dictionaryChannelToFiles = new Dictionary <string, List <byte[]> >(); foreach ((string filename, byte[] data) in files) { var channelId = filename.Split(DicomCompressionHelpers.ChannelIdAndDicomSeriesSeparator).First(); if (!dictionaryChannelToFiles.ContainsKey(channelId)) { dictionaryChannelToFiles.Add(channelId, new List <byte[]>()); } dictionaryChannelToFiles[channelId].Add(data); } var result = new List <(string ChannelId, DicomFolderContents content)>(); foreach (var item in dictionaryChannelToFiles) { var fileAndPaths = item.Value .Select(x => DicomFileAndPath.SafeCreate(new MemoryStream(x), string.Empty)) .ToList(); result.Add((item.Key, DicomFolderContents.Build(fileAndPaths))); } return(result); }
/// <inheritdoc /> public async Task <(string segmentationId, IEnumerable <DicomFile> postedImages)> StartSegmentationAsync( string modelId, IEnumerable <ChannelData> channelIdsAndDicomFiles) { if (channelIdsAndDicomFiles == null) { throw new ArgumentNullException(nameof(channelIdsAndDicomFiles), "dicomFiles is null."); } if (channelIdsAndDicomFiles.Any(x => !x.DicomFiles.Any())) { throw new ArgumentException("No dicomFiles in some channelId", nameof(channelIdsAndDicomFiles)); } // Anonymise data var anonymisedDicomData = channelIdsAndDicomFiles.Select(x => new ChannelData(x.ChannelID, AnonymizeDicomFiles(x.DicomFiles, SegmentationAnonymisationProtocolId, SegmentationAnonymisationProtocol))); // Compress anonymised data var dataZipped = DicomCompressionHelpers.CompressDicomFiles( channels: anonymisedDicomData, compressionLevel: DicomCompressionHelpers.DefaultCompressionLevel); using (var content = new ByteArrayContent(dataZipped)) { // POST var response = await _client.PostAsync(new Uri($@"/v1/model/start/{modelId}", UriKind.Relative), content).ConfigureAwait(false); if (response.StatusCode.Equals(HttpStatusCode.BadRequest)) { throw new ArgumentException(response.ReasonPhrase); } if (!response.IsSuccessStatusCode) { throw new SegmentationClientException(response.ReasonPhrase); } var segmentationId = await response.Content.ReadAsStringAsync().ConfigureAwait(false); Trace.TraceInformation($"Segmentation uploaded with id={segmentationId}"); var flatAnonymizedDicomFiles = anonymisedDicomData.SelectMany(x => x.DicomFiles); return(segmentationId, flatAnonymizedDicomFiles); } }