Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aarmey committed Feb 28, 2024
1 parent 6c28062 commit 610b102
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 133 deletions.
2 changes: 1 addition & 1 deletion lineage/Analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def Results(tHMMobj: tHMM, LL: float) -> dict[str, Any]:
results_dict["total_number_of_lineages"] = len(tHMMobj.X)
results_dict["LL"] = LL
results_dict["total_number_of_cells"] = sum(
[len(lineage.output_lineage) for lineage in tHMMobj.X]
[len(lineage) for lineage in tHMMobj.X]
)

true_states_by_lineage = [
Expand Down
23 changes: 0 additions & 23 deletions lineage/CellVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@ def __init__(self, parent: Optional["CellVar"], state: Optional[int] = None):
self.right = None
self.obs = None

def divide(self, T: np.ndarray, rng=None):
"""
Member function that performs division of a cell.
Equivalent to adding another timestep in a Markov process.
:param T: The array containing the likelihood of a cell switching states.
"""
rng = np.random.default_rng(rng)
# Checking that the inputs are of the right shape
assert T.shape[0] == T.shape[1]

# roll a loaded die according to the row in the transtion matrix
left_state, right_state = rng.choice(T.shape[0], size=2, p=T[self.state, :])
self.left = CellVar(state=left_state, parent=self)
self.right = CellVar(state=right_state, parent=self)

return self.left, self.right

def isLeafBecauseTerminal(self) -> bool:
"""
Returns true when a cell is a leaf with no children.
Expand All @@ -79,12 +62,6 @@ def isLeaf(self) -> bool:
# otherwise, it itself is observed and at least one of its daughters is observed
return False

def isRootParent(self) -> bool:
"""
Returns true if this cell is the first cell in a lineage.
"""
return self.parent is None


@dataclass(init=True, repr=True, eq=True, order=True)
class Time:
Expand Down
5 changes: 1 addition & 4 deletions lineage/HMM/E_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ def get_beta(
) # MSD of the respective lineage
ELMSD = EL * MSD

cIDXs = np.arange(first_leaf)
cIDXs = np.flip(cIDXs)

for pii in cIDXs:
for pii in range(first_leaf - 1, -1, -1):
ch_ii = np.array([pii * 2 + 1, pii * 2 + 2])
ratt = (beta[ch_ii, :] / MSD_array[ch_ii, :]) @ T.T
fac1 = np.prod(ratt, axis=0) * ELMSD[pii, :]
Expand Down
19 changes: 12 additions & 7 deletions lineage/LineageTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ def rand_init(
) # roll the dice and yield the state for the first cell
first_cell = CellVar(parent=None, state=first_state) # create first cell
full_lineage = [first_cell] # instantiate lineage with first cell
pIDX = 0

for cell in full_lineage: # letting the first cell proliferate
if cell.isLeaf(): # if the cell has no daughters...
# make daughters by dividing and assigning states
full_lineage.extend(cell.divide(T, rng=rng))
# fill in the cells
while len(full_lineage) < desired_num_cells: # letting the first cell proliferate
parent = full_lineage[pIDX]
states = rng.choice(T.shape[0], size=2, p=T[parent.state, :])

if len(full_lineage) >= desired_num_cells:
break
full_lineage.append(CellVar(parent=parent, state=states[0]))
full_lineage.append(CellVar(parent=parent, state=states[1]))
parent.left = full_lineage[-2]
parent.right = full_lineage[-1]

pIDX += 1

# Assign observations
for i_state in range(pi.size):
Expand Down Expand Up @@ -140,7 +145,7 @@ def get_Emission_Likelihoods(X: list[LineageTree], E: list) -> list:
EL = []
ii = 0
for lineageObj in X: # for each lineage in our Population
nl = len(lineageObj.output_lineage) # getting the lineage length
nl = len(lineageObj) # getting the lineage length
EL.append(ELstack[ii : (ii + nl), :]) # append the EL_array for each lineage

ii += nl
Expand Down
4 changes: 2 additions & 2 deletions lineage/figures/figure18.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
tmp_lineage = LineageTree.rand_init(
pi, T, E, desired_num_cells, censor_condition=3, desired_experiment_time=96
)
while len(tmp_lineage.output_lineage) < 3:
while len(tmp_lineage) < 3:
tmp_lineage = LineageTree.rand_init(
pi,
T,
Expand All @@ -59,7 +59,7 @@
desired_experiment_time=96,
)
population.append(tmp_lineage)
nn += len(tmp_lineage.output_lineage)
nn += len(tmp_lineage)

# Adding populations into a holder for analysing
list_of_populations.append(population)
Expand Down
5 changes: 3 additions & 2 deletions lineage/states/StateDistributionGaPhs.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ def assign_times(self, full_lineage: list):
This is used in the creation of LineageTrees
"""
# traversing the cells by generation
for cell in full_lineage:
if cell.isRootParent():
for ii, cell in enumerate(full_lineage):
# handle root separately
if ii == 0:
cell.time = Time(0, cell.obs[2] + cell.obs[3])
cell.time.transition_time = 0 + cell.obs[2]
else:
Expand Down
5 changes: 3 additions & 2 deletions lineage/states/StateDistributionGamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def assign_times(self, full_lineage: list[CellVar]):
This is used in the creation of LineageTrees.
"""
# traversing the cells by generation
for cell in full_lineage:
if cell.isRootParent():
for ii, cell in enumerate(full_lineage):
# if root
if ii == 0:
cell.time = Time(0, cell.obs[1])
else:
cell.time = Time(
Expand Down
8 changes: 4 additions & 4 deletions lineage/states/stateCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ def basic_censor(cells: list):
"""
Censors a cell if the cell's parent is censored.
"""
for cell in cells:
if not cell.isRootParent():
if not cell.parent.observed:
cell.observed = False
for cell in cells[1:]:
if not cell.parent.observed:
cell.observed = False


def bern_estimator(bern_obs: np.ndarray, gammas: np.ndarray):
Expand Down Expand Up @@ -114,6 +113,7 @@ def gamma_estimator(
method="SLSQP",
constraints=linc,
)
print(res.nit)

assert res.success
return np.exp(res.x)
84 changes: 0 additions & 84 deletions lineage/tests/test_CellVar.py

This file was deleted.

8 changes: 4 additions & 4 deletions lineage/tests/test_StateDistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def test_censor(self):
as expected.
"""
for lin in self.population:
for cell in lin.output_lineage:
if not cell.isRootParent:
if not cell.parent.observed:
self.assertFalse(cell.observed)
# Skip root parent
for cell in lin.output_lineage[1:]:
if not cell.parent.observed:
self.assertFalse(cell.observed)


@pytest.mark.parametrize("dist", [StateDistribution, StateDistPhase])
Expand Down

0 comments on commit 610b102

Please sign in to comment.