# Importing NRRD Annotations

You may import existing segmentations in the NRRD format, created, for example, using 3D Slicer, to use in the [3D medical labeling editor](https://docs.imerit.net/labeling/labeling-editor-interface/medical-labeling-editor).

## How to Import NRRD Segmentations

### Prepare the project ontology

To match the segmentation classes in your NRRD with the segmentation classes on Hub, there are two ways. Either:&#x20;

* you'll make it so that the title of the class on Ango Hub matches the `SegmentX_Name` value in the header of the NRRD file, or
* you'll make it so that that the `schemaId` of the class on Ango Hub matches the `SegmentX_ID` value in the header of the NRRD file.

Ango Hub will first try to match the segment name with the class title, then as fallback it will attempt to match the class schemaId with the segment's ID. If a match for your segment is then still not found, that segment is not imported.

For example, imagine the NRRD segmentation you wish to import has one segmentation class, and you wish for this class to be paired with a class on Ango Hub. This is how you would go about it:

1. Open the Segmentation NRRD you wish to import into Ango Hub with a text editor. The first few lines (e.g. the file header) will likely look something like this:

```
[more ...]
space origin: (100.331,-86.50509999999997,88.181799999999967)
Segment0_Color:=0.501961 0.682353 0.501961
Segment0_ColorAutoGenerated:=1
Segment0_Extent:=40 207 13 235 0 90
Segment0_ID:=Segment_1
Segment0_LabelValue:=1
Segment0_Layer:=0
Segment0_Name:=Segment_1
Segment0_NameAutoGenerated:=1
Segment0_Tags:=Segmentation.Status:inprogress|TerminologyEntry:Segmentation category and type - 3D Slicer General Anatomy list~SCT^85756007^Tissue~SCT^85756007^Tissue~^^~Anatomic codes - DICOM master list~^^~^^|
[... more]
```

2. Each segmentation class in the NRRD is represented by metadata starting with the prefix `SegmentX`, where X is an index starting from 0. All metadata pertaining to a class will have the same index. What we care about is the `SegmentX_LabelValue` value. In this case, since there is only one segmentation, its index will be 0, so we will look at `Segment0_LabelValue` only. In our case, this value is 1. We'll take note of that.
3. In the project where you'd like to import the NRRD segmentation, go to the *Settings* tab, then to the *Category Schema* section. Create a new *Medical Brush* tool. By default, schema IDs for Medical Brush tools start at 1, so in this case, since the schema ID matches the number we took note of earlier, there would be no need to change the schema ID.
4. If we did need to change the schema ID, then we would click on the *Show JSON* button (1) and change the ID from the JSON that appears to match `SegmentX_LabelValue`'s value.

<figure><img src="https://3895963154-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTcOUG6rfWxqGM0N4db2P%2Fuploads%2FMRhHH0fdq0VMiZHH7FRE%2Fimage.png?alt=media&#x26;token=ec6f305e-e4d8-4f25-897c-207c3905b177" alt=""><figcaption></figcaption></figure>

5. Save your category schema by clicking on *Save* at the bottom of the page.

### Prepare the JSON

6. Create a JSON with this format:

#### For Segmentations with Publicly Accessible URLs

```json
[
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd"
  },
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd"
  }
]
```

Where `externalId` is the external ID of the asset to pre-annotate, and `medicalBrushDataUrl` is a link to your publicly-accessible NRRD segmentation mask.

{% hint style="warning" %}
If your segmentations are stored in a cloud storage service, ensure the bucket's CORS settings are set properly, or they will not be visible on Hub.

[Here is a guide](https://docs.imerit.net/data/storages/set-up-cors) on how to set up CORS in such a way that your masks will be visible.
{% endhint %}

#### For Segmentations in Private Buckets

* Integrate your private bucket with Ango Hub by following the instructions in the [Storages](https://docs.imerit.net/data/storages) docs page.
* Navigate to your organization's *Organization* page, then navigate to the *Storages* tab.
* Copy the ID of the storage where your brush traces are stored.
* Prepare a JSON with the following format:

```json
[
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd?storageId=YOUR_STORAGE_ID"
  },
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd?storageId=YOUR_STORAGE_ID"
  }
]
```

The JSON will be a list of objects, where each object represents an asset-segmentation pair. The `data` and `externalId` fields are used by Hub to understand to which asset the prelabels will be imported. You do not need to provide both, either of them is enough.

### Upload the JSON

7. From your project's dashboard, enter the *Assets* tab, then click on *Import.* A dialog will pop up.

<figure><img src="https://3895963154-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTcOUG6rfWxqGM0N4db2P%2Fuploads%2FmDtkAu4WfpQ8339a0AVM%2Fimage.png?alt=media&#x26;token=7fbff9fa-f68b-4ff0-a61d-a87f58eb4d15" alt=""><figcaption></figcaption></figure>

8. Drag and drop the JSON you created earlier on the box. Your assets will be prelabeled.
