macronucleus / Chromagnon

Image correction software for chromatic shifts in fluorescence microscopic images
GNU General Public License v2.0
21 stars 5 forks source link

Running Chromagnon without GUI #26

Open juliomateoslangerak opened 3 years ago

juliomateoslangerak commented 3 years ago

Hi, Id like to know if it is possible to run chromagnon from python just as a module without GUI and all the overhead of bioformats. I love the application and I would like to implement it into my analysis scripts. That is, import it as a module, run it on a number of numpy arrays using another set of reference arrays or even better a configuration file/object.

Is this possible?

macronucleus commented 3 years ago

Thank you for using Chromagnon. Yes, of course, you can run Chromagnon from python, although a numpy array needs to be saved in a file, preferably with appropriate header information. Assuming you have installed the source code, then you can simply do:

import Chromagnon as ch
ch.aligner.Chromagnon.__init__.__doc__

Alternatively, please take a look at the documentation for class Chromagnon in Chromagnon.aligner.py (line 52 in v0.89). You will find how to run it interactively.

You can also omit installing JDK, javabridge, and python-bioformats, lxml, then it will not use bioformats. You will see warning concerning biofomats not installed, but don't worry. You can disable this warning by changing line 89 (as of v0.89) of Chromagnon.imgio.bioformatsIO.py as follows:

    if 0:#not commonfuncs.main_is_frozen():
        import traceback, warnings
        #traceback.print_exc()
        errs = traceback.format_exc()
        warnings.warn(errs, UserWarning)

To save numpy arrays in files, you can use any other packages or the imgio package of Chromagnon like this:

import numpy as np
from Chromagnon import imgio

outfn = 'test.tif' # the extension should be 'tif' or 'dv'
a = np.empty((2,10,256,256), np.uint16) # assuming the axes of (w,z,y,x)
with imgio.Writer(outfn) as h:
    h.setDim(nx=256, ny=256, nz=10, nt=1, nw=2, dtype=np.uint16, wave=[515,600])
    h.setPixelSize(pz=0.25, py=0.08, px=0.08) # in micron
    for w in range(h.nw):
        h.write3DArr(a[w], t=0, w=w)