Variables generated in Hooks help
Closed this issue · 2 comments
My ultimate goal is to acquire an image, run the image through a hook event, get a value in said hook, return that value and get rid of the image. This seems like an operation that should be straight forward. I have tried different tactics and they hit or miss and I dont know why. For instance the exact code I have below used to work and then I updated to the latest pycromanager and now it doesnt. I dont understand why it would interfere though.
Here are tactics I have tried and didnt work.
- Have the exposure_hook return the level value.
- Define level as an empty list in the expose method and populate it in the hook function.
- Define level as a global variable in the hook function and then return it in the expose function.
Have you tried using pycromanager like this before and if so what is your go to way to code it?
def exposure_hook(self, image, metadata):
global level
level = self.image_percentile_level(image, 0.85)
return
def expose(self, seed_exposure, channel = 'DAPI'):
'''
Runs entire auto focus algorithm in current XY position. Gives back predicted
in focus z position via focus_score method which is the Brenner score.
:param list[int, int, int] z_range: defines range and stepsize with [z start, z end, z step]
:param str channel: channel to autofocus with
:return: z coordinate for in focus plane
:rtype: float
'''
with Acquisition(directory = 'C:/Users/CyCIF PC/Desktop/test_images', name='trash', show_display=False ,image_process_fn=self.exposure_hook) as acq:
# Create some acquisition events here:
event = {'channel': {'group': 'Color', 'config': channel},'exposure': seed_exposure}
acq.acquire(event)
return level
I'm not sure if you can have exposure_hook
take three arguments (i.e. you may want to do it without the self
).
Since the hook runs on a different thread than the main thread, you need some thread safe way of passing the value. Have your expose function wait on a value to end up in the queue before returning, so it wont return until the image processor has added a value into it. This might provide a helpful example
I never considered that! I am so used to putting that in for every function. I will give it a try. Also, I am glad I asked because I had no idea about it running on a different thread. Not exactly sure what to do, but I will read through your link.