for i in range(len(testDiffImages)): aggregateDiffImages.append( im.aggregateIntensityImage(testDiffImages[:i + 1])) for i, img in enumerate(testDiffImages): print('peak {}, image {}: max- {}, min {}'.format( peak, i, im.getMaxIntensity(img), im.getMinIntensity(img))) for i, img in enumerate(aggregateDiffImages): print('peak {}, image {}: max- {}, min {}'.format( peak, i, im.getMaxIntensity(img), im.getMinIntensity(img))) testingOutDir = dm.makeOutDir(jellyOutDir, 'testAggPlot') testingOutfile = testingOutDir / 'testPlot for {} - {}.jpeg'.format( stack.name, peak - prePeakInflectionDiff) im.saveDifferenceTestingAggregationImage(relaxedImg, testDiffImages, aggregateDiffImages, testingOutfile) jelly_max_change_frames = { '20200605_JellyTest_825pm_1_xaa': (0, 31), '20200622_Jlo_512pm_xai': (9, 37), '20200624_Beyonce_930pm_xag': (0, 74), '20200622_513pm_Britney_xad': (14, 47), '20200624_Pink_930pm_xaf': (0, 99), '20200624_Pink_930pm_xat': (0, 99) }
def selectInflectionThresholdandDiff(peaksOnBinaryImage, init_movie, recordingName, peak2InflectionDiff, peak2TroughDiff, use_conserved_trough, initializationOutputDir, angleArrImageDir, centroidDir, dynamicRangeDir): if use_conserved_trough: peaksOnBinaryImage = [x - peak2TroughDiff for x in peaksOnBinaryImage] # make directory to store verification jelly plots postInflectionDiffCases = list(range(2, 12)) thresholdCases = [0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5] # out data: pulse angles x testDiffs angleData = np.empty( (len(peaksOnBinaryImage), len(postInflectionDiffCases), len(thresholdCases))) # 3D array angleData[:] = np.nan otherDataCols = np.array(['relaxed', 'peak', 'trough', 'by eye', '']) otherData = np.empty((len(peaksOnBinaryImage), len(otherDataCols))) otherData[:] = np.nan # read in by eye angle measurements byEyeAngleDF = pd.DataFrame(peaksOnBinaryImage, columns=['peaks']) byEyeAngleDF['by eye measurement (0 to 360)'] = np.nan byEyeAngleDFioPath = initializationOutputDir / '{}_byEyeAngles.csv'.format( recordingName) byEyeAngleDF.to_csv(str(byEyeAngleDFioPath), index=False) for i, peak in enumerate(peaksOnBinaryImage): if DEBUG: print("{}: {}".format(i, peak)) troughImg = init_movie[peak + peak2TroughDiff] relaxedImg = init_movie[peak + peak2InflectionDiff] centroidDiff = im.getGrayscaleImageDiff_absolute(troughImg, relaxedImg) binaryCentroidDiff = im.getBinaryJelly(centroidDiff, lower_bound=0.05) centroidRegion = im.findJellyRegion(binaryCentroidDiff)[0] centroid = im.findCentroid_boundingBox(centroidRegion) centroidVerOutFile = centroidDir / 'centroid for {} - {:03}.png'.format( recordingName, peak + peak2InflectionDiff) im.saveJellyPlot( im.getCentroidVerificationImg(centroidDiff, binaryCentroidDiff, centroid), centroidVerOutFile) peakImg = init_movie[peak] peakDiff = im.getGrayscaleImageDiff_absolute(troughImg, peakImg) binaryPeakDiff = im.getBinaryJelly(peakDiff, lower_bound=0.05) if np.sum(binaryCentroidDiff) > np.sum(binaryPeakDiff): binaryPeakDiff = binaryCentroidDiff averagedDynamicRangeMaskedImg = im.dynamicRangeImg_AreaBased( relaxedImg, binaryPeakDiff, 5) dynamicRangeImgOutfile = dynamicRangeDir / 'dynamicRangeImg_{:03}.png'.format( peak + peak2InflectionDiff) im.saveJellyPlot(averagedDynamicRangeMaskedImg, dynamicRangeImgOutfile) # dealing with inflection thresholding testDiffImages = [] for j in postInflectionDiffCases: testImg = init_movie[peak + peak2InflectionDiff + j] testDiff = im.getGrayscaleImageDiff_absolute(testImg, relaxedImg) normalizedTestDiff = testDiff / averagedDynamicRangeMaskedImg testDiffImages.append(normalizedTestDiff) testingOutfile = angleArrImageDir / 'testPlot for {} - {:03}.png'.format( recordingName, peak + peak2InflectionDiff) pulseAngleData = im.saveDifferenceTestingAggregationImage( relaxedImg, testDiffImages, thresholdCases, testingOutfile, discludeVerificationArrayImg=False, centroid=centroid) for n, row in enumerate(pulseAngleData): for m, angle in enumerate(row): angleData[i][m][n] = angle otherData[i][0] = peak + peak2InflectionDiff otherData[i][1] = peak otherData[i][2] = peak + peak2TroughDiff angleDataAsRows = [x.ravel() for x in angleData] pulseAngleOutput = np.concatenate( (np.tile([postInflectionDiffCases], len(thresholdCases)), angleDataAsRows)) otherDataOut = np.concatenate(([otherDataCols], otherData)) # warning: this results in mixed data. This cannot be saved by numpy csv methods. Pandas is easiest way to save. outframe = np.concatenate((otherDataOut, pulseAngleOutput), axis=1) # saves data into verification frame dfOut = pd.DataFrame(outframe) dataTitle = '{}_testDifferenceVerification.csv'.format(recordingName) verificationCSVOutFile = initializationOutputDir / dataTitle dfOut.to_csv(str(verificationCSVOutFile), header=False, index=False) # setting test difference and threshold def runSDanalysis(): if CHIME: dm.chime(MAC, 'input time') print('time to enter by eye angles for each pulse') print('entries must be from 0 to 360') print('Enter \'1\' to continue.') dm.getSelection([1]) byEyeAngleDF = pd.read_csv(str(byEyeAngleDFioPath)) byEyeAngles = byEyeAngleDF['by eye measurement (0 to 360)'].tolist() i = 0 while i < len(byEyeAngles): if byEyeAngles[i] == np.nan: np.delete(angleData, i, 0) byEyeAngles.pop(i) else: i += 1 angleDataShape = angleData.shape diff2byeye = np.empty( (angleDataShape[2], angleDataShape[1], angleDataShape[0])) diff2byeye[:] = np.nan for i in range(angleDataShape[0]): for j in range(angleDataShape[1]): for k in range(angleDataShape[2]): diff2byeye[k][j][i] = dm.angleDifferenceCalc( angleData[i][j][k], byEyeAngles[i]) squaredDiffs = np.square(diff2byeye) summedDiffs = np.sum(squaredDiffs, axis=2) varianceTable = summedDiffs / diff2byeye.shape[2] sdTable = np.sqrt(varianceTable) sdTableMinIndex = list([ np.where(sdTable == np.nanmin(sdTable))[0][0], np.where(sdTable == np.nanmin(sdTable))[1][0] ]) lowSDthresholds = [] lowSDtestFrames = [] for x in np.sort(sdTable.ravel())[0:5]: loc = np.where(sdTable == x) lowSDthresholds.append(loc[0][0]) lowSDtestFrames.append(loc[1][0]) inflectionTestBinaryThreshold = thresholdCases[int( np.median(lowSDthresholds))] inflectionTestDiff = postInflectionDiffCases[int( np.median(lowSDtestFrames))] return inflectionTestBinaryThreshold, inflectionTestDiff, sdTable, sdTableMinIndex inflectionTestBinaryThreshold, inflectionTestDiff, sdTable, sdTableMinIndex = runSDanalysis( ) if CHIME: dm.chime(MAC, 'input time') while True: print('thresholding options: {}'.format(thresholdCases)) print('test frame options: {}'.format(postInflectionDiffCases)) np.set_printoptions(threshold=np.inf) print(np.asarray(sdTable)) print('index of min sd: {}'.format(sdTableMinIndex)) print('selected sd: {}'.format( sdTable[thresholdCases.index(inflectionTestBinaryThreshold)][ postInflectionDiffCases.index(inflectionTestDiff)])) print('Params to change: ') print('select \'1\' to change {} which is {}'.format( 'inflectionTestBinaryThreshold', inflectionTestBinaryThreshold)) print('select \'2\' to change {} which is {}'.format( 'inflectionTestDiff', inflectionTestDiff)) print('select \'3\' to update by eye measurements') print('or \'4\' to continue.') selectionVar = dm.getSelection([1, 2, 3, 4]) if selectionVar == '1': inflectionTestBinaryThreshold = float( dm.getSelection(thresholdCases)) elif selectionVar == '2': inflectionTestDiff = int(dm.getSelection(postInflectionDiffCases)) elif selectionVar == '3': inflectionTestBinaryThreshold, inflectionTestDiff, sdTable, sdTableMinIndex = runSDanalysis( ) else: break chosenSD = sdTable[thresholdCases.index(inflectionTestBinaryThreshold)][ postInflectionDiffCases.index(inflectionTestDiff)] return inflectionTestDiff, inflectionTestBinaryThreshold, chosenSD