def autoLowerThreshold(averageTroughBinaryArea, peak2TroughDiff, peaksOnBinaryImage, fileSubset, thresholdingDir, recordingName): # completed automated based on averageTroughBinaryArea thresholdStep = 0.005 chosenThreshold = 0.05 testTroughAverage = averageTroughBinaryArea + 1 while testTroughAverage > averageTroughBinaryArea: chosenThreshold += thresholdStep troughAreas = [] for i, peak in enumerate(peaksOnBinaryImage): if peak + peak2TroughDiff < len(fileSubset): troughInfile = fileSubset[peak + peak2TroughDiff] troughImg = im.getJellyGrayImageFromFile(troughInfile) binaryTroughImg = im.getBinaryJelly(troughImg, chosenThreshold) jellyTroughBinaryArea = im.findBinaryArea(binaryTroughImg) troughAreas.append(jellyTroughBinaryArea) testTroughAverage = np.mean(troughAreas) print('chosenThreshold: {} (test area, {}; target area, {})'.format(chosenThreshold, testTroughAverage, averageTroughBinaryArea)) for i, peak in enumerate(peaksOnBinaryImage): if peak + peak2TroughDiff < len(fileSubset): peakInfile = fileSubset[peak] troughInfile = fileSubset[peak + peak2TroughDiff] peakImg = im.getJellyGrayImageFromFile(peakInfile) troughImg = im.getJellyGrayImageFromFile(troughInfile) binaryPeakImg = im.getBinaryJelly(peakImg, chosenThreshold) binaryTroughImg = im.getBinaryJelly(troughImg, chosenThreshold) im.saveJellyPlot(im.juxtaposeImages(np.array([[binaryPeakImg, binaryTroughImg]])), (thresholdingDir / '{}_thresholdVerification_{}.png'.format(recordingName, peak))) return chosenThreshold
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()
def selectAverageTroughBinaryArea(fileSubset, thresholdingDir, recordingName, peaksOnBinaryImage, peak2InflectionDiff, peak2TroughDiff): maxIntensities = [] minIntensities = [] for i, peak in enumerate(peaksOnBinaryImage): if peak + peak2InflectionDiff >= 0 and peak + peak2TroughDiff < len(fileSubset): troughInfile = fileSubset[peak + peak2TroughDiff] relaxedInfile = fileSubset[peak + peak2InflectionDiff] troughImg = im.getJellyGrayImageFromFile(troughInfile) relaxedImg = im.getJellyGrayImageFromFile(relaxedInfile) centroidDiff = im.getGrayscaleImageDiff_absolute(troughImg, relaxedImg) binaryCentroidDiff = im.getBinaryJelly(centroidDiff, lower_bound=0.05) maskedImg = im.applyMask2Img(binaryCentroidDiff, relaxedImg) jellyRegion = im.findJellyRegionWithGray(binaryCentroidDiff, maskedImg) maxIntensity = jellyRegion.max_intensity minIntensity = jellyRegion.min_intensity maxIntensities.append(maxIntensity) minIntensities.append(minIntensity) indensityDifference = np.mean(maxIntensities) - np.mean(minIntensities) lowerThreshold = np.mean(minIntensities) + (0.1 * indensityDifference) print('peak2TroughDiff: {}'.format(peak2TroughDiff)) while True: troughAreas = [] for peak in peaksOnBinaryImage: peakInfile = fileSubset[peak] troughInfile = fileSubset[peak+peak2TroughDiff] peakImg = im.getJellyGrayImageFromFile(peakInfile) troughImg = im.getJellyGrayImageFromFile(troughInfile) binaryPeakImg = im.getBinaryJelly(peakImg, lowerThreshold) binaryTroughImg = im.getBinaryJelly(troughImg, lowerThreshold) im.saveJellyPlot(im.juxtaposeImages(np.array([[binaryPeakImg, binaryTroughImg]])), (thresholdingDir / '{}_thresholdVerification_{}.png'.format(recordingName, peak))) jellyTroughBinaryArea = im.findBinaryArea(binaryTroughImg) troughAreas.append(jellyTroughBinaryArea) if CHIME: dm.chime(MAC, 'input time') print('average trough area: {}, sd of trough areas: {}'.format(np.mean(troughAreas), np.std(troughAreas))) print('Change thresholds: ') print('select \'1\' to change {} which is {}'.format('lowerThreshold', lowerThreshold)) print('select \'2\' to remove a peak from peaksOnBinaryImage') print('or \'3\' to continue.') selectionVar = dm.getSelection([1, 2, 3]) if selectionVar == '1': lowerThreshold = dm.reassignFloatVariable(lowerThreshold, 'lowerThreshold') dm.replaceDir(thresholdingDir) elif selectionVar == '2': print('peaksOnBinaryImage: {}'.format(peaksOnBinaryImage)) index2Pop = int(dm.getSelection(list(range(len(peaksOnBinaryImage))))) peaksOnBinaryImage.pop(index2Pop) else: return np.mean(troughAreas)