def test_time_points(): expected = [ { "axes": { "time": 0 }, "min_start_time": 0 }, { "axes": { "time": 1 }, "min_start_time": 10 }, { "axes": { "time": 2 }, "min_start_time": 20 }, { "axes": { "time": 3 }, "min_start_time": 30 }, { "axes": { "time": 4 }, "min_start_time": 40 }, ] assert expected == multi_d_acquisition_events(num_time_points=5, time_interval_s=10)
def test_xyz_positions(): expected = [ {"axes": {"position": 0, "z": 0}, "x": 0, "y": 0, "z": 0}, {"axes": {"position": 1, "z": 0}, "x": 1, "y": -1, "z": 1}, {"axes": {"position": 2, "z": 0}, "x": 2, "y": -2, "z": 2}, {"axes": {"position": 3, "z": 0}, "x": 3, "y": -3, "z": 3}, {"axes": {"position": 4, "z": 0}, "x": 4, "y": -4, "z": 4}, ] assert expected == multi_d_acquisition_events(xyz_positions=xyz)
def test_xy_positions(): expected = [ {"axes": {"position": 0}, "x": 0, "y": 0}, {"axes": {"position": 1}, "x": 1, "y": -1}, {"axes": {"position": 2}, "x": 2, "y": -2}, {"axes": {"position": 3}, "x": 3, "y": -3}, {"axes": {"position": 4}, "x": 4, "y": -4}, ] events = multi_d_acquisition_events(xy_positions=xy) assert events == expected
def acquire_multid(z_range): """ micro-manager data acquisition. Creates acquisition events for z-stack. This example: use multi_d_acquisition because the z-stage is run from micro-manager. Unless hardware triggering is set up in micro-manager, this will be fairly slow: micro-manager does not sweep the z-stage, but acquires plane by plane. """ with Acquisition(directory=None, name=None, show_display=False, image_process_fn=grab_image) as acq: events = multi_d_acquisition_events(z_start=z_range[0], z_end=z_range[1], z_step=(z_range[1] - z_range[0]) / (z_range[2] - 1)) acq.acquire(events)
def test_xy_absolute_z(): expected = [ {"axes": {"position": 0, "z": 0}, "x": 0, "y": 0, "z": -1}, {"axes": {"position": 0, "z": 1}, "x": 0, "y": 0, "z": 0}, {"axes": {"position": 0, "z": 2}, "x": 0, "y": 0, "z": 1}, {"axes": {"position": 1, "z": 0}, "x": 1, "y": -1, "z": -1}, {"axes": {"position": 1, "z": 1}, "x": 1, "y": -1, "z": 0}, {"axes": {"position": 1, "z": 2}, "x": 1, "y": -1, "z": 1}, {"axes": {"position": 2, "z": 0}, "x": 2, "y": -2, "z": -1}, {"axes": {"position": 2, "z": 1}, "x": 2, "y": -2, "z": 0}, {"axes": {"position": 2, "z": 2}, "x": 2, "y": -2, "z": 1}, {"axes": {"position": 3, "z": 0}, "x": 3, "y": -3, "z": -1}, {"axes": {"position": 3, "z": 1}, "x": 3, "y": -3, "z": 0}, {"axes": {"position": 3, "z": 2}, "x": 3, "y": -3, "z": 1}, {"axes": {"position": 4, "z": 0}, "x": 4, "y": -4, "z": -1}, {"axes": {"position": 4, "z": 1}, "x": 4, "y": -4, "z": 0}, {"axes": {"position": 4, "z": 2}, "x": 4, "y": -4, "z": 1}, ] assert expected == multi_d_acquisition_events(xy_positions=xy, z_start=-1, z_end=1, z_step=1)
import numpy as np from pycromanager import multi_d_acquisition_events, Acquisition if __name__ == '__main__': def external_trigger_fn(event): #TODO: send signal to external device here return event with Acquisition(directory='/Users/henrypinkard/megllandump', name='tcz_acq', post_camera_hook_fn=external_trigger_fn) as acq: # Generate the events for a single z-stack events = multi_d_acquisition_events(num_time_points=10, time_interval_s=0, channel_group='channel', channels=['DAPI', 'FITC'], z_start=0, z_end=6, z_step=0.4, order='tcz') acq.acquire(events)
def test_channels(): expected = [ { "axes": { "position": 0 }, "x": 0, "y": 0, "channel": { "group": "your-channel-group", "config": "BF" }, "exposure": 15.5, }, { "axes": { "position": 0 }, "x": 0, "y": 0, "channel": { "group": "your-channel-group", "config": "GFP" }, "exposure": 200, }, { "axes": { "position": 1 }, "x": 1, "y": -1, "channel": { "group": "your-channel-group", "config": "BF" }, "exposure": 15.5, }, { "axes": { "position": 1 }, "x": 1, "y": -1, "channel": { "group": "your-channel-group", "config": "GFP" }, "exposure": 200, }, { "axes": { "position": 2 }, "x": 2, "y": -2, "channel": { "group": "your-channel-group", "config": "BF" }, "exposure": 15.5, }, { "axes": { "position": 2 }, "x": 2, "y": -2, "channel": { "group": "your-channel-group", "config": "GFP" }, "exposure": 200, }, { "axes": { "position": 3 }, "x": 3, "y": -3, "channel": { "group": "your-channel-group", "config": "BF" }, "exposure": 15.5, }, { "axes": { "position": 3 }, "x": 3, "y": -3, "channel": { "group": "your-channel-group", "config": "GFP" }, "exposure": 200, }, { "axes": { "position": 4 }, "x": 4, "y": -4, "channel": { "group": "your-channel-group", "config": "BF" }, "exposure": 15.5, }, { "axes": { "position": 4 }, "x": 4, "y": -4, "channel": { "group": "your-channel-group", "config": "GFP" }, "exposure": 200, }, ] channel_group = "your-channel-group" channels = ["BF", "GFP"] channel_exposures_ms = [15.5, 200] assert expected == multi_d_acquisition_events( xy_positions=xy, channels=channels, channel_group=channel_group, channel_exposures_ms=channel_exposures_ms, )
def test_order(): expected = [ { "axes": { "position": 0, "time": 0, "z": 0 }, "x": 0, "y": 0, "min_start_time": 0, "z": -1, }, { "axes": { "position": 0, "time": 0, "z": 1 }, "x": 0, "y": 0, "min_start_time": 0, "z": 0, }, { "axes": { "position": 0, "time": 1, "z": 0 }, "x": 0, "y": 0, "min_start_time": 10, "z": -1, }, { "axes": { "position": 0, "time": 1, "z": 1 }, "x": 0, "y": 0, "min_start_time": 10, "z": 0, }, { "axes": { "position": 1, "time": 0, "z": 0 }, "x": 1, "y": -1, "min_start_time": 0, "z": -1, }, { "axes": { "position": 1, "time": 0, "z": 1 }, "x": 1, "y": -1, "min_start_time": 0, "z": 0, }, { "axes": { "position": 1, "time": 1, "z": 0 }, "x": 1, "y": -1, "min_start_time": 10, "z": -1, }, { "axes": { "position": 1, "time": 1, "z": 1 }, "x": 1, "y": -1, "min_start_time": 10, "z": 0, }, ] xy_small = xy[:2, :] assert expected == multi_d_acquisition_events( num_time_points=2, time_interval_s=10, xy_positions=xy_small, order="ptz", z_start=-1, z_end=0, z_step=1, )
from pycromanager import Bridge from pycromanager import Acquisition, multi_d_acquisition_events def img_process_fn(image, metadata): # modify the pixels by setting a 100 pixel square at the top left to 0 image[:100, :100] = 0 # propogate the image and metadata to the default viewer and saving classes return image, metadata z_shg_center = 0 if __name__ == '__main__': # this is important, don't forget it with Acquisition(directory='/Users/henrypinkard/megellandump/', name='exp_2_mda', image_process_fn=img_process_fn) as acq: events = multi_d_acquisition_events(z_start=z_shg_center - 20, z_end=z_shg_center + 20, z_step=5) acq.acquire(events) # acq.acquire()
from pycromanager import Acquisition, multi_d_acquisition_events def storage_monitor_fn(axes): dataset = acq.get_dataset() pixels = dataset.read_image(**axes) print(pixels) dire = 'C:\\Users\\henry\\Desktop\\datadump' with Acquisition(directory=dire, name="tcz_acq", debug=False, storage_monitor_callback_fn=None) as acq: events = multi_d_acquisition_events( num_time_points=5, time_interval_s=0, order="tcz", ) dataset = acq.get_dataset() acq.acquire(events)
def img_process_fn(image, metadata): image2 = np.array(image, copy=True) image2 = np.swapaxes(image2, 0, 1) md_2 = copy.deepcopy(metadata) image[250:350, 100:300] = np.random.randint(0, 4999) if metadata['Channel'] == 'DAPI': image[:100, :100] = 0 image2[:100, :100] = 0 else: image[-100:, -100:] = 0 image2[-100:, -100:] = 0 # metadata['Axes']['l'] = 0 md_2['Channel'] = 'A_new_channel' return [(image, metadata), (image2, md_2)] with Acquisition(directory='/Users/henrypinkard/megllandump', name='tcz_acq', image_process_fn=img_process_fn) as acq: # Generate the events for a single z-stack events = multi_d_acquisition_events(num_time_points=10, time_interval_s=0, channel_group='channel', channels=['DAPI', 'FITC'], order='tc') acq.acquire(events)
image2 = np.array(image, copy=True) image2 = np.swapaxes(image2, 0, 1) md_2 = copy.deepcopy(metadata) image[250:350, 100:300] = np.random.randint(0, 4999) if metadata["Channel"] == "DAPI": image[:100, :100] = 0 image2[:100, :100] = 0 else: image[-100:, -100:] = 0 image2[-100:, -100:] = 0 # metadata['Axes']['l'] = 0 md_2["Channel"] = "A_new_channel" return [(image, metadata), (image2, md_2)] with Acquisition(directory="/Users/henrypinkard/megllandump", name="tcz_acq", image_process_fn=img_process_fn) as acq: # Generate the events for a single z-stack events = multi_d_acquisition_events( num_time_points=10, time_interval_s=0, channel_group="Channel", channels=["DAPI", "FITC"], order="tc", ) acq.acquire(events)
from pycromanager import Acquisition, multi_d_acquisition_events import numpy as np def hook_fn(event): return event with Acquisition( directory="/Users/henrypinkard/megllandump", name="acquisition_name", pre_hardware_hook_fn=hook_fn, ) as acq: acq.acquire(multi_d_acquisition_events(10))
# %% Imports from pycromanager import Core from pycromanager import Bridge from pycromanager import Acquisition, multi_d_acquisition_events import numpy as np core = Core() print(core) core.get_version_info() # %% Acquisiton with Acquisition(directory=r'D:\Downloads', name='test') as acq: events = multi_d_acquisition_events(channel_group='Channel', channels=['DAPI', 'FITC'], z_start=0, z_end=6, z_step=0.4, order='tcz') acq.acquire(events) # %% Positions
def acquireImage(channelGroup, channelName, hook): x_array = [] y_array = [] z_array = [] for idx in range(pos_list.get_number_of_positions()): pos = pos_list.get_position(idx) #pos.go_to_position(pos, mmc) x = pos_list.get_position(idx).get(0).x y = pos_list.get_position(idx).get(0).y z = pos_list.get_position(idx).get(1).x x_array.append(x) y_array.append(y) z_array.append(z) x_array = np.array(x_array) y_array = np.array(y_array) z_array = np.array(z_array) with Acquisition(directory=directoryPATH, name=nameofSAVEDFILE, post_hardware_hook_fn=hook, post_camera_hook_fn=hook_fn) as acq: x = np.hstack([x_array[:, None]]) y = np.hstack([y_array[:, None]]) z = np.hstack([z_array[:, None]]) #Generate the events for a single z-stack xyz = np.hstack([x_array[:, None], y_array[:, None], z_array[:, None]]) events = multi_d_acquisition_events(xyz_positions=xyz, channel_group=channelGroup, channels=[channelName]) acq.acquire(events) #acquire a 2 x 1 grid #acq.acquire({'row': 0, 'col': 0}) #acq.acquire({'row': 1, 'col': 0}) stackfolder = "**/*" folder = Path(directoryPATH) foldernames = [] for name in folder.glob('saving_name_*'): print(name.stem) foldernames.append(name.stem) maximum = 1 for file in foldernames: number = int( re.search(nameofSAVEDFILE + "_" + '(\d*)', file).group(1)) # assuming filename is "filexxx.txt" # compare num to previous max, e.g. maximum = number if number > maximum else maximum # set max = 0 before for-loop print(number) highest = nameofSAVEDFILE + "_" + str(maximum) data_path = os.path.join(folder, highest) dataset = Dataset(data_path) #dataset = acq.get_dataset() #dataset = acq.get_dataset() #print(dataset) #data_path=str(directoryPATH/saving_name) dataset = Dataset(data_path) print(dataset.axes) print("data_path", data_path) length = (len(xyz)) dataset_metadata = dataset.read_metadata(channel=0, position=1) print(dataset_metadata) pos = dataset_metadata["Axes"]["position"] print(pos) if (dataset): sizeimg = dataset.read_image(channel=0, position=0) sizeimg = cv2.cvtColor(sizeimg, cv2.COLOR_GRAY2RGB) h, w, c = sizeimg.shape length = int( (sqrt(length) )) #size of the grid (row or column should be same technically) blank_image = np.zeros((h * (math.ceil(math.sqrt(length)) + 2), w * (math.ceil(math.sqrt(length)) + 2), 3), np.uint16) print("image size ", blank_image.shape) pixelsizeinum = dataset_metadata["PixelSizeUm"] #get size of pixel in um print(pixelsizeinum) """ for datarow in range(10): for datacolumn in range(10): metadata = dataset.read_metadata(row=datarow, col=datacolumn) if(metadata["Axes"]["position"]>=0): pos=metadata["Axes"]["position"] #print(pos) img = dataset.read_image(position=pos) img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB) cv2.imshow("test",img) """ xtotaloffset = 0 ytotaloffset = 0 for dataposition in range( len(xyz)): #do range for all positions in micromanager print(dataposition) metadata = dataset.read_metadata(channel=0, position=dataposition) img = dataset.read_image(channel=0, position=dataposition) img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) img = cv2.flip(img, 1) xoffset_um = metadata["XPosition_um_Intended"] yoffset_um = metadata["YPosition_um_Intended"] print("Intended location is : ", xoffset_um, yoffset_um) # cv2.imshow("test",img) # cv2.waitKey(0) xoffset_px = (xoffset_um - dataset.read_metadata( channel=0, position=0)['XPosition_um_Intended']) / pixelsizeinum yoffset_px = (yoffset_um - dataset.read_metadata( channel=0, position=0)['YPosition_um_Intended']) / pixelsizeinum xoffset_px = int(xoffset_px) print("Xoffset ", xoffset_px) #print("img max X ",blank_image.shape[0]) yoffset_px = int(yoffset_px) print("Yoffset ", yoffset_px) #print("img max Y ",blank_image.shape[1]) alpha = 0 blank_image[xoffset_px:xoffset_px + (img.shape[1]), yoffset_px:yoffset_px + (img.shape[0])] = cv2.addWeighted( blank_image[xoffset_px:xoffset_px + (img.shape[1]), yoffset_px:yoffset_px + (img.shape[0])], alpha, img, 1 - alpha, 0) #blank_image[:yoffset_px+img.shape[0], :xoffset_px+img.shape[1]] = img #blank_image = cv2.addWeighted(blank_image[yoffset_px:yoffset_px+img.shape[0], xoffset_px:xoffset_px+img.shape[1]],img) #################### #printout only ignore #################### scale_percent = 5 width = int(blank_image.shape[1] * scale_percent / 100) height = int(blank_image.shape[0] * scale_percent / 100) dim = (width, height) resized = cv2.resize(blank_image, dim, interpolation=cv2.INTER_AREA) ''' #show image winname = "test" cv2.namedWindow(winname) # Create a named window cv2.moveWindow(winname, 1000,1000) # Move it to (40,30) cv2.imshow(winname, resized) cv2.waitKey(0) ''' blank_image = cv2.cvtColor(blank_image, cv2.COLOR_BGR2GRAY) return blank_image, pixelsizeinum
import numpy as np import json import matplotlib.pyplot as plt import multiprocessing import threading import queue from inspect import signature import copy import types from pycromanager import Acquisition, multi_d_acquisition_events, Bridge event_list = multi_d_acquisition_events(num_time_points=10, z_start=0, z_end=10, z_step=0.5) # first = event_list.pop(0) # rest = event_list #Version 1: def hook_fn(event): # if np.random.randint(4) < 2: # return event return event # #Version 2: # def hook_fn(event, bridge, event_queue): # return event
from pycromanager import Acquisition, multi_d_acquisition_events with Acquisition(directory="/Users/henrypinkard/tmp", name="tcz_acq", debug=True) as acq: # Generate the events for a single z-stack events = multi_d_acquisition_events( num_time_points=5, time_interval_s=0, channel_group="Channel", channels=["DAPI", "FITC"], z_start=0, z_end=6, z_step=0.4, order="tcz", ) acq.acquire(events)
max_intensity_projection = np.max(stack, axis=2) projection_metadata = { "Axes": { "time": metadata["Axes"]["time"] }, "Channel": "max_intensity_projection", } # clear list of accumulated images img_process_fn.images = [] # propagate both original image and intensity project back to viewer return [(image, metadata), (max_intensity_projection, projection_metadata)] else: return image, metadata events = multi_d_acquisition_events(num_time_points=10, time_interval_s=2, z_start=0, z_end=10, z_step=1) # read the number of z steps num_z_steps = len(set([event['axes']['z'] for event in events])) save_dir = "/Users/henrypinkard/tmp" save_name = "max_intesnity_acq" with Acquisition(directory=save_dir, name=save_name, image_process_fn=img_process_fn) as acq: acq.acquire(events)