from numpy import reshape, max, median
from numpy.linalg import solve, inv


def get_spill(text):
    """Extracts spillover matrix from FCS text entry.

    Returns (spillover matrix new_spill, column headers)
    """
    spill = text.split(',')
    n = int(spill[0])
    markers = spill[1:(n + 1)]
    markers = [item.strip().replace('\n', '') for item in markers]
    items = [item.strip().replace('\n', '') for item in spill[n + 1:]]
    new_spill = reshape(map(float, items), (n, n))
    return new_spill, markers


def compensate(npy, spill, indices=None, comp=False, scale=False):
    """
    Compensate numpy data 'npy' given spillover matrix 'spill'
    and markers to compensate
    If markers are not given, will look for fcm.annotate.text['SPILL']
    """
    data = npy.copy()
    if indices:
        data = data[:, indices]

    if scale and not comp:
        spill = spill / max(spill)
    if comp:
        spill = inv(spill)

    return solve(spill.T, data.T).T


def gen_spill_matrix(npy, stain_index):
    """
    Generates spillover matrix for one FCS file (presumably from beads)
    npy: the numpy array of the bead data
    stain_index: index of the stained channel
    """

    # get the median for all unstained columns, zero for stained index
    spill = list()
    for column, i in enumerate(npy.T):
        if i == stain_index:
            spill.append(0.0)
            continue
        else:
            spill.append(median(column))
        
    return spill
@@ -23,13 +23,8 @@
 ape,
- max,
  med
@@ -61,13 +61,8 @@
 olve
-, inv
 %0A%0A%0Ad
@@ -546,33 +546,8 @@
 None
-, comp=False, scale=False
 ):%0A 
@@ -630,16 +630,23 @@
 d marker
+ indice
 s to com
@@ -657,79 +657,8 @@
 ate%0A
-    If markers are not given, will look for fcm.annotate.text%5B'SPILL'%5D%0A
     
@@ -690,23 +690,32 @@
 %0A    if 
+len(
 indices
+) %3E 0
 :%0A      
@@ -745,111 +745,8 @@
 s%5D%0A%0A
-    if scale and not comp:%0A        spill = spill / max(spill)%0A    if comp:%0A        spill = inv(spill)%0A%0A
     

remove some unused imports, make sure indices are integers