def load_grid_size_test(out_folder, in_folder): #res = np.loadtxt(os.path.join(out_folder, "res.txt"), delimiter=' ', skiprows=1) mask, mask_cell_border, fx, fy, tx, ty, u, v = load_data( in_folder, pixel_size) contractilities = [] strain_energies = [] lengths = read_lenghs_from_file_names(out_folder) stress_tensors = [] for l in tqdm(lengths): tx_int, ty_int, u_int, v_int, mask_interp, pixelsize1, pixelsize2, area2 = zoom_fields( tx, ty, u, v, l, pixel_size, mask, order=3) ep = strain_energy_points(u_int, v_int, tx_int, ty_int, pixelsize1, pixelsize2) cenergy = np.sum(ep[mask_interp]) strain_energies.append(cenergy) contractile_force, proj_x, proj_y, center = contractillity( tx_int, ty_int, pixelsize2, mask_interp) contractilities.append(contractile_force) l = int(l) if int(l) == l else l l_label = str(np.round(l, 2)).replace(".", "_") print(l_label) stress_tensors.append( np.load(os.path.join(out_folder, "stress_tensor%s.npy" % l_label))) return lengths, stress_tensors, strain_energies, contractilities
def compare_strain_energies(u, v, fx_b, fy_b, fx_bf, fy_bf, fx_nf, fy_nf, fx, fy): mask = binary_erosion(np.ones((100, 100)), iterations=10).astype(bool) energy_points_f = strain_energy_points( u, v, fx_b, fy_b, 1, 1) # contractile energy at any point strain_b = np.sum(energy_points_f[mask]) energy_points_f = strain_energy_points( u, v, fx_bf, fy_bf, 1, 1) # contractile energy at any point strain_bf = np.sum(energy_points_f[mask]) energy_points_f = strain_energy_points( u, v, fx_nf, fy_nf, 1, 1) # contractile energy at any point strain_fnf = np.sum(energy_points_f[mask]) energy_points_f = strain_energy_points( u, v, fx, fy, 1, 1) # contractile energy at any point strain_f = np.sum(energy_points_f[mask]) print("strain energy\n", "input: ", strain_b, "\ninput filtered: ", strain_bf, "\n output unfiltered: ", strain_fnf, "\noutput filtered: ", strain_f) return strain_b, strain_bf, strain_fnf, strain_f
def try_grid_sizes(lengths, tx, ty, u, v, out_folder, pixelsize, verbose=False): strain_energies = [] contractile_forces = [] order = 3 # doesnt seem to matter for fluctuations of contractillity and strain energy for l in tqdm(lengths): tx_int, ty_int, u_int, v_int, mask_interp, pixelsize1, pixelsize2, area2 = zoom_fields( tx, ty, u, v, l, pixelsize, mask, order=3) ep = strain_energy_points(u_int, v_int, tx_int, ty_int, pixelsize1, pixelsize2) cenergy = np.sum(ep[mask_interp]) contractile_force, proj_x, proj_y, center = contractillity( tx_int, ty_int, pixelsize2, mask_interp) strain_energies.append(cenergy) contractile_forces.append(contractile_force) # calcualte stresses with the interpolated force field UG_sol, stress_tensor = stress_wrapper(mask_interp, tx_int * area2, ty_int * area2, young=1, sigma=0.5, verbose=verbose) l_label = str(np.round(l, 2)).replace(".", "_") np.save(os.path.join(out_folder, "stress_tensor%s.npy" % l_label), stress_tensor) # monitor contractility /strain energy change strain_energies = np.array(strain_energies) contractilities = np.array(contractile_forces) out = np.array([lengths, strain_energies, contractilities]).T np.savetxt(os.path.join(out_folder, "res.txt"), out, fmt="%.4e", delimiter=' ', header="grid_element_length strain_energy contractility")
fx_f_nf, fy_f_nf = traction_wrapper(u_b, v_b, pixelsize, h, young, mask=mask, sigma=0.5, filter=None, fs=None) # assuming pixelsize == 1 # show_quiver(fx_f_nf, fy_f_nf) fx_f, fy_f = traction_wrapper(u_b, v_b, pixelsize, h, young, mask=mask, sigma=0.5, filter="gaussian", fs=filter_size) # assuming pixelsize == 1 if plotting: paras["filter"] = [0, 1] f, a = show_forces_forward(x=fx_b, y=fy_b, **paras) f.savefig(os.path.join(out_folder2, str(r) + "input.svg")) paras["filter"] = [0, 1] if r < 15 else [0, 4] f, a = show_forces_forward(x=fx_f_nf, y=fy_f_nf, **paras) f.savefig(os.path.join(out_folder2, str(r) + "output_unfiiltered.svg")) f, a = show_forces_forward(x=fx_f, y=fy_f, **paras) f.savefig(os.path.join(out_folder2, str(r) + "output_filtered.svg")) plt.close("all") energy_points = strain_energy_points(u_b, v_b, fx_b, fy_b, 1, 1) # contractile energy at any point cont_input.append(np.sum(energy_points[mask])) energy_points = strain_energy_points(u_b, v_b, fx_f_nf, fy_f_nf, 1, 1) # contractile energy at any point cont_output_unfiltered.append(np.sum(energy_points[mask])) energy_points = strain_energy_points(u_b, v_b, fx_f, fy_f, 1, 1) # contractile energy at any point cont_output_filtered.append(np.sum(energy_points[mask])) out_arr = np.stack([np.array(cont_input), np.array(cont_output_unfiltered), np.array(cont_output_filtered)]).T header = "input,unfiltered,filtered" np.savetxt(os.path.join(output_folder, "out.txt"), out_arr, header=header, delimiter=",") la = np.loadtxt(os.path.join(output_folder, "out.txt"),delimiter=",") cont_input = la[:,0] cont_output_unfiltered = la[:,1] cont_output_filtered = la[:,2] cont_input_norm=np.array(cont_input)/np.array(cont_input)
plt.bar(list(range(8)), [ strain_b, strain_bf, strain_fnf, strain_f, strain_b1, strain_bf1, strain_fnf1, strain_f1 ]) plt.figure() plt.bar(list(range(8)), [ contractile_force_b, contractile_force_bf, contractile_force_fnf, contractile_force_f, contractile_force_b1, contractile_force_bf1, contractile_force_fnf1, contractile_force_f1 ]) slicing_plot(u_b, fx_b, fx_bf, fx_f_nf, fx_f) slicing_plot(u_b1, fx_b, fx_bf, fx_f_nf1, fx_f1) ep1 = strain_energy_points(u_b1, v_b1, fx_f_nf1, fy_f_nf1, 1, 1) ep2 = strain_energy_points(u_b1, v_b1, fx_f1, fy_f1, 1, 1) show_quiver(u_b1, v_b1) show_quiver(fx_f_nf1, fy_f_nf1) show_quiver(fx_f1, fy_f1) plt.figure() plt.imshow(ep1) plt.colorbar() plt.figure() plt.imshow(ep2) plt.colorbar() ### illustrating the method plt.close("all") fx = np.random.uniform(-1, 1, (10, 10))
# This function assumes a finite substrate height unless you set h="infinite". # plotting the traction field fig2, ax = show_quiver(tx, ty, cbar_str="tractions\n[Pa]") ## measuring force generation mask = plt.imread(os.path.join(folder, "force_measurement.png")).astype(bool) mask = binary_fill_holes( mask) # the mask should be a single patch without holes # changing the masks dimensions to fit to the deformation and traction field: mask = interpolation(mask, dims=u.shape) # Strain energy: # Calculating a map of strain energy energy_points = strain_energy_points(u, v, tx, ty, ps1, ps2) # J/pixel # Calculating the total strain energy in the area of the cells strain_energy = np.sum(energy_points[mask]) # 1.92*10**-13 J # Contractillity contractile_force, proj_x, proj_y, center = contractillity( tx, ty, ps2, mask) # 2.01*10**-6 N ## measuring stresses # first mask: The area used for Finite Elements Methods. # should encircle all forces generated by the cell colony mask_FEM = plt.imread(os.path.join(folder, "FEM_area.png")).astype(bool) mask_FEM = binary_fill_holes( mask_FEM) # the mask should be a single patch without holes # changing the masks dimensions to fit to the deformation and traction field:
contractile_forces = [] for u, v in zip(us, vs): #show_quiver(u, v, filter=[0, 7]) pixelsize1 = pixel_size pixelsize2 = pixel_size * np.mean(np.array(im1.shape) / np.array(u.shape)) mask_interp = interpolation(mask, u.shape) tx, ty = TFM_tractions(u, v, pixelsize1=pixelsize1 * 10**-6, pixelsize2=pixelsize2 * 10**-6, h=h, young=young, sigma=sigma, filter="gaussian", fs=fs * 10**-6) ep = strain_energy_points(u, v, tx, ty, pixelsize1, pixelsize2) cenergy = np.sum(ep[mask_interp]) contractile_force, proj_x, proj_y, center = contractillity( tx, ty, pixelsize2, mask_interp) strain_energies.append(cenergy) contractile_forces.append(contractile_force) #show_quiver(tx,ty,filter=[0,3]) plt.figure() plt.plot(ws_range, strain_energies) plt.ylim(0, np.max(strain_energies) * 1.2)