Пример #1
0
def mod_zedge(composite, mod_id, algorithm, **kwargs):

    zedge_channel, zedge_channel_created = composite.channels.get_or_create(name="-zedge")

    for t in range(composite.series.ts):
        print("step02 | processing mod_zedge t{}/{}...".format(t + 1, composite.series.ts), end="\r")

        zdiff_mask = composite.masks.get(channel__name__contains=kwargs["channel_unique_override"], t=t).load()
        zbf = exposure.rescale_intensity(composite.gons.get(channel__name="-zbf", t=t).load() * 1.0)
        zedge = zbf.copy()

        binary_mask = zdiff_mask > 0
        outside_edge = distance_transform_edt(dilate(edge_image(binary_mask), iterations=4))
        outside_edge = 1.0 - exposure.rescale_intensity(outside_edge * 1.0)
        zedge *= outside_edge * outside_edge

        zedge_gon, zedge_gon_created = composite.gons.get_or_create(
            experiment=composite.experiment, series=composite.series, channel=zedge_channel, t=t
        )
        zedge_gon.set_origin(0, 0, 0, t)
        zedge_gon.set_extent(composite.series.rs, composite.series.cs, 1)

        zedge_gon.array = zedge.copy()
        zedge_gon.save_array(composite.series.experiment.composite_path, composite.templates.get(name="source"))
        zedge_gon.save()
def estimate_coil_sensitivities_SOS(data, trajectory, par):
    """
      Estimate complex coil sensitivities using a sum-of-squares approach.

      Estimate complex coil sensitivities by dividing each coil channel
      with the SoS reconstruciton. A Hann window is used to filter out high
      k-space frequencies.


    Args
    ----
          data (numpy.array):
              complex k-space data
          trajectory (numpy.array):
              trajectory information
          par (dict):
              A python dict containing the necessary information to
              setup the object. Needs to contain the number of slices
              (num_slc), number of scans (num_scans),
              image dimensions (dimX, dimY), number of coils (num_coils),
              sampling pos (num_reads) and read outs (num_proj).
    """
    par["Data"]["phase_map"] = np.zeros(
        (par["Data"]["image_dimension"],
         par["Data"]["image_dimension"]),
        dtype=par["Data"]["DTYPE"])

    FFT = linop.NUFFT(par=par, trajectory=trajectory)

    windowsize = par["Data"]["num_reads"]/10
    window = np.hanning(windowsize)
    window = np.pad(window, int((par["Data"]["num_reads"]-windowsize)/2))

    lowres_data = data*window.T

    coil_images = FFT.adjoint(lowres_data * par["FFT"]["dens_cor"])
    combined_image = np.sqrt(
        1/coil_images.shape[0]
        * np.sum(np.abs(coil_images)**2, 0)
        )

    coils = coil_images/combined_image
    thresh = skimage.filters.threshold_otsu(combined_image)
    mask = combined_image > thresh*0.3
    mask = dilate(mask, iterations=10)

    par["Data"]["coils"] = coils
    par["Data"]["mask"] = mask
    _norm_coils(par)
Пример #3
0
 def find(self, fp, orig, dest):
     orig, dest = orig.get(), dest.get()
     self.field = self._field.copy()
     self.field = dilate(self.field, fp.get_morph_kernel()).astype(int)
     self.field[self.field == 1] = -1
     f = self.field
     if self.acheck(f, dest):
         dest = self._find_nearest_free(f, dest)
     self.orig, self.dest = orig, dest
     self.queue = Queue()
     self.queue.add(orig)
     self.sources = self.amake_2d(None, self.size)
     s = self.sources
     self.aset(f, self.dest, -3)
     self.aset(s, self.orig, -2)
     all_cards = [(0, 1), (1, 0), (-1, 0), (0, -1)]
     all_diags = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
     self.checked = []
     while not self.queue.is_empty():
         current = self.queue.pop()
         if current in self.checked:
             continue
         self.checked += [current]
         try:
             for delta in all_cards:
                 if not self._is_bwds(self.aget(s, current), current,
                                      delta):
                     self._expand_card(current, delta)
             for delta in all_diags:
                 if not self._is_bwds(self.aget(s, current), current,
                                      delta):
                     self._expand_diag(current, delta)
         except FoundPath:
             break
     nodes = self._find_nodes()
     return nodes  #self._reconstruct(nodes)
Пример #4
0
	def create_zedge(self, channel_unique_override):
		zedge_channel, zedge_channel_created = self.channels.get_or_create(name='-zedge')

		for t in range(self.series.ts):
			print('step02 | processing mod_zedge t{}/{}...'.format(t+1, self.series.ts), end='\r')

			zunique_mask = self.masks.get(channel__name__contains=channel_unique_override, t=t).load()
			zbf = exposure.rescale_intensity(self.gons.get(channel__name='-zbf', t=t).load() * 1.0)
			zedge = zbf.copy()

			binary_mask = zunique_mask>0
			outside_edge = distance_transform_edt(dilate(edge_image(binary_mask), iterations=4))
			outside_edge = 1.0 - exposure.rescale_intensity(outside_edge * 1.0)
			zedge *= outside_edge * outside_edge

			zedge_gon, zedge_gon_created = self.gons.get_or_create(experiment=self.experiment, series=self.series, channel=zedge_channel, t=t)
			zedge_gon.set_origin(0,0,0,t)
			zedge_gon.set_extent(self.series.rs, self.series.cs, 1)

			zedge_gon.array = zedge.copy()
			zedge_gon.save_array(self.experiment.composite_path, self.templates.get(name='source'))
			zedge_gon.save()

		return zedge_channel
Пример #5
0
#obtaining this array with nan values in place of True
filledx = x_wolimb.filled()

#y is the  binary representation of x. ma.masked_inside places True/1 for values which are masked
# and False/0 for values which are not, that is values that remain post masking. Therefore applying logical not on it, to get true for values
#that remain after masking and false for values that are masked and finally converting them to 0 and 1
###includes pixels that are off the limb
y = np.logical_not(np.ma.masked_inside(smooth,-sig*std_smooth,sig*std_smooth).mask).astype(np.int)



##to make it work on le_wolimb, a masked array, had to fill the masked values
# and extract the filled array

le1 = erode(y,structure=np.ones((3,3))).astype(x.dtype)
le = dilate(le1,structure=[[True,True,True],[True,True,True],[True,True,True]]).astype(le1.dtype)
le_wolimb = np.ma.array(le,mask=limbmask)
np.ma.set_fill_value(le_wolimb,0.0)

#labeling islands leftover from erode and dilate without off limb pixels
island_lab = measurements.label(le_wolimb.filled())
labels = island_lab[0]


indx,indq = [[] for i in range(2)]
for j in range(dim[1]):
	for i in range(dim[2]):
		dum = q[:,j,i]
		dumdum = filledx[j,i]
		temp = np.dot(dum[0],dum[1:-1])
		np.abs(dum)
Пример #6
0
levels = 1

#plotting masked contours of Q over V
fig3 = plt.figure(figsize=(12, 12))
ax3 = plt.axes()
im3 = plt.imshow(x, vmax=0.01, vmin=-0.01)
#im3 = plt.imshow(combv,vmax=0.08,vmin=-0.08,cmap='gray')
#im3_1 = plt.contour(y.astype(int),levels,origin='lower',colors = 'r')#,cmap=plt.get_cmap('jet'))
fig3.colorbar(im3)
fig3.tight_layout(pad=1.8)
plt.gca().invert_yaxis()
plt.suptitle('Contours of LP over combV')

le1 = erode(y, structure=np.ones((2, 2))).astype(x.dtype)
le = dilate(le1, structure=[[True, True], [True, True]]).astype(le1.dtype)

fig3 = plt.figure(figsize=(12, 12))
ax3 = plt.axes()
im3 = plt.imshow(le, vmax=0.01, vmin=-0.01)
#im3 = plt.imshow(combv,vmax=0.08,vmin=-0.08,cmap='gray')
#im3_1 = plt.contour(y.astype(int),levels,origin='lower',colors = 'r')#,cmap=plt.get_cmap('jet'))
fig3.colorbar(im3)
fig3.tight_layout(pad=1.8)
plt.gca().invert_yaxis()
plt.suptitle('Eroded and dilated')

##plotting masked contours of V over Q
#fig4 = plt.figure(figsize=(12,12))
#ax4 = plt.axes()
## im4 = plt.imshow(xv,vmax=0.08,vmin=-0.08)
Пример #7
0
def grow_mask(ori_image,mask):
    neighboors = dilate(mask)-mask
    seed = ori_image*neighboors
    seed = np.unravel_index(seed.argmax(),seed.shape)
    mask[seed] = 1
    return mask
Пример #8
0
x_wolimb = np.ma.array(x, mask=limbmask)
#changing the filled value in masked array from True to Nan
np.ma.set_fill_value(x_wolimb, np.nan)
#obtaining this array with nan values in place of True
filledx = x_wolimb.filled()

#masking values lesser than 3 sigma
#y = np.ma.greater(smooth,sig*std_smooth).astype(int)
y = np.logical_not(np.ma.equal(smooth, 90000)).astype(int)

##to make it work on le_wolimb, a masked array, had to fill the masked values
# and extract the filled array

le1 = erode(y, structure=np.ones((fac, fac))).astype(x.dtype)

le = dilate(le1, structure=np.ones((fac, fac)).astype(bool)).astype(le1.dtype)

le_wolimb = np.ma.array(le, mask=limbmask)
np.ma.set_fill_value(le_wolimb, 0.0)

#labeling islands leftover from erode and dilate without off limb pixels
island_lab = measurements.label(le_wolimb.filled())
labels = island_lab[0]

lad = np.copy(labels)
for i in range(1, island_lab[1] + 1):
    mk = np.ma.masked_equal(lad, i).mask
    temp = combq[mk]
    lim = 3.5 * np.mean(thres[mk])
    ww = temp[temp > lim]
    if ww.shape[0]:
Пример #9
0
        #print(erodedMitoArray)
        
        mitoPerimeterArray = mitoArray - erodedMitoArray # create a perimeter profile of the mitochondria
        #print(mitoPerimeterArray)
        #plt.imshow(mitoPerimeterArray) 
        #plt.show() # should print out the perimeter of the mitochondria
        
        mitoPerimeterPixelValue = np.sum(mitoPerimeterArray) # total number of pixels in the mitochondrial perimeter
        #print("Total mitochondrial perimeter pixels: ", mitoPerimeterPixelValue)

        ### Generating the array of mitochondrial dilation contours 
        
        dilationFactor = 1  ; # number of pixels to dilate each contour by
        contourNumber  = 100; #number of countours to make
                
        mitoDilated = dilate(mitoArray).astype(int); #the first dilation
        #print(mitoDilated)
        
        dilatedArray = [mitoDilated]; # array to hold subsequent dilations
        
        newDilated = mitoDilated;
        
        for x in range(contourNumber): # this loop will fill the dilatedArray with ### "contourNumber" of dilated contours, each dilated by "dilationFactor"
            newDilated = dilate(newDilated, iterations=dilationFactor).astype(int);
            dilatedArray.append(newDilated);
        
        ### ER condition
    
               
        ERname = mGroup[mGroup['contourType'] == 'ER.tiff'].to_string(header=False,index=False,index_names=False)
        ERname = ERname.split('  ')
Пример #10
0
def AnalyseAndExport(tracking_output,inputs,graphical_output):
    '''
    
    Function
    ----------
    Trace2XLS converts the annotated tracking frames into an excel datasheet
    It's at this stage the various metric extraction functions can be called 
    to extract the desired metric from the annotated tracking masks. 
    
    Use
    ----------
    Call this function as shown below, with the required inputs masks from
    SegNet and tracking_output from TrackNet:
    Trace2XLS(masks,tracking_output)

    Parameters
    ----------
    masks : list
        output of SegNet
    tracking_output : list
        output of TrackNet

    Returns
    -------
    None
    
    '''
    # Unpack inputs
    tracked_frames = tracking_output[0]
    images = [np.squeeze(img) for img in inputs[0]]
    selection = graphical_output[4]
    
    # Transform the selection list into a more useful form
    tracklist = {}
    selection = [sub for sub in selection if len(sub)>0]
    for sub in selection:
        # Get non '+' type submissions
        if '+' not in sub:
            # Non-tuple submissions need auto frame range finding
            sub = eval(sub)
            if type(sub) != tuple:
                ID = sub
                tracklist[ID] = Range(ID,tracked_frames)
            else:
                # Tuple submissions have a range associated with them
                tracklist[sub[0]] = (sub[1],sub[2])
        
        # Get '+' type submissions
        elif '+' in sub:
            # Get all IDs present across the tracked frames
            IDs = np.unique(tracked_frames)
            
            # Get non-tuple submissions
            if ',' not in sub:
                # Extract ID, find ancestors and append with their frame range
                IDx = eval(sub[:-1])
                tracklist[IDx] = Range(IDx,tracked_frames)
                
                for IDy in IDs:
                    if round(IDx) == round(IDy) and IDx != IDy:
                        tracklist[IDy] = Range(IDy,tracked_frames)
                
            else:
                # Tuple submissions have the range associated with them
                sub = sub.split(',')
                IDx = eval(sub[0][:-1])
                tracklist[IDx] = (eval(sub[1]),eval(sub[2]))
                for IDy in IDs:
                    if round(IDx) == round(IDy) and IDx != IDy:
                        tracklist[IDy] = Range(IDy,tracked_frames)
                
    print(tracklist)              
    # Create workbook
    wb = Workbook()
    ws = wb.active
    ws.title = 'Tracking Results'
   
    # Add column titles
    ws['A1'] = 'Track ID'
    ws['B1'] = 'Frame'
    ws['C1'] = 'X center'
    ws['D1'] = 'Y center'
    ws['E1'] = 'Area'
    ws['F1'] = 'Nuclear Intensity'
    ws['G1'] = 'Ring Intensity'
    ws['H1'] = 'CDK2 Activity'

    # Iterate over the nuclei in tracklist, adding their metrics to the sheet
    j = 0
    for ID in tracklist:
        # Get the tracking range
        start,end = tracklist[ID]
        f_range = np.arange(start,end+1)
        
        # Get a list of frames/images with the isolated nuclei
        nuclei = [np.where(i==ID,1,0) for i in tracked_frames[start:end+1]]
        
        # Get the areas of the nucleus
        area = [np.sum(nucleus) for nucleus in nuclei]
        
        # Get the coordinate trace of the nucleus
        coords = [center_of_mass(nucleus) for nucleus in nuclei]
        X_coords = [coord[0] for coord in coords]
        Y_coords = [coord[1] for coord in coords]
        
        # Get the nuclear PCNA intensity from the images
        PCNA = [np.sum(i*nuc)/np.sum(nuc) for i in images[start:end+1]
                for nuc in nuclei]
        
        # For ring intensity, buffer nucleus with one pixel, then dilate +3
        buffer = [dilate(nuc,structure=np.ones((3,3))) for nuc in nuclei]
        dilated = [dilate(nuc,structure=np.ones((3,3)),iterations=4) for nuc in nuclei]
        
        # Generate rings by subtracting buffer from dilation
        rings = []
        for i,(buf,dil) in enumerate(zip(buffer,dilated)):
            rings.append(dil^buf)
            
        # Get ring intensity by multiplying ring by PCNA image
        I_ring = [np.sum(i*ring)/np.sum(ring) for i in images[start:end+1]
                  for ring in rings]
            
        # Add the above information to the spreadsheet
        for i,frame in enumerate(f_range):
            ws.cell(row=i+2+j,column=1,value=ID)
            ws.cell(row=i+2+j,column=2,value=frame)
            ws.cell(row=i+2+j,column=3,value=X_coords[i])
            ws.cell(row=i+2+j,column=4,value=Y_coords[i])
            ws.cell(row=i+2+j,column=5,value=area[i])
            ws.cell(row=i+2+j,column=6,value=PCNA[i])
            ws.cell(row=i+2+j,column=7,value=I_ring[i])
            ws.cell(row=i+2+j,column=8,value=I_ring[i]/PCNA[i])
            
        
        # Update the row offset
        j += end + 2 - start

    wb.save('Data.xlsx')
Пример #11
0
    def handle(self, *args, **options):

        composite = Composite.objects.get(experiment__name="260714", series__name="15")

        # frames
        previous_frame_index = 0
        target_frame_index = 1
        next_frame_index = 2

        # stacks
        previous_gfp_stack = composite.gons.get(t=previous_frame_index, channel__name="0")
        previous_bf_stack = composite.gons.get(t=previous_frame_index, channel__name="1")

        target_gfp_stack = composite.gons.get(t=target_frame_index, channel__name="0")
        target_bf_stack = composite.gons.get(t=target_frame_index, channel__name="1")
        target_zmean_single = composite.gons.get(t=target_frame_index, channel__name="-zmean")
        target_zbf_single = composite.gons.get(t=target_frame_index, channel__name="-zbf")
        target_zmod_single = composite.gons.get(t=target_frame_index, channel__name="-zmod")

        next_gfp_stack = composite.gons.get(t=next_frame_index, channel__name="0")
        next_bf_stack = composite.gons.get(t=next_frame_index, channel__name="1")

        # images
        # previous_gfp = previous_gfp_stack.load()
        # previous_bf = previous_bf_stack.load()

        # target_gfp = target_gfp_stack.load()
        # target_bf = target_bf_stack.load()
        target_zmean = target_zmean_single.load() / 255.0
        target_zbf = target_zbf_single.load() / 255.0
        target_zbf_canny_s3 = canny(target_zbf, sigma=1).astype(float)
        target_zmod = target_zmod_single.load() / 255.0 * (composite.series.zs - 1)

        # next_gfp = next_gfp_stack.load()
        # next_bf = next_bf_stack.load()

        # 1. find maximum from marker
        # marker = composite.markers.get(pk=124)

        track_image = np.zeros(target_zmod.shape)

        class Traveller:
            movement_cost = 1
            directions = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
            spawned = False

            def __init__(self, r, c, direction_index, money, cost_function):
                self.r = r
                self.c = c
                self.direction_index = direction_index
                self.drdc()
                self.money = money
                self.cost_function = cost_function

            def drdc(self):
                self.dr, self.dc = self.directions[self.direction_index]

            def move(self):
                # pick one of five directions in the direction of travel, find the one that costs the least money
                # get array of direction indices relative to current
                new_directions = [
                    (abs(i), (i if i < 8 else i - 8) if i >= 0 else i + 7)
                    for i in range(self.direction_index - 2, self.direction_index + 3)
                ]
                costs = []

                for cost_index, direction in new_directions:
                    dr, dc = self.directions[direction]
                    new_r, new_c = self.r + dr, self.c + dc

                    if (
                        0 <= new_r < composite.series.rs
                        and 0 <= new_c < composite.series.cs
                        and 0 <= self.r < composite.series.rs
                        and 0 <= self.c < composite.series.cs
                        and track_image[new_r, new_c] == 0
                    ):  # not out of bounds
                        # print(new_r, new_c, self.r, self.c, dr, dc)
                        # differences
                        delta_z = int(target_zmod[new_r, new_c]) - int(target_zmod[self.r, self.c])
                        delta_zbf = (target_zbf[new_r, new_c] - target_zbf[self.r, self.c]) / target_zbf[self.r, self.c]
                        delta_zmean = (target_zmean[new_r, new_c] - target_zmean[self.r, self.c]) / target_zmean[
                            self.r, self.c
                        ]

                        abs_zbf = target_zbf[new_r, new_c]
                        abs_zmean = target_zmean[new_r, new_c]

                        cost = self.cost_function(delta_z, delta_zbf, delta_zmean, abs_zbf, abs_zmean)
                        costs.append({"cost": cost, "direction": direction})

                if costs:
                    min_direction = min(costs, key=lambda c: c["cost"])
                    track_image[self.r, self.c] = 1
                    self.r, self.c = tuple(
                        np.array(self.directions[min_direction["direction"]]) + np.array((self.r, self.c))
                    )
                    self.money -= min_direction["cost"]
                else:
                    self.money = 0

        # cost functions
        def test_cost_function(delta_z, delta_zbf, delta_zmean, abs_zbf, abs_zmean):
            cost = 0

            cost += np.abs(delta_z) * 8
            cost += -delta_zbf * 4 if delta_zbf < 0 else 0
            # cost += -delta_zmean * 10.0

            return cost

        def dont_go_through_dark_edges(delta_z, delta_zbf, delta_zmean, abs_zbf, abs_zmean):
            cost = 0

            cost += np.abs(delta_z) * 4
            cost += -delta_zbf * 7 if delta_zbf < 0 else 0

            return cost

        def dont_go_to_another_z(delta_z, delta_zbf, delta_zmean, abs_zbf, abs_zmean):
            cost = 0

            cost += np.abs(delta_z) * 8

            return cost

        # set up segmentation - round 1
        travellers = []
        for marker in composite.markers.filter(track_instance__t=target_frame_index):
            for i in range(10):
                travellers.append(Traveller(marker.r, marker.c, np.random.randint(0, 7), 10, dont_go_to_another_z))

        iterations = 0
        while sum([t.money for t in travellers]) > 0:
            iterations += 1
            for traveller in travellers:
                if traveller.money > 0:
                    traveller.move()
                if traveller.money <= 0:
                    travellers.remove(traveller)
                    del traveller

        # spawn travellers from edge of track_image or round 2
        travellers = []
        track_edge = edge_image(track_image)
        for r, c in zip(*np.where(track_edge > 0)):
            travellers.append(Traveller(r, c, np.random.randint(0, 7), 10, dont_go_through_dark_edges))

        iterations = 0
        while sum([t.money for t in travellers]) > 0:
            iterations += 1
            for traveller in travellers:
                if traveller.money > 0:
                    traveller.move()
                if traveller.money <= 0:
                    travellers.remove(traveller)
                    del traveller

            # print(iterations, len(travellers), sum([t.money for t in travellers]))

        # cut = target_zmean[marker.r-10: marker.r+10, marker.c-10: marker.c+10]
        # plt.imshow(cut)
        # plt.show()

        track_image = dilate(erode(erode(dilate(track_image))))

        display_image = target_zbf.copy()
        display_image[track_image == 1] += 0.3
        plt.imshow(display_image)
        plt.show()