def isInitialized():
  """Check if Ilastik is useable
  
  Returns:
    bool: True if Ilastik is installed and useable by *ClearMap*
  """
  
  return ilastik.isInitialized();
def isInitialized():
  """Check if Ilastik is useable
  
  Returns:
    bool: True if Ilastik is installed and useable by *ClearMap*
  """
  
  return ilastik.isInitialized();
def classifyCells(img, classifyCellsParameter = None, classifier = None, classindex = 0, save = None, verbose = False,
                  detectCellShapeParameter = None,
                  subStack = None, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    The routine assumes that the first class is identifying the cells.
        
    Arguments:    
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *classindex* (int)                class index considered to be cells
            *save*       (str or None)        save the detected cell pixel to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        tuple: centers of the cells, intensity measurments
        
    Note:    
        The routine could be potentially refined to make use of background 
        detection in ilastik
    """
    
    classifier = getParameter(classifyCellsParameter, "classifier", classifier);
    classindex = getParameter(classifyCellsParameter, "classindex", classindex);
    save       = getParameter(classifyCellsParameter, "save", save);   
    verbose    = getParameter(classifyCellsParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik cell detection:', classifier = classifier, classindex = classindex, save = save);        

    timer = Timer(); 

    ilastik.isInitialized();
    
    #remove background
    #img = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image / assume class 1 are the cells !  
    timer = Timer();  
    
    imgmax = ilastik.classifyPixel(classifier, img);
    #print imgmax.shape
    #max probability gives final class, last axis is class axis
    imgmax = numpy.argmax(imgmax, axis = -1);
    
    if save:
        writeSubStack(save, numpy.asarray(imgmax, dtype = 'float32'), subStack = subStack)    

    # class 0 is used as cells 
    imgmax = imgmax == classindex; # class 1 is used as cells 
    imgshape, nlab = sm.label(imgmax);
    
    if verbose > 1:
        plotTiling(imgmax);
        
    #center of maxima
    centers = findCenterOfMaxima(img, imgmax, imgshape, verbose = verbose, out = out, **parameter);
    
    #intensity of cells
    #cintensity = findIntensity(img, centers, verbose = verbose, out = out, **parameter);

    #intensity of cells in filtered image
    #cintensity2 = findIntensity(img, centers, verbose = verbose, out = out, **parameter);
    
    #if verbose:
    #    out.write(timer.elapsedTime(head = 'Ilastik cell detection') + '\n');    
    
    #return ( centers, numpy.vstack((cintensity, cintensity2)).transpose() );   
    #return ( centers, cintensity ); 
    
    
    #cell size detection
    #detectCellShapeParameter = getParameter(classifyCellsParameter, "detectCellShapeParameter", detectCellShapeParameter);
    #cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold", None);
    
    #if not cellShapeThreshold is None:
        
    # cell shape via watershed
    #imgshape = detectCellShape(img, centers, detectCellShapeParameter = detectCellShapeParameter, verbose = verbose, out = out, **parameter);
    
    #size of cells        
    csize = findCellSize(imgshape, maxLabel = centers.shape[0], out = out, **parameter);
    
    #intensity of cells
    cintensity = findCellIntensity(img, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in background image
    #cintensity2 = findCellIntensity(img2, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in dog filtered image
    #if dogSize is None:
    #    cintensity3 = cintensity2;
    #else:
    #    cintensity3 = findCellIntensity(img3, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik Cell Detection') + '\n');
    
    #remove cell;s of size 0
    idz = csize > 0;
                   
    #return ( centers[idz], numpy.vstack((cintensity[idz], cintensity3[idz], cintensity2[idz], csize[idz])).transpose());        
    return ( centers[idz], numpy.vstack((cintensity[idz], csize[idz])).transpose() ); 
def classifyCells(img, classifyCellsParameter = None, classifier = None, classindex = 0, save = None, verbose = False,
                  detectCellShapeParameter = None,
                  subStack = None, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    The routine assumes that the first class is identifying the cells.
        
    Arguments:    
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *classindex* (int)                class index considered to be cells
            *save*       (str or None)        save the detected cell pixel to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        tuple: centers of the cells, intensity measurments
        
    Note:    
        The routine could be potentially refined to make use of background 
        detection in ilastik
    """
    
    classifier = getParameter(classifyCellsParameter, "classifier", classifier);
    classindex = getParameter(classifyCellsParameter, "classindex", classindex);
    save       = getParameter(classifyCellsParameter, "save", save);   
    verbose    = getParameter(classifyCellsParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik cell detection:', classifier = classifier, classindex = classindex, save = save);        

    timer = Timer(); 

    ilastik.isInitialized();
    
    #remove background
    #img = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image / assume class 1 are the cells !  
    timer = Timer();  
    
    imgmax = ilastik.classifyPixel(classifier, img);
    #print imgmax.shape
    #max probability gives final class, last axis is class axis
    imgmax = numpy.argmax(imgmax, axis = -1);
    
    if save:
        writeSubStack(save, numpy.asarray(imgmax, dtype = 'float32'), subStack = subStack)    

    # class 0 is used as cells 
    imgmax = imgmax == classindex; # class 1 is used as cells 
    imgshape, nlab = sm.label(imgmax);
    
    if verbose > 1:
        plotTiling(imgmax);
        
    #center of maxima
    centers = findCenterOfMaxima(img, imgmax, imgshape, verbose = verbose, out = out, **parameter);
    
    #intensity of cells
    #cintensity = findIntensity(img, centers, verbose = verbose, out = out, **parameter);

    #intensity of cells in filtered image
    #cintensity2 = findIntensity(img, centers, verbose = verbose, out = out, **parameter);
    
    #if verbose:
    #    out.write(timer.elapsedTime(head = 'Ilastik cell detection') + '\n');    
    
    #return ( centers, numpy.vstack((cintensity, cintensity2)).transpose() );   
    #return ( centers, cintensity ); 
    
    
    #cell size detection
    #detectCellShapeParameter = getParameter(classifyCellsParameter, "detectCellShapeParameter", detectCellShapeParameter);
    #cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold", None);
    
    #if not cellShapeThreshold is None:
        
    # cell shape via watershed
    #imgshape = detectCellShape(img, centers, detectCellShapeParameter = detectCellShapeParameter, verbose = verbose, out = out, **parameter);
    
    #size of cells        
    csize = findCellSize(imgshape, maxLabel = centers.shape[0], out = out, **parameter);
    
    #intensity of cells
    cintensity = findCellIntensity(img, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in background image
    #cintensity2 = findCellIntensity(img2, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in dog filtered image
    #if dogSize is None:
    #    cintensity3 = cintensity2;
    #else:
    #    cintensity3 = findCellIntensity(img3, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik Cell Detection') + '\n');
    
    #remove cell;s of size 0
    idz = csize > 0;
                   
    #return ( centers[idz], numpy.vstack((cintensity[idz], cintensity3[idz], cintensity2[idz], csize[idz])).transpose());        
    return ( centers[idz], numpy.vstack((cintensity[idz], csize[idz])).transpose() );