Giter Club home page Giter Club logo

dicom-streams-js's Introduction

dicom-streams-js

Build Status Coverage Status

The purpose of this project is to create a streaming API for reading and processing DICOM data using node-streams. It can be used both on the backend using Node, as well as on the frontend. Advantages of streaming DICOM data include better control over resource allocation such as memory via strict bounds on DICOM data chunk size and network utilization using back-pressure.

This project is a port of the dicom-streams project which is written in Scala using Akka-streams.

Setup

The dicom-streams-js library is deployed to NPM. Install it using npm install -s @exini/dicom-streams-js. Time and date handling using js-joda is marked as an external dependency in dicom-streams-js. If you want to read and modify times and dates, install this using npm install -s js-joda.

Data Model

Streaming binary DICOM data may originate from many different sources such as files, a HTTP POST request (on the server side), or a read from a database. Streaming data arrives in chunks (Buffers). In the Node Streams nomenclature, chunks originate from readables, they are processed in transforms and and folded into a non-streaming plain objects using writables. Synonyms for these terms are sources, flows and sinks. These latter terms are used in the docs and throughout the code.

This library provides flows for parsing binary DICOM data into DICOM parts (represented by the DicomPart abstraction) - small objects representing a part of a data element. These DICOM parts are bounded in size by a user specified chunk size parameter. Flows of DICOM parts can be processed using a series of flows in this library. There are flows for filtering based on tag path conditions, flows for converting between transfer syntaxes, flows for re-encoding sequences and items, etc.

The Element interface provides a set of higher level data classes, each roughly corresponding to one row in a textual dump of a DICOM files. Here, chunks are aggregated into complete data elements. There are representations for standard tag-value elements, sequence and item start elements, sequence and item delimitation elements, fragments start elements, etc. A DicomPart stream is transformed into an Element stream via the elementFlow flow.

A flow of Elements can be materialized into a representation of a dataset called an Elements using the elementSink sink. For processing of large sets of data, one should strive for a fully streaming DICOM pipeline, however, in some cases it can be convenient to work with a plain dataset; Elements serves this purpose. Internally, the sink aggregates Elements into ElementSets, each with an asssociated tag number (value elements, sequences and fragments). Elements implements a straight-forward data hierarchy:

  • An Elements holds a list of ElementSets (ValueElement, Sequence and Fragments)
  • A ValueElement is a standard attribute with tag number and binary value
  • A Sequence holds a list of Items
    • An Item contains zero or one Elements (note the recursion)
  • A Fragments holds a list of Fragments
    • A Fragment holds a binary value.

The following diagram shows an overview of the data model at the DicomPart, Element and ElementSet levels.

Data model

As seen, a standard attribute, represented by the ValueElement class is composed by one HeaderPart followed by zero, one or more ValueChunks of data. Likewise, ecapsulated data such as a jpeg image is composed by one FragmentsPart followed by, for each fragment, one ItemPart followed by ValueChunks of data, and ends with a SequenceDelimitationPart.

Examples

The following example reads the DICOM file provided as input argument, folds its contents first into a stream of Elements, and then into a concrete Elements object and logs the result to the console

const fs = require('fs');
const { parseFlow, elementFlow, elementSink, pipe, VR } = require('@exini/dicom-streams-js');

const src = fs.createReadStream(process.argv[2]);

pipe(
    src,
    parseFlow(),
    elementFlow(),
    elementSink((elements) => {
        console.log(elements.toString());
    }),
);

The next, longer, example reads the file specified by the first input argument then passes the data through the following flows:

  1. Parsing the binary data into DicomParts for further processing
  2. Re-encoding the data to always use indeterminate length sequences and items with explicit sequence and item delimitations
  3. Re-encoding the data to use the UTF-8 character set
  4. Filtering of the elements to preserve only those on a allow list specified as an array of TagTrees (trees of pointers into a dataset)
  5. Filtering of the remaining elements according to a deny list of tag trees
  6. Modification of the remaining elements to set Patient Name to Anon 001, add or modifiy the attribute Patient Identity Removed to YES, and leave other elements unmodified
  7. Map the resulting elements to their corresponding byte representations
  8. Write the results to disk using the file name specified by the second input argument.
const fs = require('fs');
const {
    TagPath,
    TagTree,
    parseFlow,
    toBytesFlow,
    allowFilter,
    denyFilter,
    toUtf8Flow,
    toIndeterminateLengthSequences,
    modifyFlow,
    TagModification,
    TagInsertion,
    pipe,
} = require('@exini/dicom-streams-js');

const src = fs.createReadStream(process.argv[2]);
const dest = fs.createWriteStream(process.argv[3]);

pipe(
    src,
    parseFlow(),
    toIndeterminateLengthSequences(),
    toUtf8Flow(),
    allowFilter([
        TagTree.fromTag(Tag.SpecificCharacterSet),
        TagTree.fromTag(Tag.PatientName),
        TagTree.fromTag(Tag.PatientName),
        TagTree.fromTag(Tag.StudyDescription),
        TagTree.fromTag(Tag.SeriesDate),
        TagTree.fromAnyItem(Tag.MACParametersSequence),
    ]),
    denyFilter([TagTree.fromAnyItem(Tag.MACParametersSequence).thenTag(Tag.DataElementsSigned)]),
    modifyFlow(
        [TagModification.equals(TagPath.fromTag(Tag.PatientName), () => Buffer.from('Anon 001'))],
        [new TagInsertion(TagPath.fromTag(Tag.PatientIdentityRemoved), () => Buffer.from('YES'))],
    ),
    toBytesFlow(),
    dest,
);

Custom Processing

New non-trivial DICOM flows can be built using a modular system of capabilities that are mixed in as appropriate with a core class implementing a common base interface. The base abstraction for DICOM flows is DicomFlow and new flows are created using the DicomFlow.createFlow method. The DicomFlow interface defines a series of events, one for each type of DicomPart that is produced when parsing DICOM data with DicomParseFlow. The core events are:

  public onPreamble(part: PreamblePart): DicomPart[]
  public onHeader(part: HeaderPart): DicomPart[]
  public onValueChunk(part: ValueChunk): DicomPart[]
  public onSequence(part: SequencePart): DicomPart[]
  public onSequenceDelimitation(part: SequenceDelimitationPart): DicomPart[]
  public onFragments(part: FragmentsPart): DicomPart[]
  public onItem(part: ItemPart): DicomPart[]
  public onItemDelimitation(part: ItemDelimitationPart): DicomPart[]
  public onDeflatedChunk(part: DeflatedChunk): DicomPart[]
  public onUnknown(part: UnknownPart): DicomPart[]
  public onPart(part: DicomPart): DicomPart[]

Default behavior to these events are implemented in core classes. The most natural behavior is to simply pass parts on down the stream, e.g.

  public onPreamble(part: PreamblePart): DicomPart[] { return [part]; }
  public onHeader(part: HeaderPart): DicomPart[] { return [part]; }
  ...

This behavior is implemented in the IdentityFlow core class. Another option is to defer handling to the onPart method which is implemented in the DeferToPartFlow core class. This is appropriate for flows which define a common behavior for all part types.

To give an example of a custom flow, here is the implementation of a filter that removes nested sequences from a dataset. We define a nested dataset as a sequence with depth > 1 given that the root dataset has depth = 0.

  public nestedSequencesFilter() = createFlow(new class extends TagPathTracking(GuaranteedValueEvent(GuaranteedDelimitationEvents(InFragments(DeferToPartFlow)))) {
    public onPart(part: DicomPart): DicomPart[] { return this.tagPath.depth() > 1 ? [] : [part];
  }());

In this example, we chose to use DeferToPartFlow as the core class and mixed in the TagPathTracking capability (along with its dependencies GuaranteedValueEvent, GuaranteedDelimitationEvents and InFragments) which gives access to a tagPath: TagPath variable at all times which is automatically updated as the flow progresses.

License

This project is released under the Apache License, version 2.0.

dicom-streams-js's People

Contributors

dependabot[bot] avatar jonatan-exini avatar karl-exini avatar karlsjostrand avatar pewniak747 avatar richterjens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

dicom-streams-js's Issues

early stopping in non-streaming parser does not work as intended

The early stopping function is applied to the last parsed (and materialized) Element. The stopping function must be exclusive to work as intended, meaning the stopping function must be evaluated early when parsing a new Element, and parsing should be interrupted is the stop critierion is met. This is also what the docs say, and how the stop flow works when doing streaming parsing.

Would be nice to have original file offset in OB/OW value and fragments

First off, thanks for writing such a nice library. I have been playing around with it for a few weeks and find it really easy to work with.

I realize this is not really the goad of this library, but it would be nice to be able to access the original file byte offsets on OB and OW vr types such as pixel data and LUTs. My use case is around indexing files, I don't need the pixel data as it streams, but would like to access only the appropriate byte range later. In fact, it would be even better if we could optionally disable buffering of the whole buffer to save memory while streaming.

I tried to figure out how to hook it up, but the flow is somewhat complex.. just something to think of..

Feature request: direct support for Web Streams API

Web Streams API have experimental support in Node, more info here: https://nodejs.org/api/webstreams.html

If supported, same code would be able to run in server (node) and browser environments without any polyfills necessary (let's hope). Then maybe we could also get rid of the multipipe dependency? (has a few transitives as well)

I could spend some time looking into this if you think it is an interesting direction. It would require a major version bump for sure.

Edit: fact checked availability of the API. They were available since v16.5, but no warning is emitted since v18.

Example needed: browser usage

This library looks really cool, and is solving a super important problem in medical imaging software!
I am however having trouble using it in the browser. I would like to create a custom anonymisation stream and pipe it to the server using the new fetch streaming upload feature in Chrome v105 (see https://developer.chrome.com/articles/fetch-streaming-requests).

I'm using vite as the bundler. Due to multipipe package, I get Buffer is not defined when I try to import {} from "@exini/dicom-streams-js". There are mentions of __webpack_require__ in the stack, which could mean that the library is assuming usage of webpack.

Is there a way to not depend on multipipe in the browser environment?

EDIT: could be that I misunderstood this line. Could it be that you meant using electron for the frontend?

It can be used both on the backend using Node, as well as on the frontend.

Example parse-modify-write.js not working

I'm trying to get the example working, however it throws the error TypeError: dicom_streams_js_1.parseFlow is not a function.
Please see the sandbox link to reproduce. How can I get the example working?

const got = require("got");
const dicomUrl =
  "https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm";

const src = got.stream(dicomUrl);

pipe(
  src,
  parseFlow(),
  toIndeterminateLengthSequences(),
  toUtf8Flow(),
  allowFilter([
    TagTree.fromTag(Tag.SpecificCharacterSet),
    TagTree.fromTag(Tag.PatientName),
    TagTree.fromTag(Tag.PatientName),
    TagTree.fromTag(Tag.StudyDescription),
    TagTree.fromTag(Tag.SeriesDate),
    TagTree.fromAnyItem(Tag.MACParametersSequence)
  ]),
  denyFilter([
    TagTree.fromAnyItem(Tag.MACParametersSequence).thenTag(
      Tag.DataElementsSigned
    )
  ]),
  modifyFlow(
    [
      TagModification.equals(TagPath.fromTag(Tag.PatientName), () =>
        Buffer.from("Anon 001")
      )
    ],
    [
      new TagInsertion(TagPath.fromTag(Tag.PatientIdentityRemoved), () =>
        Buffer.from("YES")
      )
    ]
  ),
  toBytesFlow()
);

Thanks a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.