Title: Read DICOM in Memory and Download Zip Member
Date: 10/22/2020
Description:
To illustrate how to get archive Zip File from Flywheel and read DICOM file in memory.

Requirements

Install and Import Dependencies

Flywheel API Key and Client

Get your API_KEY. More on this in the Flywheel SDK doc here.

Instantiate the Flywheel API client

Show Flywheel logging information


Initialize a few values

Define the Group's ID and Project's Label that contain DICOM zip archive files and let's look for it on your Flywheel instance.

Tip: Group ID and Project Label can be found on top of the Project page on the Flywheel Instance as shown in the snippet below.

group-id-proj-label


Requirements

Before starting off, we want to check your permission on the Flywheel Instance in order to proceed in this notebook.

check_user_permission will return True if both the group and project meet the minimum requirement, else a compatible list will be printed.


Helpful Function


Main Script

For the first part of the tutorial, we will go through how to read the DICOM file from the zip archive and read the data with pydicom. To illustrate we will be reading the 2nd DICOM image in the zip file.

View DICOM Image

Here we will be using matplotlib to plot the DICOM Image that we have selected for each Subject and some DICOM information.


Read DICOM images of a specific Subject

Helpful Function

Let's say you are only interested in a specific Subject and you would like to do some analysis with the Subject's DICOM image.

Get the anx_s4 Subject container

Get the DICOM zip archive for the Subject anx_s4.

Helpful function that get the zip file member and store all the zip member dicom file for one subject.

Interactive DICOM viewer

We will be using the ipywidgets.interactive() method to build a DICOM Viewer from the DICOM image that we have pulled from the Subject.


Download a Zip member from a Zip Archive in Analysis container via SDK

Often a gear will store its output into a large zip file in an analysis container and it can be useful to extract specific file from it across different gear runs. In this section, you will learn how to get a zip file member from Flywheel and download the files into your local directory

Get analyses container

First, we get the Subject container first. In this tutorial we will be working with sub-04.

We will be working with the first Session for Subject sub-4 and the first Analyses in the Session container.

Info: Use reload() method here to retrieve a complete view of the data within the Session container .

Now, we will loop through the files within the analyses container in the Session container.

After looking at the available files in the Analysis container, we decided to work with the second file in the Analysis container.

To retrieve the files information within the zip file, you can use get_file_zip_info which will take the zip_file_name as argument.

Let's see what the archive contains and pinpoint the location of the .nifti file.

Download File

To download the file from the zip output file, we will be using download_file_zip_member method. It requires three parameters, which is file_name: The name of the zip file, member_path: the choosen member in the zip file, and dest_file: The name of the file you would like to download as.

In this example, we are going to download the 86th member in the zip file and save as vectorRGB.nii.gz on your local directory.