Change one element of the array to two and then delete the original (cell division simulation)

I want to simulate basic cell division using python arrays. I have u, which is an array of arrays defined as:

n=2 #number of elements that can describe each cell N=2 # number of cells u=[np.zeros((n,n)) for i in range(N)] V=2.0 epsilon=2.0 

each u [i] is a cell in the system, and u is the whole system. Then I define some functions that I will use for my cell division algorithm later.

 Mx=np.zeros((n,n)) My=np.zeros((n,n)) for x in range (n): Mx[x]=xn/2 for y in range (n): My[y]=yn/2 

Here I initialize my cells so that they take the values ​​1 or 0:

 for i in range(N): for x in range(n): for y in range(n): if (xn/2)**2+(yn/2)**2<5: u[i][x,y]=1 

After I multiply my cell, I want to check for cells that have grown more, say, volume V

  for i in range (N): if (np.sum([i]>V): theta=random.uniform(0, 2*math.pi) u.append(np.zeros((n,n))) u[-1]=u[i]/2.0* (1+np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) u[i]=u[i]/2.0* (1-np.tanh((math.cos(theta)*Mx+math.sin(theta)*My)/epsilon)) N+=1 

Now I say if my volume is larger than a specific volume V, then I add a cell to the system (adding an array of zeros) and I define my two child cells as

  u[-1]=u[i]/2.0*(1-np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) 

and

  u[i]=u[i]/2.0* (1+np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) 

where the new array u [i] replaces my original one. As an alternative, I tried to add two arrays np.zero ((n, n)) and defined two child cells as

 u[-1] and u[-2] 

then deleting the original parent cell with

 del u[i] 

but that didn't work either. After running this code, I get more arrays in U than I should receive. If N = 2, then I should have N = 4 after cell division.

Ideally, I want my original u array to look something like this:

 u=[array([[0,1], [1,0]]), array([[1,1],[1,0]])] 

and I want to check

 np.sum(u[0])>V 

then I will define two new components in u, namely

 u[-1] and u[i] 

where the new u [i] replaces the original u [i]

as

 np.sum(u[0]) and np.sum(u[1]) are both > V 

then my new u array should look something like this:

 u=[array([[1,1], [1,0]]), array([[1,1], [1,0]]), array([[1,1], [1,0]]), array([[1,1], [1,0]])] 

(these values ​​in my array are just approximate values. The actual value depends on my function

  u[-1]=u[i]/2.0*(1+np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) u[i]=u[i]/2.0*(1- np.tanh((math.cos(theta)*Mx+math.sin(theta)*My)/epsilon)) 
+5
source share
2 answers

I noticed that the code does not start.

I think this is not true:

if (np.sum([i]>V): to change, I think, like if np.sum([i])>V:

Missing one parenthesis in:

 u[-1]=u[i]/2.0*(1-np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) u[i]=u[i]/2.0*(1+np.tanh((math.cos(theta)*Mx+math.sin(theta)*My/epsilon)) 

Missing V and epsilon values. At least V is required to understand the intended output form.

Please write an example of the expected output form in u to understand what you want to do. Maybe better with n = 2 , so the size of u smaller.

UPDATE: With the new information, I think I better understand the task. Check to see if the code below is what you expected.

Make sure that constant is the correct equation.

Make sure if (np.sum(u[i])>V): is the correct instruction.

 import numpy as np import math import random n=2 #number of elements that can describe each cell N=2 # number of cells u=[np.zeros((n,n)) for i in range(N)] V=2.0 epsilon=2.0 Mx=np.zeros((n,n)) My=np.zeros((n,n)) for x in range (n): Mx[x]=xn/2 for y in range (n): My[y]=yn/2 for i in range(N): for x in range(n): for y in range(n): if (xn/2)**2+(yn/2)**2<5: u[i][x,y]=1 lenu = len(u) for i in range (lenu): if (np.sum(u[i])>V): print(u[i], i, u, len(u)) theta=random.uniform(0, 2*math.pi) constant = np.tanh((math.cos(theta)*Mx+math.sin(theta)*My)/epsilon) u_end = u[i]/2.0*(1 + constant) u[i] = u[i]/2.0*(1 - constant) u.append(u_end) N+=1 
+1
source

To isolate the problem, take your mind off it. NumPy and the exact condition for mitosis are not related to how you replace one element in a sequence with two. It is also inappropriate how these new elements will be created.

So, suppose our cells are represented by alphanumeric strings. Those that are completely filled are at the point at which they would fall into cell division. We can simulate the process as follows:

 from itertools import chain cells = ['a', 'B', 'C', 'D', 'e', 'F'] # Consonants grow quicker than vowels ;-) # mitosis: cells = list( chain.from_iterable( [ '{}1'.format(cell.lower()), '{}2'.format(cell.lower()), ] if cell == cell.upper() else [cell] for cell in cells ) ) # Now, cells == ['a', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2', 'e', 'f1', 'f2'] 

(See the documentation for itertools.chain.from_iterable . I feed its generator expressions . Generator expressions work similar to list recognition , but do not explicitly build the list.)

We can make this more clear by pulling out some functions:

 from itertools import chain def is_ready_for_mitosis(cell): return cell == cell.upper() def perform_mitosis(cell): first_new_cell = '{}1'.format(cell.lower()) second_new_cell = '{}2'.format(cell.lower()) return first_new_cell, second_new_cell def perform_mitosis_if_ready(cell): if is_ready_for_mitosis(cell): return perform_mitosis(cell) # (else) return (cell,) cells = ['a', 'B', 'C', 'D', 'e', 'F'] # mitosis: cells = list(chain.from_iterable(perform_mitosis_if_ready(cell) for cell in cells)) # Now, cells == ['a', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2', 'e', 'f1', 'f2'] 

Now all the simplifications that I have made are encapsulated in the function of the first. Thus, to use the third function and update procedure based on itertools for your case, simply repeat the implementation of the first two functions as you like.

0
source

Source: https://habr.com/ru/post/1243775/


All Articles