예제 #1
0
def getBinaryAreas(filesSubset, lowerThreshold, DEBUG = False):
    binaryImageAreas = []
    for i in range(len(filesSubset)):
        if i % 100 == 0 and DEBUG: print("recieved areas up to pulse: {}".format(i))
        jellyimage = im.getJellyImageFromFile(str(filesSubset[i]))
        jellyimagebinary = im.getBinaryJelly(jellyimage, lowerThreshold)
        jellyBinaryArea = im.findBinaryArea(jellyimagebinary)
        binaryImageAreas.append(jellyBinaryArea)

    return binaryImageAreas
예제 #2
0
def downturnFinder(files, refactoryPeriod, lowerThresh, numberOfConsecutiveDrops, peak2InflectionDiff, peak2TroughDiff, DEBUG = False):

    print('searching for peaks (downturnfinder)')

    i = 0
    numFiles = len(files)

    peakIndicies = []

    # initializes lists with 'numberOfConsecutiveDrops' of files
    def reinitializeTestFramesAndAreas(j):
        testFrames = []  # this list should never be more than 5 entries long, ex. [51, 52, 53, 54, 55]
        testAreas = []  # this list should never be more than 5 entries long, ex. [253, 255, 256, 255, 255]

        while len(testFrames) < numberOfConsecutiveDrops and j < numFiles:
            image = im.getJellyImageFromFile(files[j])
            binary_image = im.getBinaryJelly(image, lowerThresh)
            area = im.findBinaryArea(binary_image)

            testFrames.append(j)
            testAreas.append(area)
            j += 1

        return testFrames, testAreas, j

    testFrames, testAreas, i = reinitializeTestFramesAndAreas(i)

    while i < numFiles:
        isDownturn = dm.is_downturn(0, testAreas, numberOfConsecutiveDrops)

        if DEBUG: print('i: {}, isDownturn: {}, testAreas: {}, testFrames: {}'.format(i, isDownturn, testAreas, testFrames))

        if isDownturn:
            peak = i - numberOfConsecutiveDrops
            if peak + peak2InflectionDiff >= 0 and peak + peak2TroughDiff < numFiles:
                peakIndicies.append(peak)

            i += refactoryPeriod

            testFrames, testAreas, i = reinitializeTestFramesAndAreas(i)

        else:
            testFrames.pop(0)
            testAreas.pop(0)

            image = im.getJellyImageFromFile(files[i])
            binary_image = im.getBinaryJelly(image, lowerThresh)
            area = im.findBinaryArea(binary_image)

            testFrames.append(i)
            testAreas.append(area)
            i += 1

    return peakIndicies
예제 #3
0
    def reinitializeTestFramesAndAreas(j):
        testFrames = []  # this list should never be more than 5 entries long, ex. [51, 52, 53, 54, 55]
        testAreas = []  # this list should never be more than 5 entries long, ex. [253, 255, 256, 255, 255]

        while len(testFrames) < numberOfConsecutiveDrops and j < numFiles:
            image = im.getJellyImageFromFile(files[j])
            binary_image = im.getBinaryJelly(image, lowerThresh)
            area = im.findBinaryArea(binary_image)

            testFrames.append(j)
            testAreas.append(area)
            j += 1

        return testFrames, testAreas, j
예제 #4
0
def differenceAngleFinder(files):

    global currentSegmentEndingFrame

    i = 0

    # movement parameters
    firstStationaryAfterMovement = 0  # first stationary frame (i) after movement
    isMoving = False
    isQuestionablyStationary = False
    centroidBefore = None
    lastStationaryCentroid = None
    counter = 0
    peak = 0
    pulseCountInQuestionablyStationary = 0
    centroid = None

    data = []
    movingPeaks = []

    # initializes lists with 'numConsecutiveDrops' of files
    def reinitializeTestFramesAndAreas(j):
        testFrames = [
        ]  # this list should never be more than 5 entries long, ex. [51, 52, 53, 54, 55]
        testAreas = [
        ]  # this list should never be more than 5 entries long, ex. [253, 255, 256, 255, 255]

        while len(testFrames) < numConsecutiveDrops and j < framesInChunk:
            image = im.getJellyImageFromFile(files[j])
            binary_image = im.getBinaryJelly(image, lowerThreshold)
            area = im.findBinaryArea(binary_image)

            testFrames.append(j)
            testAreas.append(area)
            j += 1

        return testFrames, testAreas, j

    # function to save out data
    def saveOutData():
        df = pd.DataFrame(data,
                          columns=[
                              'global frame', 'chunk frame', 'angle',
                              'centroid x', 'centroid y'
                          ])
        if DEBUG: print(df.head())
        dataTitle = '{}_{:03}.csv'.format(chunkName, movementSegment)
        df.to_csv(str(angleOutputDir / dataTitle), index=False)

    testFrames, testAreas, i = reinitializeTestFramesAndAreas(i)

    try:
        while i < framesInChunk:

            isDownturn = dm.is_downturn(0, testAreas, numConsecutiveDrops)

            if isDownturn:
                peak = i - numConsecutiveDrops

                print("chunk: {}, i: {}, peak: {}".format(chunkName, i, peak))

                # checks that peaks are within testing bounds
                if peak + peak2InflectionDiff >= 0 and peak + peak2TroughDiff < framesInChunk:

                    troughInfile = files[peak + peak2TroughDiff]
                    relaxedInfile = files[peak + peak2InflectionDiff]

                    troughImg = im.getJellyGrayImageFromFile(troughInfile)
                    relaxedImg = im.getJellyGrayImageFromFile(relaxedInfile)

                    centroidDiff = im.getGrayscaleImageDiff_absolute(
                        troughImg, relaxedImg)
                    binaryCentroidDiff = im.getBinaryJelly(centroidDiff,
                                                           lower_bound=0.05,
                                                           upper_bound=1)
                    centroidRegion = im.findJellyRegion(binaryCentroidDiff)
                    centroid = im.findCentroid_boundingBox(centroidRegion)

                    if lastStationaryCentroid is None:
                        lastStationaryCentroid = centroid

                    if CONFIRMATIONIMAGES:
                        im.saveJellyPlot(
                            im.getCentroidVerificationImg(
                                centroidDiff, binaryCentroidDiff, centroid),
                            str(confirmationImagesPath /
                                '{}_{}_centroid.png'.format(peak, chunkName)))

                    if isMoving:

                        data.append([
                            peak + lastFrameOfPreviousChunk, peak, np.nan,
                            centroid[0], centroid[1]
                        ])

                        movedBefore = isMoving
                        isMoving = im.distance(centroid, lastStationaryCentroid
                                               ) > movementThreshold2KeepMoving

                        lastStationaryCentroid = centroid

                        if movedBefore and not isMoving:
                            firstStationaryAfterMovement = i
                            pulseCountInQuestionablyStationary = 0
                            isQuestionablyStationary = True

                    elif isQuestionablyStationary:

                        data.append([
                            peak + lastFrameOfPreviousChunk, peak, np.nan,
                            centroid[0], centroid[1]
                        ])

                        isMoving = im.distance(
                            centroid, lastStationaryCentroid
                        ) > movementThreshold4reinitialization

                        if isMoving:
                            movingPeaks.append(peak)
                            isQuestionablyStationary = False

                        pulseCountInQuestionablyStationary += 1

                        if i - firstStationaryAfterMovement > numFrames2ConfirmStationary:
                            # now there is confirmed time after initial stationary point

                            if firstStationaryAfterMovement == 0:
                                data = []
                            else:
                                # must mutate data to take out
                                data = data[:
                                            -pulseCountInQuestionablyStationary]
                                currentSegmentEndingFrame = i
                                saveSegmentVariableParams()
                                saveOutData()
                                data = []

                            i = firstStationaryAfterMovement

                            # peak2InflectionDiff, peak2TroughDiff, postPeakRefractoryPeriod, infflectionTestDiff,
                            # inflectionTestBinaryThreshold, and chosen SD are all static.

                            initialize_params(files, i)

                            isQuestionablyStationary = False

                            pulseCountInQuestionablyStationary = 0

                        # until count from current i to last stationary i reaches this point,
                        # the program is in a holding pattern of sorts.

                    else:
                        testInfile = files[peak + peak2InflectionDiff +
                                           inflectionTestDiff]
                        testImg = im.getJellyGrayImageFromFile(testInfile)

                        if CONFIRMATIONIMAGES:
                            plt.imsave(
                                str(confirmationImagesPath /
                                    '{}_{}_interestFrames.png'.format(
                                        peak, chunkName)),
                                im.juxtaposeImages(
                                    np.array([[
                                        relaxedImg, testImg, peakImg, troughImg
                                    ]])))

                        if centroidBefore is not None:
                            reinitializeAreaPlot = im.distance(
                                centroid, centroidBefore
                            ) > movementThreshold4newNormalizationImg
                            if reinitializeAreaPlot:
                                peakInfile = files[peak]
                                peakImg = im.getJellyGrayImageFromFile(
                                    peakInfile)
                                peakDiff = im.getGrayscaleImageDiff_absolute(
                                    troughImg, peakImg)
                                binaryPeakDiff = im.getBinaryJelly(
                                    peakDiff, lower_bound=0.05, upper_bound=1)
                                averagedDynamicRangeMaskedImg = im.dynamicRangeImg_AreaBased(
                                    relaxedImg, binaryPeakDiff, 5)

                        else:
                            peakInfile = files[peak]
                            peakImg = im.getJellyGrayImageFromFile(peakInfile)
                            peakDiff = im.getGrayscaleImageDiff_absolute(
                                troughImg, peakImg)
                            binaryPeakDiff = im.getBinaryJelly(
                                peakDiff, lower_bound=0.05, upper_bound=1)
                            averagedDynamicRangeMaskedImg = im.dynamicRangeImg_AreaBased(
                                relaxedImg, binaryPeakDiff, 5)

                        centroidBefore = centroid

                        if CONFIRMATIONIMAGES:
                            im.saveJellyPlot(
                                averagedDynamicRangeMaskedImg,
                                str(confirmationImagesPath /
                                    '{}_{}_dynRng.png'.format(peak,
                                                              chunkName)))

                        testDiff = im.getGrayscaleImageDiff_absolute(
                            testImg, relaxedImg)
                        normalizedTestDiff = testDiff / averagedDynamicRangeMaskedImg

                        binaryDiffImg = im.getBinaryJelly(
                            normalizedTestDiff,
                            lower_bound=inflectionTestBinaryThreshold)

                        biggestRegion = im.findJellyRegion(binaryDiffImg)

                        if biggestRegion is not None:
                            local_com = im.findCentroid_regionProp(
                                biggestRegion)
                            zeroDegreePoint = (centroid[0], 0)

                            angle = dm.getAngle(zeroDegreePoint, centroid,
                                                local_com)

                            if CONFIRMATIONIMAGES:
                                im.saveJellyPlot(
                                    binaryDiffImg,
                                    str(confirmationImagesPath /
                                        '{}_{}_angle.png'.format(
                                            peak, chunkName)),
                                    [centroid, local_com, zeroDegreePoint])
                        else:
                            angle = np.nan

                            if CONFIRMATIONIMAGES:
                                im.saveJellyPlot(
                                    binaryDiffImg,
                                    str(confirmationImagesPath /
                                        '{}_{}_angle.png'.format(
                                            peak, chunkName)), [centroid])

                        data.append([
                            peak + lastFrameOfPreviousChunk, peak, angle,
                            centroid[0], centroid[1]
                        ])

                        movedBefore = isMoving
                        isMoving = im.distance(
                            centroid, lastStationaryCentroid
                        ) > movementThreshold4reinitialization

                        if isMoving and not movedBefore:
                            isQuestionablyStationary = False

                            lastStationaryCentroid = centroid

                i += postPeakRefractoryPeriod
                counter += 1

                testFrames, testAreas, i = reinitializeTestFramesAndAreas(i)

            else:
                testFrames.pop(0)
                testAreas.pop(0)

                image = im.getJellyImageFromFile(files[i])
                binary_image = im.getBinaryJelly(image, lowerThreshold)
                area = im.findBinaryArea(binary_image)

                testFrames.append(i)
                testAreas.append(area)
                i += 1
                counter += 1

    except Exception as error:
        print('{} error occured.'.format(error))
        print(
            "chunkName: {}, index: {}, isMoving: {}, isQStat: {}, centroid: {}"
            .format(chunkName, i, isMoving, isQuestionablyStationary,
                    str(centroid)))
        raise

    finally:
        currentSegmentEndingFrame = i
        saveSegmentVariableParams()
        saveOutData()
예제 #5
0
    upperThreshold = 0.9  # just used for testing initially
    lowerThreshold = 0.1  # just used for testing initially
    prePeakInflectionDiff = None
    postPeakTroughDiff = None
    postInflectionTestDiff = 5
    initialRefracPeriod = 60  # number of frames needed in initial assessment of peaks. More accurate comes after peak interval testing.
    postPeakRefractoryPeriod = None
    movementThreshold4reinitialization = 10
    thresholdBinaryAreaForPeak = None

    binaryImageAreas = []
    for file in files:
        print(file.name)

        jellyimage = im.getJellyImageFromFile(str(file))
        jellyimagebinary = im.getBinaryJelly(jellyimage, lowerThreshold,
                                             upperThreshold)
        jellyBinaryArea = im.findBinaryArea(jellyimagebinary)
        binaryImageAreas.append(jellyBinaryArea)

    # gets peak frame nums from binaryImageAreas
    peaksOnBinaryImage = am.downturnFinder(files, initialRefracPeriod,
                                           lowerThreshold, upperThreshold)
    print(peaksOnBinaryImage)

    troughsOnBinaryImage = dm.getTroughs(binaryImageAreas)
    print(troughsOnBinaryImage)

    prePeakInflectionDiff = dm.getLikelyInflectionDiff(binaryImageAreas,
                                                       peaksOnBinaryImage)