예제 #1
0
파일: models.py 프로젝트: apollo-dev/apollo
	def save_array(self, root, template):
		# 1. iterate through planes in bulk
		# 2. for each plane, save plane based on root, template
		# 3. create path with url and add to gon

		if not os.path.exists(root):
			os.makedirs(root)

		file_name = template.rv.format(self.experiment.name, self.series.name, self.channel.name, str_value(self.t, self.series.ts), '{}')
		url = os.path.join(root, file_name)

		if len(self.array.shape)==2:
			imsave(url.format(str_value(self.z, self.series.zs)), self.array)
			self.paths.create(composite=self.composite if self.composite is not None else self.gon.composite, channel=self.channel, template=template, url=url.format(str_value(self.z, self.series.zs)), file_name=file_name.format(str_value(self.z, self.series.zs)), t=self.t, z=self.z)

		else:
			for z in range(self.array.shape[2]):
				plane = self.array[:,:,z].copy()

				imsave(url.format(str_value(z+self.z, self.series.zs)), plane) # z level is offset by that of original gon.
				self.paths.create(composite=self.composite, channel=self.channel, template=template, url=url.format(str_value(self.z, self.series.zs)), file_name=file_name.format(str_value(self.z, self.series.zs)), t=self.t, z=z+self.z)

				# create gons
				gon = self.gons.create(experiment=self.composite.experiment, series=self.composite.series, channel=self.channel, template=template)
				gon.set_origin(self.r, self.c, z, self.t)
				gon.set_extent(self.rs, self.cs, 1)

				gon.array = plane.copy().squeeze()

				gon.save_array(self.experiment.composite_path, template)
				gon.save()
예제 #2
0
def mod_region_test(composite, mod_id, algorithm, **kwargs):

    region_test_path = os.path.join(composite.experiment.video_path, "regions", composite.series.name)
    if not os.path.exists(region_test_path):
        os.makedirs(region_test_path)

    for t in range(composite.series.ts):
        zbf = composite.gons.get(t=t, channel__name="-zbf").load()
        region_mask = composite.masks.get(t=t, channel__name__contains=kwargs["channel_unique_override"]).load()

        mask_edges = mask_edge_image(region_mask)

        zbf_mask_r = zbf.copy()
        zbf_mask_g = zbf.copy()
        zbf_mask_b = zbf.copy()

        # edges
        zbf_mask_r[mask_edges > 0] = 100
        zbf_mask_g[mask_edges > 0] = 100
        zbf_mask_b[mask_edges > 0] = 100

        # region labels
        # prepare drawing
        blank_slate = np.zeros(zbf.shape)
        blank_slate_img = Image.fromarray(blank_slate)
        draw = ImageDraw.Draw(blank_slate_img)
        for unique in [u for u in np.unique(region_mask) if u > 0]:
            if (
                composite.series.region_instances.filter(region_track_instance__t=t, mode_gray_value_id=unique).count()
                > 0
            ):
                region = composite.series.region_instances.get(
                    region_track_instance__t=t, mode_gray_value_id=unique
                ).region

                # get coords (isolate mask, cut to black, use r/c)
                isolated_mask = region_mask == unique
                cut, (r0, c0, rs, cs) = cut_to_black(isolated_mask)
                com_r, com_c = com(isolated_mask)

                draw.text(
                    (com_c + 30, com_r + 30),
                    "{}".format(region.name),
                    font=ImageFont.load_default(),
                    fill="rgb(0,0,255)",
                )

        blank_slate = np.array(blank_slate_img)
        zbf_mask_r[blank_slate > 0] = 0
        zbf_mask_g[blank_slate > 0] = 0
        zbf_mask_b[blank_slate > 0] = 255

        whole = np.dstack([zbf_mask_r, zbf_mask_g, zbf_mask_b])

        imsave(
            join(
                region_test_path,
                "regions_{}_s{}_t{}.tiff".format(
                    composite.experiment.name, composite.series.name, str_value(t, composite.series.ts)
                ),
            ),
            whole,
        )
예제 #3
0
def mod_tile(composite, mod_id, algorithm, **kwargs):

    tile_path = os.path.join(composite.experiment.video_path, "tile", composite.series.name)
    if not os.path.exists(tile_path):
        os.makedirs(tile_path)

    for t in range(composite.series.ts):
        zbf_gon = composite.gons.get(t=t, channel__name="-zbf")
        zcomp_gon = composite.gons.get(t=t, channel__name="-zcomp")
        zmean_gon = composite.gons.get(t=t, channel__name="-zmean")
        mask_mask = composite.masks.get(t=t, channel__name__contains=kwargs["channel_unique_override"])

        zbf = zbf_gon.load()
        zcomp = zcomp_gon.load()
        zmean = zmean_gon.load()
        mask = mask_mask.load()

        mask_outline = mask_edge_image(mask)

        zbf_mask_r = zbf.copy()
        zbf_mask_g = zbf.copy()
        zbf_mask_b = zbf.copy()

        zcomp_mask_r = zcomp.copy()
        zcomp_mask_g = zcomp.copy()
        zcomp_mask_b = zcomp.copy()

        # drawing
        # 1. draw outlines in red channel
        zbf_mask_r[mask_outline > 0] = 255
        zbf_mask_g[mask_outline > 0] = 0
        zbf_mask_b[mask_outline > 0] = 0
        zcomp_mask_r[mask_outline > 0] = 255
        zcomp_mask_g[mask_outline > 0] = 0
        zcomp_mask_b[mask_outline > 0] = 0

        markers = composite.markers.filter(track_instance__t=t, track__cell__isnull=False)
        for marker in markers:
            if hasattr(marker.track_instance, "cell_instance"):
                # 2. draw markers in blue channel
                zbf_mask_r[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 0
                zbf_mask_g[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 0
                zbf_mask_b[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 255
                zcomp_mask_r[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 0
                zcomp_mask_g[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 0
                zcomp_mask_b[marker.r - 2 : marker.r + 3, marker.c - 2 : marker.c + 3] = 255

                # 3. draw text in green channel
                blank_slate = np.zeros(zbf.shape)
                blank_slate_img = Image.fromarray(blank_slate)
                draw = ImageDraw.Draw(blank_slate_img)
                draw.text(
                    (marker.c + 5, marker.r + 5),
                    "{}".format(marker.track.cell.pk),
                    font=ImageFont.load_default(),
                    fill="rgb(0,0,255)",
                )
                blank_slate = np.array(blank_slate_img)

                zbf_mask_r[blank_slate > 0] = 0
                zbf_mask_g[blank_slate > 0] = 255
                zbf_mask_b[blank_slate > 0] = 0
                zcomp_mask_r[blank_slate > 0] = 0
                zcomp_mask_g[blank_slate > 0] = 255
                zcomp_mask_b[blank_slate > 0] = 0

                # regions
        region_mask = composite.masks.get(t=t, channel__name__contains=composite.current_region_unique).load()
        region_mask_edges = mask_edge_image(region_mask)

        zbf_mask_r[region_mask_edges > 0] = 100
        zbf_mask_g[region_mask_edges > 0] = 100
        zbf_mask_b[region_mask_edges > 0] = 100

        zcomp_mask_r[region_mask_edges > 0] = 100
        zcomp_mask_g[region_mask_edges > 0] = 100
        zcomp_mask_b[region_mask_edges > 0] = 100

        # region labels
        # prepare drawing
        blank_slate = np.zeros(zbf.shape)
        blank_slate_img = Image.fromarray(blank_slate)
        draw = ImageDraw.Draw(blank_slate_img)
        for unique in [u for u in np.unique(region_mask) if u > 0]:
            if (
                composite.series.region_instances.filter(region_track_instance__t=t, mode_gray_value_id=unique).count()
                > 0
            ):
                region = composite.series.region_instances.get(
                    region_track_instance__t=t, mode_gray_value_id=unique
                ).region

                # get coords (isolate mask, cut to black, use r/c)
                isolated_mask = region_mask == unique
                cut, (r0, c0, rs, cs) = cut_to_black(isolated_mask)

                draw.text(
                    (c0 + 30, r0 + 30), "{}".format(region.name), font=ImageFont.load_default(), fill="rgb(0,0,255)"
                )

        blank_slate = np.array(blank_slate_img)

        zbf_mask_r[blank_slate > 0] = 0
        zbf_mask_g[blank_slate > 0] = 0
        zbf_mask_b[blank_slate > 0] = 255

        zcomp_mask_r[blank_slate > 0] = 0
        zcomp_mask_g[blank_slate > 0] = 0
        zcomp_mask_b[blank_slate > 0] = 255

        # tile zbf, zbf_mask, zcomp, zcomp_mask
        top_half = np.concatenate((np.dstack([zbf, zbf, zbf]), np.dstack([zbf_mask_r, zbf_mask_g, zbf_mask_b])), axis=0)
        bottom_half = np.concatenate(
            (np.dstack([zmean, zmean, zmean]), np.dstack([zcomp_mask_r, zcomp_mask_g, zcomp_mask_b])), axis=0
        )

        whole = np.concatenate((top_half, bottom_half), axis=1)

        imsave(
            join(
                tile_path,
                "tile_{}_s{}_t{}.tiff".format(
                    composite.experiment.name, composite.series.name, str_value(t, composite.series.ts)
                ),
            ),
            whole,
        )
예제 #4
0
  def save_array(self, root, template):
    # 1. iterate through planes in bulk
    # 2. for each plane, save plane based on root, template
    # 3. create path with url and add to gon

    if not os.path.exists(root):
      os.makedirs(root)

    self.file_name = template.rv.format(self.experiment.name, self.series.name, self.channel.name, str_value(self.t, self.series.ts), str_value(self.z, self.series.zs))
    self.url = os.path.join(root, self.file_name)

    imsave(self.url, self.array)
예제 #5
0
	def create_region_tile(self, channel_unique_override, top_channel='-zbf', side_channel='-zunique', main_channel='-zunique'):
		tile_path = join(self.experiment.video_path, 'regions', self.series.name, channel_unique_override)
		if not exists(tile_path):
			os.makedirs(tile_path)

		for t in range(self.series.ts):
			zbf_gon = self.gons.get(t=t, channel__name=top_channel)
			zcomp_gon = self.gons.get(t=t, channel__name=side_channel)
			zmean_gon = self.gons.get(t=t, channel__name=main_channel)

			zbf = zbf_gon.load()
			zbf = zbf if len(zbf.shape)==2 or (len(zbf.shape)==2 and zbf.shape[2]==2) else np.squeeze(zbf[:,:,0])
			zcomp = zcomp_gon.load()
			zcomp = zcomp if len(zcomp.shape)==2 or (len(zcomp.shape)==2 and zcomp.shape[2]==2) else np.squeeze(zcomp[:,:,0])
			zmean = zmean_gon.load()
			zmean = zmean if len(zmean.shape)==2 or (len(zmean.shape)==2 and zmean.shape[2]==2) else np.squeeze(zmean[:,:,0])

			zbf_mask_r = zbf.copy()
			zbf_mask_g = zbf.copy()
			zbf_mask_b = zbf.copy()

			zcomp_mask_r = zcomp.copy()
			zcomp_mask_g = zcomp.copy()
			zcomp_mask_b = zcomp.copy()

			# draw regions
			for region_instance in self.series.region_instances.filter(region_track_instance__t=t):
				# load mask
				mask = region_instance.masks.all()[0].load()
				# get mask outline
				mask_edge = edge_image(mask)

				# draw outlines in blue channel
				zbf_mask_r[mask_edge>0] = 0
				zbf_mask_g[mask_edge>0] = 0
				zbf_mask_b[mask_edge>0] = 255
				zcomp_mask_r[mask_edge>0] = 0
				zcomp_mask_g[mask_edge>0] = 0
				zcomp_mask_b[mask_edge>0] = 255

			# tile zbf, zbf_mask, zcomp, zcomp_mask
			top_half = np.concatenate((np.dstack([zbf, zbf, zbf]), np.dstack([zbf_mask_r, zbf_mask_g, zbf_mask_b])), axis=0)
			bottom_half = np.concatenate((np.dstack([zmean, zmean, zmean]), np.dstack([zcomp_mask_r, zcomp_mask_g, zcomp_mask_b])), axis=0)
			whole = np.concatenate((top_half, bottom_half), axis=1)

			imsave(join(tile_path, 'tile_{}_s{}_region-{}_t{}.tiff'.format(self.experiment.name, self.series.name, channel_unique_override, str_value(t, self.series.ts))), whole)
예제 #6
0
	def create_tile(self, channel_unique_override, top_channel='-zbf', side_channel='-zunique', main_channel='-zedge', region_list=[]):
		tile_path = join(self.experiment.video_path, 'tile', self.series.name, '{}-{}'.format(dt.datetime.now().strftime('%Y-%m-%d-%H-%M'), channel_unique_override))
		if not exists(tile_path):
			os.makedirs(tile_path)

		for t in range(self.series.ts):
			zbf_gon = self.gons.get(t=t, channel__name=top_channel)
			zcomp_gon = self.gons.get(t=t, channel__name=side_channel)
			zmean_gon = self.gons.get(t=t, channel__name=main_channel)
			mask_mask = self.masks.get(t=t, channel__name__contains=channel_unique_override)

			zbf = zbf_gon.load()
			zbf = zbf if len(zbf.shape)==2 or (len(zbf.shape)==2 and zbf.shape[2]==2) else np.squeeze(zbf[:,:,0])
			zcomp = zcomp_gon.load()
			zcomp = zcomp if len(zcomp.shape)==2 or (len(zcomp.shape)==2 and zcomp.shape[2]==2) else np.squeeze(zcomp[:,:,0])
			zmean = zmean_gon.load()
			zmean = zmean if len(zmean.shape)==2 or (len(zmean.shape)==2 and zmean.shape[2]==2) else np.squeeze(zmean[:,:,0])
			mask = mask_mask.load()

			# remove cells in regions
			if region_list:
				for cell_mask in mask_mask.cell_masks.exclude(region__name__in=region_list):
					mask[mask==cell_mask.gray_value_id] = 0 # delete masks from image if not in regions

			mask_outline = mask_edge_image(mask)

			zbf_mask_r = zbf.copy()
			zbf_mask_g = zbf.copy()
			zbf_mask_b = zbf.copy()

			zcomp_mask_r = zcomp.copy()
			zcomp_mask_g = zcomp.copy()
			zcomp_mask_b = zcomp.copy()

			# drawing
			# 1. draw outlines in red channel
			zbf_mask_r[mask_outline>0] = 255
			zbf_mask_g[mask_outline>0] = 0
			zbf_mask_b[mask_outline>0] = 0
			zcomp_mask_r[mask_outline>0] = 255
			zcomp_mask_g[mask_outline>0] = 0
			zcomp_mask_b[mask_outline>0] = 0

			# draw markers
			markers = self.markers.filter(track_instance__t=t, track__cell__isnull=False)
			for marker in markers:
				if hasattr(marker.track_instance, 'cell_instance'):
					if marker.track_instance.cell_instance.masks.filter(channel__name__contains=channel_unique_override):
						if region_list==[] or (marker.track_instance.cell_instance.masks.get(channel__name__contains=channel_unique_override).region is not None and marker.track_instance.cell_instance.masks.get(channel__name__contains=channel_unique_override).region.name in region_list):
							# 2. draw markers in blue channel
							zbf_mask_r[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 0
							zbf_mask_g[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 0
							zbf_mask_b[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 255
							zcomp_mask_r[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 0
							zcomp_mask_g[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 0
							zcomp_mask_b[marker.r-2:marker.r+3,marker.c-2:marker.c+3] = 255

							# 3. draw text in green channel
							blank_slate = np.zeros(zbf.shape)
							blank_slate_img = Image.fromarray(blank_slate)
							draw = ImageDraw.Draw(blank_slate_img)
							draw.text((marker.c+5, marker.r+5), '{}'.format(marker.track.cell.pk), font=ImageFont.load_default(), fill='rgb(0,0,255)')
							blank_slate = np.array(blank_slate_img)

							zbf_mask_r[blank_slate>0] = 0
							zbf_mask_g[blank_slate>0] = 255
							zbf_mask_b[blank_slate>0] = 0
							zcomp_mask_r[blank_slate>0] = 0
							zcomp_mask_g[blank_slate>0] = 255
							zcomp_mask_b[blank_slate>0] = 0

			# draw regions
			for region_instance in self.series.region_instances.filter(region_track_instance__t=t):
				# load mask
				mask = region_instance.masks.all()[0].load()
				# get mask outline
				mask_edge = edge_image(mask)

				# draw outlines in blue channel
				zbf_mask_r[mask_edge>0] = 0
				zbf_mask_g[mask_edge>0] = 0
				zbf_mask_b[mask_edge>0] = 255
				zcomp_mask_r[mask_edge>0] = 0
				zcomp_mask_g[mask_edge>0] = 0
				zcomp_mask_b[mask_edge>0] = 255

			# tile zbf, zbf_mask, zcomp, zcomp_mask
			top_half = np.concatenate((np.dstack([zbf, zbf, zbf]), np.dstack([zbf_mask_r, zbf_mask_g, zbf_mask_b])), axis=0)
			bottom_half = np.concatenate((np.dstack([zmean, zmean, zmean]), np.dstack([zcomp_mask_r, zcomp_mask_g, zcomp_mask_b])), axis=0)
			whole = np.concatenate((top_half, bottom_half), axis=1)

			imsave(join(tile_path, 'tile_{}_s{}_marker-{}_t{}.tiff'.format(self.experiment.name, self.series.name, channel_unique_override, str_value(t, self.series.ts))), whole)