import numpy as np scale = 70. sys.path.append("/Users/Alex/Dropbox/Science/Datta lab/Posture Tracking/hsmm-particlefilters/renderer") path_to_behavior_data = "/Users/Alex/Dropbox/Science/Datta lab/Posture Tracking/Test Data" from load_data import load_behavior_data cmds.promptDialog(title="Which Image?", message="Image number:") index = cmds.promptDialog(query=True, text=True) index = int(index) print index+1 images = load_behavior_data(path_to_behavior_data, index+1, "images") image = images[-1] width,height = image.shape mesh_name = "mouse_mesh" cmds.polyPlane(name=mesh_name, width=width, height=height, subdivisionsWidth=width, subdivisionsHeight=height) cmds.move(width/2., 0, height/2., relative=True) for i in range(width*height): translation = cmds.xform(mesh_name +".vtx[%d]" % (i), query=True, worldSpace=True, t=True) x,y = translation[0], translation[2] x,y = int(x), int(y)
from __future__ import division from load_data import load_behavior_data import scipy.ndimage as ndimage from os.path import join, dirname import numpy as np num_frames = 8000 # total number of images in the behavior file num_iterations_dilation = 1 blur_sigma = (2,2) source_behavior_data = "../Test Data/Mouse No Median Filter, No Dilation" dest_behavior_data = "../Test Data/Blurred Edge" path_to_behavior_data = join(dirname(__file__),'..',source_behavior_data) images = load_behavior_data(path_to_behavior_data, num_frames, 'images') out_images = np.zeros_like(images) for i,img in enumerate(images): mask = img == 0 dilation = ndimage.morphology.binary_dilation(mask, iterations=num_iterations_dilation) mask = dilation-mask blurred_img = ndimage.filters.median_filter(img, (5,5)) img[mask] = blurred_img[mask] out_images[i] = img out_images.tofile(join(dest_behavior_data, "Extracted Mouse Images.int16binary"))
def test_single_mouse(): path_to_behavior_data = os.path.join(os.path.dirname(__file__), "..", "Test Data/Blurred Edge") # which_img = 30 # which_img = 731 which_img = 5 from load_data import load_behavior_data image = load_behavior_data(path_to_behavior_data, which_img + 1, "images")[-1] image = image.T[::-1, :].astype("float32") num_particles = 128 ** 2 numCols = 32 numRows = 32 scenefile = os.path.join(os.path.dirname(__file__), "data/mouse_mesh_low_poly3.npz") useFramebuffer = False ms = MouseScene( scenefile, mouse_width=80, mouse_height=80, scale_width=18.0, scale_height=200.0, scale_length=18.0, numCols=numCols, numRows=numRows, useFramebuffer=useFramebuffer, ) ms.gl_init() # Figure out the number of passes we'll be making num_passes = int(num_particles / ms.num_mice) # Let's fill in our particles particle_data = np.zeros((num_particles, 8 + ms.num_bones * 3)) # Set the horizontal offsets position_val = 0 particle_data[1:, :2] = np.random.normal(loc=0, scale=1, size=(num_particles - 1, 2)) # Set the vertical offset particle_data[1:, 2] = np.random.normal(loc=0.0, scale=3.0, size=(num_particles - 1,)) # Set the angles (yaw and roll) theta_val = 0 particle_data[1:, 3] = theta_val + np.random.normal(loc=0, scale=3, size=(num_particles - 1,)) particle_data[1:, 4] = np.random.normal(loc=0, scale=0.01, size=(num_particles - 1,)) # Set the scales (width, length, height) particle_data[0, 5] = np.max(ms.scale_width) particle_data[0, 6] = np.max(ms.scale_length) particle_data[0, 7] = np.max(ms.scale_height) particle_data[1:, 5] = np.random.normal(loc=18, scale=2, size=(num_particles - 1,)) particle_data[1:, 6] = np.random.normal(loc=18, scale=2, size=(num_particles - 1,)) particle_data[1:, 7] = np.abs(np.random.normal(loc=200.0, scale=10, size=(num_particles - 1,))) # Grab the baseline joint rotations rot = ms.get_joint_rotations().copy() rot = np.tile(rot, (num_passes, 1, 1)) particle_data[:, 8::3] = rot[:, :, 0] particle_data[:, 9::3] = rot[:, :, 1] particle_data[:, 10::3] = rot[:, :, 2] # Add noise to the baseline rotations (just the pitch and yaw for now) # particle_data[1:,8::3] += np.random.normal(scale=20, size=(num_particles-1, ms.num_bones)) particle_data[1:, 9 + 6 :: 3] += np.random.normal(scale=20, size=(num_particles - 1, ms.num_bones - 2)) particle_data[1:, 10::3] += np.random.normal(scale=20, size=(num_particles - 1, ms.num_bones)) likelihoods, posed_mice = ms.get_likelihood( image, x=position_val, y=position_val, theta=theta_val, particle_data=particle_data, return_posed_mice=True ) # L = ms.likelihood.T.ravel() particle_rotations = np.hstack((particle_data[:, 9::3], particle_data[:, 10::3])) real_rotations = np.hstack((rot[:, :, 1], rot[:, :, 2])) rotation_diffs = np.sum((particle_rotations - real_rotations) ** 2.0, 1) figure() plot(rotation_diffs, likelihoods, ".k") ylabel("Likelihood") xlabel("Rotation angle differences") title("Rotation angle difference versus likelihood") binrange = (0, 3000) num_bins = 10 bins = np.linspace(binrange[0], binrange[1], num_bins) index = np.digitize(rotation_diffs, bins) means = [np.mean(likelihoods[index == i]) for i in range(num_bins)] errs = [np.std(likelihoods[index == i]) for i in range(num_bins)] errorbar(bins, means, yerr=errs, linewidth=2) # savefig("/Users/Alex/Desktop/angle vs likelihood.png") # figure(); imshow(ms.likelihood) # figure(); imshow(ms.data); colorbar() # figure(); imshow(ms.diffmap); colorbar() # Find the five best mice idx = np.argsort(likelihoods) fivebest = np.hstack(posed_mice[idx[-5:]]) # Show first the raw mouse, then my hand-posed mouse, and then the five best poses figure() title("Five best (best, far right)") imshow(np.hstack((ms.mouse_img, posed_mice[0], fivebest))) plt.clim(0, 300) vlines(ms.mouse_width, 0, ms.mouse_height, linewidth=3, color="w") text( ms.mouse_width / 2.0, ms.mouse_width * 0.9, "Real Mouse", horizontalalignment="center", verticalalignment="center", color="white", ) vlines(ms.mouse_width * 2, 0, ms.mouse_height, linewidth=3, color="w") text( ms.mouse_width * 1.5, ms.mouse_width * 0.9, "Unposed Mouse", horizontalalignment="center", verticalalignment="center", color="white", ) return ms, rotation_diffs, likelihoods, particle_data, posed_mice