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
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
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
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
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) print(prePeakInflectionDiff) prePeakInflectionDiff += 5
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)