mspt.dataViewer package

Submodules

mspt.dataViewer.array2DViewer module

mspt.dataViewer.array2DViewer.XServer_is_running()[source]

Code used to test if the X11 server is running. This is used to know how configure the displays in matplotlib

Returns:True or False
mspt.dataViewer.array2DViewer.closePlot()[source]

Closes the matplotlib plotting area.

mspt.dataViewer.array2DViewer.displayImageFromArray(data, name=None)[source]

Displays an image from a numpy array using matplotlib. It uses “jet” colormap and the figure opens and closes automatically.

Parameters:
  • data – 2D numpy array.
  • name – If name is not None, it is used as the title of the figure.
mspt.dataViewer.array2DViewer.displayPlot()[source]

Draws the window with the figure and the subplots. It is displayed during 10 seconds.

mspt.dataViewer.array2DViewer.loadData(filename)[source]

Loads cPickle data to retrieve the python data stored in it.

Parameters:filename – Path to the file to load.
Returns:The data loaded.
mspt.dataViewer.array2DViewer.openPNGImageToArray(filename)[source]

Loads a PNG image and returns it as a numpy array.

Parameters:filename – The image file to open.
Returns:A 2D numpy array corresponding to the input image.
mspt.dataViewer.array2DViewer.saveDataToImagePNG(data, filename, min_max=, []bw=True)[source]

Save a 2D array to an image file: png if the data is in the range 0-255 and tiff otherwise. The array is linearly scaled to be in the range 0-255 (png) or 0-65535 (tiff). This is determined by the maximum value stored in min_max.

Parameters:
  • data – 2D numpy array
  • filename – path and name of the image to be saved. The extension (.png or .tiff) will be added in the function.
  • min_max – By default min_max = []. In this case the maximum image intensity is automatically set to 255 or 65535 depending on what the maximum value is in data. If min_max = [min_Val , max_Val], the linear scaling determines the slope and the intercept to be used based on these min_max values. Max_Val will determine if the image will be a png (0-255) or a tiff (0-65535).
  • bw – True (Default) / False. True to save the image in black and white (grayscale), False to save it in color. Note that images saved in tiff format are automatically saved in black-and white.
mspt.dataViewer.array2DViewer.saveFigure(pathToSave, filename, ext='png', subplotIdx=None)[source]

Saves a matplotlib figure to an image file (png or pdf).

Parameters:
  • pathToSave – Path where to save the image.
  • filename – Image name.
  • ext – File extension (png or pdf). By default: png.
  • subplotIdx – If the figure contains several subplots and the user wants to save only 1 subplot it needs to specify it. For example if the plots are in a grid 2x3 and one wants to plot the 4th subplot, subplotIdx = 234. If there is a single subplot: 111. It is None by default.
mspt.dataViewer.array2DViewer.storeData(filename, data)[source]

Stores some data to a cPickle structure. This allows to open the data, as it would be in the simulator, outside the simulator.

Parameters:
  • filename – Path where to save the cPickle file.
  • data – Any python data to save.
mspt.dataViewer.array2DViewer.storeMatrixAsImage(pathToSave, data, name, alongAxis=None, cpkl=False, bw=True, minmax=None)[source]

Stores a 3D numpy array as a stack of images and stores the numpy array data in a cPickle structure through the function ‘storeData()’. Slices can be stored along a given axis: ‘z’ dicom patient axis, ‘y’ dicom patient axis, ‘x’ dicom patient axis. By default ‘z’ axis is chosen.

Parameters:
  • pathToSave – Path where to save the images.
  • data – 3D numpy array to save.
  • name – Name for the images to save.
  • alongAxis – List of the axis along which the images will be saved. By default the value is None which corresponds to ‘z’. Possible values are ‘x’ (i.e. the images are stored along the column axis of the dicom image), ‘y’ (i.e. the images are stored along the rows axis of the dicom image), ‘z’ (i.e. the images are stored along the frame axis of the dicom image). This can be used when the patient is moving and the user wants to see the motions from the side, the top or the front of the dicom image.
  • cpkl – True to save the 3D numpy array to a cPickle structure. This allows to have have access to the numpy array outside the simulator. False otherwise. Default: False.
  • bw – True to save the images in black and white. False otherwise. Default: True
  • minmax – Min and Max values used to scale the image intensities. Default: None, in this case the min and max used are the min and max of the numpy array.

mspt.dataViewer.dvh module

class mspt.dataViewer.dvh.DVH(configData, dose, listVOIs)[source]

Bases: object

Class DVH is used to build and draw/save a Dose Volume Histogram (DVH)

Parameters:
  • configData

    configuration dictionary (globalVariables) for the simulator loaded in simulator.simulatorMSPT. It should contain the keys:

    • ‘mainPath’ : The path where the simulation directory will be saved.
    • ‘nameSimulation’ : The simulation name.
    • ‘dvhRelativeVolume’ : True or False: True to plot the DVH using a relative volume (0-100%), False to plot with real volumes.
  • dose – 3D dose distribution (in Gy) stored in a numpy array.
  • listVOIs – List of the volumes of interest (VOIs). List of tuples: ( mask 3D Numpy array , ROI name, ROI observation, ROI index, Type). See the function getMasksForAssociatedDicomImage() in the module dicomReader.dicomRSReader. for more details.

Note

Output dose in cGy.

buildDVHForVOIs(doseDistr, listVOIs, spacing=None)[source]

Function that receives a list of VOI and build DVH for each of these VOIs.

Parameters:
  • doseDistr – 3D dose distribution (3D array) in Gy.
  • listVOIs – List of VOIs : each VOI in the list should be stored in the shape provided by dicomRSReader: [ ( mask 3D Numpy array , ROI name, ROI observation, ROI index) , ( ... )... ]
  • spacing – Voxels spacing: used if relativeVolume is False. If spacing is None and relativeVolume is False, then it will be considered that voxel spacing is {‘frames’:10,’rows’:10’cols’:10}. Spacing is in mm.

Note

A Dose Volume Histogram (DVH) is a cumulative histogram for a specified volume of interest (VOI). It represents the cumulative volume irradiated as a function of the dose. See ICRU report: 5 Geometric Terms, and Dose and Dose-Volume Definitions for more details. To compute it:

  1. build the histogram nb voxels vs received dose
  2. build the cumulative histogram from 1): this represents the number of voxels receiving a dose <= tp the given dose
  3. build the DVH from 2): DVH = nb_tot_voxels_in_VOI - cumulative_histogram: this represent the number of voxels receiving at least the given dose
closeDVH()[source]

Closes pyplot figure.

computeDvData(listVolumes, voiName=None)[source]

Computes Dv (Dv indicates that a volume V of VOI receives at least a dose equal to Dv. See ICRU report: 5 Geometric Terms, and Dose and Dose-Volume Definitions for more details.

Parameters:
  • listVolumes – List of volume values to consider for Dv data.
  • voiName – Name of the VOI. If voiName is None, it will be computed for all the VOIs.
computeVdData(listDoses, voiName=None)[source]

Computes Vd (Vd indicates that the volume Vd of VOI that received at least D) values. See ICRU report: 5 Geometric Terms, and Dose and Dose-Volume Definitions for more details.

Parameters:
  • listDoses – List of dose values to consider for Vd data.
  • voiName – Name of the VOI. If voiName is None, it will be computed for all the VOIs.
drawDVHs(voiName=None)[source]

Draws the DVH in a pyplot figure.

Praram voiName:None by default in order to display DVH for all VOIs. If not none it should contain the name of the VOI to display. If the name is unknown it will display the DVH for all the VOIs.
drawDvData(voiName=None)[source]

Displays Dv Data in the pyplot figure.

Praram voiName:None by default in order to display DVH for all VOIs. If not none it should contain the name of the VOI to display. If the name is unknown it will display the DVH for all the VOIs.
drawVdData(voiName=None)[source]

Displays Vd Data in the pyplot figure.

:praram voiName None by default in order to display DVH for all VOIs. If not none it should contain the name of the VOI to display. If the name is unknown it will display the DVH for all the VOIs.

getDVHForVOI(voi)[source]

Get the DVH for a given VOI name.

Parameters:voi – Name of the voi. If voi=’all’ returns a dictonary with all the DVHs.
Returns:The DVH as a dictionary: dictDVH[voi]= [dose bins array , volume data array].

Note

Dose in cGy.

getDvDataForVOI(voi)[source]

Get the Dv (Dose received by a given volume) data for a given VOI name. For example D50, would be the dose received by 50% of the VOI volume.

Parameters:voi – Name of the voi. If voi=’all’ return a dictonary with all the DVHs.
Returns:The Dv as a dictionary: dictDv[voi]= [ [dose1,vol1], [dose2,vol2], ... [dosen,voln] ].

Note

Dose in cGy.

getVdDataForVOI(voi)[source]

Get the Vd (portion of the volume receiving a given dose) Data for a given VOI name. For example V10: volume of VOI receiving 10 dose units.

Parameters:voi – Name of the voi. If voi=’all’ return a dictonary with all the DVHs.
Returns:The Vd as a dictionary: dictVd[voi]= [ [dose1,vol1], [dose2,vol2], ... [dosen,voln] ].

Note

Dose in cGy.

normalizeDVH(listVOIs, spacing)[source]

Normalizes the DVH such that the volumes of each VOI is set in the range 0% - 100%.

Parameters:
  • listVOIs – List of VOIs : each VOI in the list should be stored in the shape provided by DicomRSReader: [ ( mask 3D Numpy array , ROI name, ROI observation, ROI index, Type) , ( ... )... ]
  • spacing – Voxels spacing: used if relativeVolume is False. If spacing is None and relativeVolume is False, then it will be considered that voxel spacing is {‘frames’:10,’rows’:10’cols’:10}. spacing in mm
saveDVHAsCSV(filename)[source]

Saves DVH data to CSV files: 1 per VOI.

Parameters:filename – Basis name of the CSV files to save.
saveDVHAsImage(filename)[source]

Saves the current DVH (in a pyplot figure) as an image.

Parameters:filename – Name of the image file to save.
saveDvAsCSV(filename)[source]

Saves Dv data to CSV files: 1 per VOI

Parameters:filename – Filename basis of the CSV files to save.
saveEntireDVHAsCSV(filename)[source]

Saves an entire DVH data to a CSV file.

Parameters:filename – Filename basis of the CSV file to save.
saveVdAsCSV(filename)[source]

Saves Dv data to CSV files: 1 per VOI.

Parameters:filename – Basis name of the CSV files to save.
mspt.dataViewer.dvh.buildCumulHisto(data3D, indMask=None, maxValue=None)[source]

Builds a cumulative histogram for data3D.

Parameters:
  • data3D – 3D array for which we want to build a histogram.
  • indMask – Indices where the VOI is located. If indMask is not None, the histogram is built only for the voxels specified by indMask, otherwise it builds an histogram for the entire array.
  • maxValue – If maxValue is None a maxValue is defined as the maximum value of the 3D volume used ( limited to indMask if indMask is not None), otherwise the value provided is used.

The histogram is built with bin ranging from 0 to maxValue with a step of 0.5. To build the cumulative histogram the function buildHistogram() is called.

Returns:A tuple with:
  1. y values of the histogram (array)
  2. x values (bins) of the histogram (array)
mspt.dataViewer.dvh.buildHistogram(data3D, indMask=None, maxValue=None)[source]

Builds an histogram for data3D.

Parameters:
  • data3D – 3D array for which we want to build a histogram.
  • indMask – Indices where the VOI is located. If indMask is not None, the histogram is built only for the voxels specified by indMask, otherwise it builds an histogram for the entire array.
  • maxValue – If maxValue is None a maxValue is defined as the maximum value of the 3D volume used ( limited to indMask if indMask is not None), otherwise the value provided is used.

The histogram is built with bins ranging from 0 to maxValue with a step of 0.5

Returns:A tuple with:
  1. y values of the histogram (array)
  2. x values (bins) of the histogram (array)
mspt.dataViewer.dvh.exportAllDVHToCSV(dictDVH, name)[source]

Saves a DVH dictionary to a csv files. One file for all the VOIs.

Parameters:
  • dictDVH – DVH dictionary to save.
  • name – Name of the DVH. Used as the filename.
mspt.dataViewer.dvh.exportDVHToCSVFile(dictDVH, name)[source]

Saves a DVH dictionary to csv files. One file per VOI.

Parameters:
  • dictDVH – DVH dictionary to save.
  • name – Name of the DVH. Used as the filename basis.
mspt.dataViewer.dvh.exportDvDataToCSVFile(dictDv, volumeType, name)[source]

Saves Dv data to csv files. One file per VOI.

Parameters:
  • dictDV – Dictionary containing the (Dv, Volume) tuples for each VOI.
  • volumeType – ‘Relative_%’ or ‘Absolute_cm^3’
  • name – Filename basis.
mspt.dataViewer.dvh.exportVdDataToCSVFile(dictVd, volumeType, name)[source]

Saves Vd data to csv files. One file per VOI.

Parameters:
  • dictDV – Dictionary containing the (Dv, Volume) tuples for each VOI.
  • volumeType – ‘Relative_%’ or ‘Absolute_cm^3’
  • name – Filename basis.
mspt.dataViewer.dvh.getDoseForVolume(doseData, volData, vol)[source]

Calculate the Dv (Dose received by a given volume) Data for a given VOI name. For example D50, would be the dose received by 50% of the VOI volume.

See ICRU report: 5 Geometric Terms, and Dose and Dose-Volume Definitions for more details.

This function is called by computeDvData().

Parameters:
  • doseData – 1D array with dose values of the DVH. It should be in inscreasing order.
  • volData – 1D array with volume values of the DVH. It should be in decreasing order.
  • vol – Value of the volume considered.
Returns:

Dv value calculated.

mspt.dataViewer.dvh.getSubDoseDataForMask(doseData, mask)[source]

Get the dose distribution in the volume defined by the mask.

Parameters:
  • doseData – 3D dose distribution (3D numpy array).
  • mask – 3D numpy binary array (1 in the VOI, 0 itherwise).
Returns:

A tuple with:

  1. A 3D volume where doseData is preserved in the VOI (using mask) and is set to 0 outside.
  2. Indices where the mask is 1 (VOI).

mspt.dataViewer.dvh.getVolumeForDose(doseData, volData, dose)[source]

Computes Vd (Vd indicates that the volume Vd of VOI that received at least D) values.

See ICRU report: 5 Geometric Terms, and Dose and Dose-Volume Definitions for more details.

This function is called by computeVdData().

Parameters:
  • doseData – 1D array with dose values of the DVH. It should be in inscreasing order.
  • volData – 1D array with volume values of the DVH. It should be in decreasing order.
  • dose – Value of the dose considered.
Returns:

Vd value calculated.

mspt.dataViewer.dvh.processDVHForDoseDistribution(dose, listVOIs, listDv, listVd, nameDVH, dataConfig)[source]

Builds the DVH for a dose distribution and save the data computed: DVH, Dv, Vd in an image and in CSV files for each VOIs.

Parameters:
  • listVOIs – List of the volumes of interest (VOIs). List of tuples: ( mask 3D Numpy array , ROI name, ROI observation, ROI index, type). See dicomReader.dicomRSReader.getMasksForAssociatedDicomImage() for more details.
  • dose – 3D dose distribution (3D numpy array).
  • listDv – List of volume values for which to compute the Dv value.
  • listVd – List of dose values for which to compute the Vd value.
  • nameDVH – Name for the files to save.
  • dataConfig – configuration dictionary (globalVariables) for the simulator loaded in simulator.simulatorMSPT. It should contain the keys:
  • ‘mainPath’ : The path where the simulation directory will be saved.
  • ‘nameSimulation’ : The simulation name.
  • ‘dvhRelativeVolume’ : True or False: True to plot the DVH using a relative volume (0-100%), False to plot with real volumes.
Returns:(Dv data, Vd data)
mspt.dataViewer.dvh.save2DListToCSV(data, filename)[source]

Saves a 2D list to a CSV file.

Parameters:
  • data – 2D list of values.
  • filename – CSV filename.

mspt.dataViewer.plotDataGraphs module

mspt.dataViewer.plotDataGraphs.clearSubplot(subplot)[source]

Clears the subplot.

Parameters:subplot – Reference to the subplot.
mspt.dataViewer.plotDataGraphs.closePlot()[source]

Closes a plotting area when done.

mspt.dataViewer.plotDataGraphs.displayPlot()[source]

Draws the window with the figure and the subplots. It is displayed during 2 seconds.

mspt.dataViewer.plotDataGraphs.drawData(listXData, listYData, listNames=None, display=True, title=None, xLabel=None, yLabel=None, posLegend=None)[source]

Draws the several set of (X,Y) data in a pyplot figure. The first xValues array and the first yValues array is plot. Then the second and so on.

If display is True, the figure will be displayed.

Parameters:
  • listXdata – List of 1D arrays of x Values
  • listYData – List of 1D arrays of y Values
  • listNames – List of names to be used with each set of (X,Y) data. If listNames is None (default) the names are defined automatically as “Data_IDX” where IDX is their index in the lists.
  • display – True (default) to display the window, False otherwise.
  • title – String for the figure title. None by default.
  • xLabel – title for x axis. None by default.
  • yLabel – title for y axis. None by default.
  • posLegend – Legend position: [‘right’, ‘bottom’]. By default it is set to None which is considered as ‘right’.
mspt.dataViewer.plotDataGraphs.drawPlot(subplot, xData, yData, xmin=None, xmax=None, ymin=None, ymax=None, xlabel=None, ylabel=None, titlePlot=None, singlePlot=False, nameSinglePlot=None, grid=True)[source]

Draws the data (xData,yData) in subplot.

Parameters:
  • xmin – minimum value of the x axis. None by default.
  • xmax – maximum value of the x axis. None by default.
  • ymin – minimum value of the y axis. None by default.
  • ymax – maximum value of the y axis. None by default.
  • xlabel – title for the x axis. None by default.
  • ylabel – title for the y axis. None by default.
  • titlePlot – title for the subplot. None by default.
  • singlePlot – True if a single plot is in the figure. False by default.
  • nameSinglePlot – title for the figure if there is only one plot (singlePlot is True). None by default.
  • grid – True (default) or False if we want to display or not a grid in the subplot
mspt.dataViewer.plotDataGraphs.findminMax(listData)[source]

Function that finds the overall minimum and maximum values in a list of data arrays.

Parameters:listData – List of 1D arrays.
Returns:List : [min, max]
mspt.dataViewer.plotDataGraphs.getCurrentFigure()[source]

Get the reference of the current figure. Equivalent to pyplot.gcf().

Returns:The reference of the current figure.
mspt.dataViewer.plotDataGraphs.makePlotLegend(fig, subplot, pos=None)[source]

Builds a legend for the subplot in a figure.

Parameters:
  • fig – reference to the figure
  • subplot – reference to the subplot.
  • pos – Legend position: [‘right’, ‘bottom’]. By default None, which corresponds to ‘right’
mspt.dataViewer.plotDataGraphs.newSubplot(fig, multiple=False, num=1)[source]

Creates a new subplot in a figure.

Parameters:
  • fig – Reference of the matplotlib figure.
  • multiple – True to draw multiple subplots in the figure. False (default) otherwise.
  • num – Number of desired subplots (used only if multiple is True). If there are multiple subplots in the figure it is set such that there are 2 rows of subplots and n columns.
Returns:

A reference to the subplot created and added to the figure.

mspt.dataViewer.plotDataGraphs.plotHorizontalLine(subplot, yVal, xEnd=None, color=None, label=None, font=None)[source]

Plots a horizontal line in the subplot at yVal. By default it draws the line from y axis to the right of the subplot, but a x value (corresponding to the real xValue) can be specified if the user wants to stop it before.

Parameters:
  • yVal – Y value where to draw the line.
  • xEnd – X value where to end the line. By default it is None.
  • color – Matplotlib color used to draw the line and the label if a label is provided. By default black.
  • label – Text being displayed at the left the y axis.
  • font – Font properties if a label is defined. By default it is None and uses matplotlib default font
mspt.dataViewer.plotDataGraphs.plotVerticalLine(subplot, xVal, yEnd=None, color=None, label=None, font=None)[source]

Plots a vertical line in the subplot at xVal. By default it draws the line from x axis to the top of the subplot, but a y value (corresponding to the real yValue) can be specified if the user wants to stop it before.

Parameters:
  • xVal – X value where to draw the line.
  • yEnd – Y value where to end the line. By default it is None.
  • color – Matplotlib color used to draw the line and the label if a label is provided. By default black.
  • label – Text being displayed at the left the y axis.
  • font – Font properties if a label is defined. By default it is None and uses matplotlib default font
mspt.dataViewer.plotDataGraphs.savePlotImage(fig, filename, dirPath=None, ext='png', subplot=None)[source]

Saves a figure to image file (png or pdf).

Parameters:
  • fig – reference to the figure to save
  • filename – image name
  • dirPath – directory where to save the image. By default it is None: dirPath is set to os.getcwd() + “/PlotImages/”.
  • ext – file extension (png or pdf). By default: png
  • subplot – Reference to the subplot. If the figure contains several subplots and the user wants to save only 1 subplot it needs to specify it.

Module contents