2019-07-17 16:56:05 +02:00
""" This script is supposed to be launched from fiji ' s jython interpreter
"""
# # note: fiji's jython doesn't support encoding keyword
2019-09-18 12:34:52 +02:00
# https://imagej.net/Scripting_Headless
2019-09-30 16:18:32 +02:00
#@ String raw_images_root_path
2019-07-17 16:56:05 +02:00
2019-10-01 12:25:15 +02:00
import unittest # unittest2 doesn't exist in fiji
2019-07-17 16:56:05 +02:00
import sys
2020-01-30 16:44:11 +01:00
from lipase . imageengine import IImageEngine , PixelType , Aabb # pylint: disable=import-error
from lipase . imagej . ijimageengine import IJImageEngine , IJImage # pylint: disable=import-error
from lipase . preprocessing import WhiteEstimator , correct_non_uniform_lighting # pylint: disable=import-error
from lipase . maxima_finder import MaximaFinder # pylint: disable=import-error
from lipase . template_matcher import TemplateMatcher # pylint: disable=import-error
from lipase . traps_detector import TrapsDetector # pylint: disable=import-error
from lipase . catalog import ImageCatalog , Sequence # pylint: disable=import-error
2019-07-17 16:56:05 +02:00
2019-10-01 12:25:15 +02:00
class TestLipase ( unittest . TestCase ) :
2019-07-17 16:56:05 +02:00
2019-10-01 12:25:15 +02:00
RAW_IMAGES_ROOT_PATH = raw_images_root_path # eg '/Users/graffy/ownCloud/ipr/lipase/raw-images' pylint: disable=undefined-variable
# we need to know if the test succeeded or not https://stackoverflow.com/questions/4414234/getting-pythons-unittest-results-in-a-teardown-method
# CURRENT_RESULT = None # holds last result object passed to run method
def setUp ( self ) :
print ( " initializing TestLipase instance " )
IImageEngine . set_instance ( IJImageEngine ( ) )
self . catalog = ImageCatalog ( self . RAW_IMAGES_ROOT_PATH )
def tearDown ( self ) :
print ( " uninitializing TestLipase instance " )
self . catalog = None
2020-01-30 16:45:28 +01:00
def test_estimate_white ( self ) :
sequence = self . catalog . sequences [ ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2 ' ]
white_estimator = WhiteEstimator ( open_size = 75 , close_size = 75 , average_size = 75 )
white_estimate = white_estimator . estimate_white ( [ sequence ] , [ ' DM300_327-353_fluo ' ] )
# find_white_reference_image(white_estimate, sequence.get_white())
print ( white_estimate )
IImageEngine . get_instance ( ) . save_as_tiff ( white_estimate , ' ./white_estimate.tiff ' )
# assert False, "hellooooo"
print ( ' end of test_estimate_white ' )
def test_uniform_lighting_correction ( self ) :
non_uniform_sequence = self . catalog . sequences [ ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' ]
uniform_sequence = correct_non_uniform_lighting ( non_uniform_sequence , ' DM300_nofilter_vis ' , white_estimator = WhiteEstimator ( open_size = 75 , close_size = 75 , average_size = 75 ) )
2019-10-08 16:40:39 +02:00
2020-01-29 18:19:01 +01:00
def test_traps_detector ( self ) :
# the typical value of peaks is -500 and the value between peaks is below -2500
threshold = - 1500.0
tolerance = 1500
maxima_finder = MaximaFinder ( threshold , tolerance )
template_matcher = TemplateMatcher ( maxima_finder )
traps_detector = TrapsDetector ( template_matcher )
2020-01-29 12:11:43 +01:00
sequence = self . catalog . sequences [ ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' ]
x_min = 423
x_max = 553
y_min = 419
y_max = 533
trap_aabb = Aabb ( x_min , y_min , x_max , y_max )
2020-01-29 18:19:01 +01:00
matches = traps_detector . compute_traps_mask ( sequence , ' DM300_nofilter_vis ' , trap_aabb )
num_traps = len ( matches )
print ( " number of traps found : %d " % num_traps )
num_expected_traps = 13 # 13 traps are completely visible in the
self . assertAlmostEqual ( len ( matches ) , num_expected_traps , delta = 1.0 )
2019-07-17 16:56:05 +02:00
def run_script ( ) :
2019-10-01 12:25:15 +02:00
# unittest.main() # this would result in : ImportError: No module named __main__
# solution from : https://discourse.mcneel.com/t/using-unittest-in-rhino-python-not-possible/15364
suite = unittest . TestLoader ( ) . loadTestsFromTestCase ( TestLipase )
stream = sys . stdout # by default it's sys.stderr, which doesn't appear in imagej's output
test_result = unittest . TextTestRunner ( stream = stream , verbosity = 2 ) . run ( suite )
print ( ' test_result : %s ' % test_result )
# store summary of the result in a file so that the caller of imagej can detect that this python script failed (imagej seems to always return error code 0, regardless the error returned by the python script it executes : even sys.exit(1) doesn't change this)
with open ( ' /tmp/test_result.txt ' , ' w ' ) as f :
f . write ( ' %d ' % { True : 0 , False : 1 } [ test_result . wasSuccessful ( ) ] )
print ( ' end of run_script ' )
2019-07-17 16:56:05 +02:00
# note : when launched from fiji, __name__ doesn't have the value "__main__", as when launched from python
run_script ( )