Esempio n. 1
0
    def setproperties(imp,
                      scaleX=1.0,
                      scaleY=1.0,
                      scaleZ=1.0,
                      unit="micron",
                      sizeC=1,
                      sizeZ=1,
                      sizeT=1):
        """Set properties of image in Fiji

        :param imp: Image
        :type imp: ImgPlus
        :param scaleX: scaleX, defaults to 1.0
        :type scaleX: float, optional
        :param scaleY: scaleY, defaults to 1.0
        :type scaleY: float, optional
        :param scaleZ: scaleZ, defaults to 1.0
        :type scaleZ: float, optional
        :param unit: scale unit, defaults to "micron"
        :type unit: str, optional
        :param sizeC: sizeC, defaults to 1
        :type sizeC: int, optional
        :param sizeZ: sizeZ, defaults to 1
        :type sizeZ: int, optional
        :param sizeT: sizeT, defaults to 1
        :type sizeT: int, optional
        :return: Image
        :rtype: ImgPlus
        """        

        # check if scaleZ has a valid value to call modify the properties
        if scaleZ is None:
            scaleZ = 1

        # run the image properties tool
        IJ.run(imp, "Properties...", "channels=" + str(sizeC) +
               " slices=" + str(sizeZ)
               + " frames=" + str(sizeT)
               + " unit=" + unit
               + " pixel_width=" + str(scaleX)
               + " pixel_height=" + str(scaleY)
               + " voxel_depth=" + str(scaleZ))

        # create new Calibration object
        newCal = Calibration()

        # set the new paramters
        newCal.pixelWidth = scaleX
        newCal.pixelHeight = scaleY
        newCal.pixelDepth = scaleZ

        # set the correct unit fro the scaling
        newCal.setXUnit(unit)
        newCal.setYUnit(unit)
        newCal.setZUnit(unit)

        # apply the new calibration
        imp.setCalibration(newCal)

        return imp
Esempio n. 2
0
    def setscale(imp,
                 scaleX=1.0,
                 scaleY=1.0,
                 scaleZ=1.0,
                 unit="micron"):

        # check if scaleZ has a valid value to call modify the scaling
        if scaleZ is None:
            scaleZ = 1.0

        # create new Calibration object
        newCal = Calibration()

        # set the new paramters
        newCal.pixelWidth = scaleX
        newCal.pixelHeight = scaleY
        newCal.pixelDepth = scaleZ

        # set the correct unit fro the scaling
        newCal.setXUnit(unit)
        newCal.setYUnit(unit)
        newCal.setZUnit(unit)

        # apply the new calibration
        imp.setCalibration(newCal)

        return imp
Esempio n. 3
0
    def setscale(imp,
                 scaleX=1.0,
                 scaleY=1.0,
                 scaleZ=1.0,
                 unit="micron"):
        """Set new scaling for image.

        :param imp: image
        :type imp: ImgPlus
        :param scaleX: scaleX, defaults to 1.0
        :type scaleX: float, optional
        :param scaleY: scaleY, defaults to 1.0
        :type scaleY: float, optional
        :param scaleZ: scaleZ, defaults to 1.0
        :type scaleZ: float, optional
        :param unit: scaling unit, defaults to "micron"
        :type unit: str, optional
        :return: image
        :rtype: ImgPlus
        """  


        # check if scaleZ has a valid value to call modify the scaling
        if scaleZ is None:
            scaleZ = 1.0

        # create new Calibration object
        newCal = Calibration()

        # set the new paramters
        newCal.pixelWidth = scaleX
        newCal.pixelHeight = scaleY
        newCal.pixelDepth = scaleZ

        # set the correct unit fro the scaling
        newCal.setXUnit(unit)
        newCal.setYUnit(unit)
        newCal.setZUnit(unit)

        # apply the new calibratiion
        imp.setCalibration(newCal)

        return imp
Esempio n. 4
0
    def setproperties(imp,
                      scaleX=1.0,
                      scaleY=1.0,
                      scaleZ=1.0,
                      unit="micron",
                      sizeC=1,
                      sizeZ=1,
                      sizeT=1):

        # check if scaleZ has a valid value to call modify the properties
        if scaleZ is None:
            scaleZ = 1

        # run the image properties tool
        IJ.run(imp, "Properties...", "channels=" + str(sizeC)
               + " slices=" + str(sizeZ)
               + " frames=" + str(sizeT)
               + " unit=" + unit
               + " pixel_width=" + str(scaleX)
               + " pixel_height=" + str(scaleY)
               + " voxel_depth=" + str(scaleZ))

        # create new Calibration object
        newCal = Calibration()

        # set the new paramters
        newCal.pixelWidth = scaleX
        newCal.pixelHeight = scaleY
        newCal.pixelDepth = scaleZ

        # set the correct unit fro the scaling
        newCal.setXUnit(unit)
        newCal.setYUnit(unit)
        newCal.setZUnit(unit)

        # apply the new calibration
        imp.setCalibration(newCal)

        return imp
Esempio n. 5
0
def process(srcDir, dstDir, currentDir, fileName, keepDirectories):
    print "Processing:"

    # Opening the image
    print "Open image file", fileName
    imp = IJ.openImage(os.path.join(currentDir, fileName))

    #Here we make sure the calibration are correct
    units = "pixel"
    TimeUnit = "unit"

    newCal = Calibration()
    newCal.pixelWidth = 1
    newCal.pixelHeight = 1
    newCal.frameInterval = 1

    newCal.setXUnit(units)
    newCal.setYUnit(units)
    newCal.setTimeUnit(TimeUnit)
    imp.setCalibration(newCal)
    cal = imp.getCalibration()

    dims = imp.getDimensions()  # default order: XYCZT

    if (dims[4] == 1):
        imp.setDimensions(1, 1, dims[3])

# Start the tracking

    model = Model()

    #Read the image calibration
    model.setPhysicalUnits(cal.getUnit(), cal.getTimeUnit())

    # Send all messages to ImageJ log window.
    model.setLogger(Logger.IJ_LOGGER)

    settings = Settings()
    settings.setFrom(imp)

    # Configure detector - We use the Strings for the keys
    # Configure detector - We use the Strings for the keys
    settings.detectorFactory = DownsampleLogDetectorFactory()
    settings.detectorSettings = {
        DetectorKeys.KEY_RADIUS: 2.,
        DetectorKeys.KEY_DOWNSAMPLE_FACTOR: 2,
        DetectorKeys.KEY_THRESHOLD: 1.,
    }

    print(settings.detectorSettings)

    # Configure spot filters - Classical filter on quality
    filter1 = FeatureFilter('QUALITY', 0, True)
    settings.addSpotFilter(filter1)

    # Configure tracker - We want to allow merges and fusions
    settings.trackerFactory = SparseLAPTrackerFactory()
    settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap(
    )  # almost good enough
    settings.trackerSettings['LINKING_MAX_DISTANCE'] = LINKING_MAX_DISTANCE
    settings.trackerSettings['ALLOW_TRACK_SPLITTING'] = ALLOW_TRACK_SPLITTING
    settings.trackerSettings['SPLITTING_MAX_DISTANCE'] = SPLITTING_MAX_DISTANCE
    settings.trackerSettings['ALLOW_TRACK_MERGING'] = ALLOW_TRACK_MERGING
    settings.trackerSettings['MERGING_MAX_DISTANCE'] = MERGING_MAX_DISTANCE
    settings.trackerSettings[
        'GAP_CLOSING_MAX_DISTANCE'] = GAP_CLOSING_MAX_DISTANCE
    settings.trackerSettings['MAX_FRAME_GAP'] = MAX_FRAME_GAP

    # Configure track analyzers - Later on we want to filter out tracks
    # based on their displacement, so we need to state that we want
    # track displacement to be calculated. By default, out of the GUI,
    # not features are calculated.

    # The displacement feature is provided by the TrackDurationAnalyzer.

    settings.addTrackAnalyzer(TrackDurationAnalyzer())
    settings.addTrackAnalyzer(TrackSpeedStatisticsAnalyzer())

    filter2 = FeatureFilter('TRACK_DISPLACEMENT', 10, True)
    settings.addTrackFilter(filter2)

    #-------------------
    # Instantiate plugin
    #-------------------
    trackmate = TrackMate(model, settings)

    #--------
    # Process
    #--------

    ok = trackmate.checkInput()
    if not ok:
        sys.exit(str(trackmate.getErrorMessage()))

    ok = trackmate.process()
    if not ok:
        sys.exit(str(trackmate.getErrorMessage()))

#----------------
# Display results
#----------------
    if showtracks:
        model.getLogger().log('Found ' +
                              str(model.getTrackModel().nTracks(True)) +
                              ' tracks.')
        selectionModel = SelectionModel(model)
        displayer = HyperStackDisplayer(model, selectionModel, imp)
        displayer.render()
        displayer.refresh()


# The feature model, that stores edge and track features.
    fm = model.getFeatureModel()

    with open(dstDir + fileName + 'tracks_properties.csv', "w") as file:
        writer1 = csv.writer(file)
        writer1.writerow([
            "track #", "TRACK_MEAN_SPEED (micrometer.secs)",
            "TRACK_MAX_SPEED (micrometer.secs)", "NUMBER_SPLITS",
            "TRACK_DURATION (secs)", "TRACK_DISPLACEMENT (micrometer)"
        ])

        with open(dstDir + fileName + 'spots_properties.csv',
                  "w") as trackfile:
            writer2 = csv.writer(trackfile)
            #writer2.writerow(["spot ID","POSITION_X","POSITION_Y","Track ID", "FRAME"])
            writer2.writerow(
                ["Tracking ID", "Timepoint", "Time (secs)", "X pos", "Y pos"])

            for id in model.getTrackModel().trackIDs(True):

                # Fetch the track feature from the feature model.
                v = (fm.getTrackFeature(id, 'TRACK_MEAN_SPEED') *
                     Pixel_calibration) / Time_interval
                ms = (fm.getTrackFeature(id, 'TRACK_MAX_SPEED') *
                      Pixel_calibration) / Time_interval
                s = fm.getTrackFeature(id, 'NUMBER_SPLITS')
                d = fm.getTrackFeature(id, 'TRACK_DURATION') * Time_interval
                e = fm.getTrackFeature(
                    id, 'TRACK_DISPLACEMENT') * Pixel_calibration
                model.getLogger().log('')
                model.getLogger().log('Track ' + str(id) +
                                      ': mean velocity = ' + str(v) + ' ' +
                                      model.getSpaceUnits() + '/' +
                                      model.getTimeUnits())

                track = model.getTrackModel().trackSpots(id)
                writer1.writerow(
                    [str(id), str(v),
                     str(ms), str(s),
                     str(d), str(e)])

                for spot in track:
                    sid = spot.ID()
                    x = spot.getFeature('POSITION_X')
                    y = spot.getFeature('POSITION_Y')
                    z = spot.getFeature('TRACK_ID')
                    t = spot.getFeature('FRAME')
                    time = int(t) * int(Time_interval)
                    writer2.writerow(
                        [str(id), str(t),
                         str(time), str(x),
                         str(y)])