def create_loss_map(img, vesselPts, threshold, iter): h, w = np.array(img).shape loss_map = [] for _ in range(w): loss_map.append([0.0 for _ in range(h)]) for ix, iy in np.ndindex(img.shape): # NEED TO ONLY APPLY TO NON-VESSEL GRID LOCATIONS!!!!!! if not (ix, iy) in vesselPts: # if too low, waste production and death loss = 2 * hillPlusOne(img[ix, iy], 2) - 0.2 # if loss < 1: # loss = 1 loss_map[ix][iy] = loss # for ix,iy in np.ndindex(img.shape): # # NEED TO ONLY APPLY TO NON-VESSEL GRID LOCATIONS!!!!!! # if not (ix, iy) in vesselPts: # val = img[ix,iy] # loss = 1 # if val >= threshold: # loss_map[ix][iy] = 1 / (1 + val) # else: # loss_map[ix][iy] = loss # loss = gaussian(img[ix,iy]/img.mean()) # for ix,iy in np.ndindex(img.shape): # # loss_map[ix][iy] = gaussian(img[ix,iy]/img.mean()) # loss_map[ix][iy] = 1 / (1 + img[ix,iy]) # ideal = np.ones(img.shape)[1:,1:] # val = np.abs(ideal - img[1:,1:]) return np.array(loss_map)[1:, 1:]
def get_dxopt_dp(lin_solver, df_dx, d_dp_df_dx, d_dx_df_dx, A, b, xopt, p): ans = np.zeros(xopt.shape + p.shape) for index in np.ndindex(*p.shape): delta_p_direction = np.zeros(p.shape) delta_p_direction[index] = 1. temp = get_dxopt_delta_p(lin_solver, df_dx, d_dp_df_dx, d_dx_df_dx, A, b, xopt, p, delta_p_direction) ans[(slice(None),)+index] = temp[:len(xopt)] return ans
def loss_health(img, iter, vessel_points): threshold = 0.9 # 2D array of neutrient values # sum of sigmoid values (high N is low low, low N is high loss) total_loss = 0.0 for ix, iy in np.ndindex(img.shape): if vessel_points[(ix, iy)] > 0.0: val = img[ix, iy] loss = 1 if val >= threshold: loss = 1 / (1 + img[ix, iy]) total_loss += loss return total_loss
def loss_health(img, vesselPts, threshold, iter): # 2D array of neutrient values # sum of sigmoid values (high N is low low, low N is high loss) total_loss = 0.0 for ix, iy in np.ndindex(img.shape): # NEED TO ONLY APPLY TO NON-VESSEL GRID LOCATIONS!!!!!! if not (ix, iy) in vesselPts: # if too low, waste production and death loss = 2 * hillPlusOne(img[ix, iy], 2) - 0.2 # if loss < 1: # loss = -1 total_loss = total_loss + (loss) print('LabMeatMain Line 134 LOSS: ', total_loss, iter) return total_loss
def create_loss_map(img, iter): # np_img = np.array(img) # mn, mx = np_img.min(), np_img.max() # np_img = (np_img - mn) / (mx - mn) h, w = np.array(img).shape loss_map = [] for _ in range(w): loss_map.append([0.0 for _ in range(h)]) for ix,iy in np.ndindex(img.shape): # loss_map[ix][iy] = exp_decay(img[ix,iy]) loss_map[ix][iy] = 1 / (1 + img[ix,iy]) # ideal = np.ones(img.shape)[1:,1:] # val = np.abs(ideal - np_img[1:,1:]) return np.array(loss_map)[1:,1:]
def loss_health(img, iter): # np_img = np.array(img) # mn, mx = np_img.min(), np_img.max() # np_img = (np_img - mn) / (mx - mn) # 2D array of neutrient values # sum of sigmoid values (high N is low low, low N is high loss) total_loss = 0.0 for ix,iy in np.ndindex(img.shape): loss = 1 / (1 + img[ix,iy]) # loss = exp_decay(img[ix,iy]) # loss = gaussian(img[ix,iy]/img.mean() total_loss = total_loss + (loss) # ideal = np.ones(img.shape)[1:,1:] # total_loss = np.sum(np.abs(ideal - (img[1:,1:] /img.mean()))) # total_loss = np.abs(ideal - (img[1:,1:])).mean() # total_loss = np.mean(1 / (1 + img[1:,1:]) print('LabMeatMain Line 171 LOSS: ', total_loss, iter) return total_loss
def step_ODE(values, params, pdeDelta, vas_structure, shape_of_img): # Computes the changes to the protein values for all the cells # based on each cells ODE # ode_Delta = np.zeros(np.array(vas_structure.img).shape+(2,)) # # for cellId in range(0, HowManyCells): # for ix,iy in np.ndindex(np.array(vas_structure.img).shape): # # update by GRN, one cell is [P0, P1] # cellId = (ix, iy) # # values = [vas_structure.nutrient_values[cellId], vas_structure.product_values[cellId]] # # print('pdeDelta: ', pdeDelta, pdeDelta.shape) # # ode_Delta.append(oneCell_ODE(values[cellId,:], params, cellId, pdeDelta[cellId,:], vas_structure)) # ode_Delta[cellId] = oneCell_ODE(values[cellId], params, cellId, pdeDelta[cellId], vas_structure) # shape = np.array(vas_structure.img).shape # print(shape) r, c = shape_of_img ode_Delta = [] for _ in range(r): ode_Delta.append([0 for _ in range(c)]) for ix,iy in np.ndindex(shape_of_img): cellId = (ix, iy) ode_Delta[ix][iy] = oneCell_ODE(values[cellId], params, cellId, pdeDelta[cellId], vas_structure) return np.array(ode_Delta, dtype=np.float64)
def solveModelODEPDE(vas_structure, times, params = (), nonLinear = False, movablePts = []): #Simple solver that just uses the ODE PDE functions as update rules # Unrolls the dynamics for the number of sample times # Returns the dynamics which is a list of shape [HowManyCells,VarCount] # Returns the fitness for each iteration, the list of updates on the product # at the vessel cells (params,) = params trajectories = [] fitnessList = [] odeDeltaList = [] pdeDeltaList = [] detlat = times[1]-times[0] shape_of_img = np.array(vas_structure.product_values).shape values = np.zeros(shape_of_img + (2,), dtype=np.float64) vessel_points = vas_structure.pts_on_vessels vesselCellIds = [(ix, iy) for ix,iy in np.ndindex(shape_of_img) if vessel_points[(ix,iy)] > 0.0] pdeDelta = np.zeros(shape_of_img + (VarCount,), dtype=np.float64) # then do the ODE and PDE for t in range(0,len(times)): # t_start = time.time() trajectories.append(values) # ode_start = time.time() odeDelta = step_ODE(values, params, pdeDelta, vas_structure, shape_of_img) #ODE updates # ode_end = time.time() # print(' step ODE: ', ode_end-ode_start, 'seconds') # values_start = time.time() values = values + odeDelta*detlat # add in ODE updates # values_end = time.time() # print(' Values: ', values_end-values_start, 'seconds') # pde_start = time.time() pdeDelta = step_PDE(values, params, vas_structure, shape_of_img, nonLinear = nonLinear, movablePts=movablePts) #PDE updates # pde_end = time.time() # print(' step PDE: ', pde_end-pde_start, 'seconds') # values_start2 = time.time() values = values + pdeDelta*detlat # add in PDE updates # values_end2 = time.time() # print(' Values2: ', values_end2-values_start2, 'seconds') # values_start3 = time.time() values = values + addSources(params, shape_of_img) # add sources (0 for meat domain) # values_end3 = time.time() # print(' Values3: ', values_end3-values_start3, 'seconds') # update the fitness # get the values of the second protein (the waste) # sum up the ODE updates along the vessels. # total product removed from the environment deltaList = [] for ix,iy,iz in np.ndindex(odeDelta.shape): if iz == 1: #product deltaList.append(odeDelta[(ix,iy,iz)]) # deltaList = odeDelta[vesselCellIds][:,1][0] fitnessList.append(-1*np.sum(deltaList)) # remember odeDeltaList.append(odeDelta) pdeDeltaList.append(pdeDelta) # t_end = time.time() # print(f'TIME iter {t}: ', t_end-t_start, 'seconds') # print(values[(1,1)]) for ix,iy,iz in np.ndindex(values.shape): if iz == 0: try: vas_structure.nutrient_values[ix][iy] = values[(ix,iy,iz)]._value except AttributeError as e: vas_structure.nutrient_values[ix][iy] = values[(ix,iy,iz)] elif iz == 1: try: vas_structure.product_values[ix][iy] = values[(ix,iy,iz)]._value except AttributeError as e: vas_structure.product_values[ix][iy] = values[(ix,iy,iz)] return (np.array(trajectories), fitnessList, np.array(odeDeltaList), np.array(pdeDeltaList))