Skip to content

Commit

Permalink
exporting png brightfield images done
Browse files Browse the repository at this point in the history
  • Loading branch information
Heerpa committed May 13, 2024
1 parent 8f2f7b9 commit 57ef00e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.10

WORKDIR /app

COPY pyproject.toml poetry.lock ./

RUN pip install poetry
RUN poetry install --no-dev

COPY . .

CMD ["python", "-m", "unittest"]
46 changes: 46 additions & 0 deletions picasso_workflow/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,52 @@ def _plot_locs_vs_frame(self, filename):
plt.close(fig)
return results

@module_decorator
def export_brightfield(self, i, parameters, results):
"""Opens a single-plane tiff image and saves it to png with
contrast adjustment.
Args:
i : int
the module index in the protocol
parameters : dict
necessary items:
filepath : str or list of str
the tiff file(s) to load. The converted file(s) will
have the same name, but with .png extension
optional items:
min_quantile : float, default: 0
the quantile below which pixels are shown black
max_quantile : float, default: 1
the quantile above which pixels are shown white
results : dict
the results dict, created by the module_decorator
Returns:
parameters : dict
as input, potentially changed values, for consistency
results : dict
the analysis results
filename : the png file
"""
fps_in = parameters["filepath"]
if isinstance(fps_in, str):
fps_in = [fps_in]
fps_out = []
for fp in fps_in:
mov, _ = io.load_movie(fp)
frame = mov[0]
fn = os.path.split(fp)[1]
fn = os.path.splitext(fn)[0] + ".png"
fp = os.path.join(results["folder"], fn)
fps_out.append(fp)
min_quantile = parameters.get("min_quantile", 0)
max_quantile = parameters.get("max_quantile", 1)
process_brightfield.save_frame(
fp, frame, min_quantile, max_quantile
)
results["filenames"] = fps_out
return parameters, results

@module_decorator
def undrift_rcc(self, i, parameters, results):
"""Undrifts localized data using redundant cross correlation.
Expand Down
28 changes: 28 additions & 0 deletions picasso_workflow/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ def localize(self, i, parameters, results):
os.path.split(res["filename"])[1],
)

def export_brightfield(self, i, parameters, results):
"""Describes the export_brightfield section of picasso
Args:
"""
logger.debug("Reporting export_brightfield.")
text = """
<ac:layout><ac:layout-section ac:type="single"><ac:layout-cell>
<p><strong>Exporting Brightfield</strong></p>
<ul>
"""
text += f"""
<li>Start Time: {results['start time']}</li>
<li>Duration: {results["duration"] // 60:.0f} min
{(results["duration"] % 60):.02f} s</li></ul>
</ac:layout-cell></ac:layout-section></ac:layout>
"""
self.ci.update_page_content(
self.report_page_name, self.report_page_id, text
)

for fp in results.get("filepaths"):
self.ci.upload_attachment(self.report_page_id, fp)
self.ci.update_page_content_with_image_attachment(
self.report_page_name,
self.report_page_id,
os.path.split(fp)[1],
)

def undrift_rcc(self, i, parameters, results):
"""Describes the Localize section of picasso
Args:
Expand Down
21 changes: 21 additions & 0 deletions picasso_workflow/process_brightfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import logging
from moviepy.editor import ImageSequenceClip
from imageio import imsave # package is dependency of moviepy
import numpy as np

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -62,3 +63,23 @@ def save_movie(fname, movie, min_quantile=0, max_quantile=1, fps=1):
# Create movie file
clip = ImageSequenceClip(adjusted_images, fps=fps)
clip.write_videofile(fname, verbose=False) # , codec='mpeg4')


def save_frame(pathname, frame, min_quantile=0, max_quantile=1):
"""Save a grayscale frame to png
Args:
pathname : str
the file name to save
frame : 2D np array (x,y)
the frame to save
min_quantile : float, default: 0
the quantile below which pixels are shown black
max_quantile : float, default: 1
the quantile above which pixels are shown white
"""
logger.debug(frame.shape)
adjusted_frame = adjust_contrast(
frame, min_quantile, max_quantile
) # [..., np.newaxis]
logger.debug(adjusted_frame.shape)
imsave(pathname, adjusted_frame)
13 changes: 13 additions & 0 deletions picasso_workflow/tests/test_analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ def localize(self, mock_get_spots, mock_fit_spot, mock_locs_from_fits):

shutil.rmtree(os.path.join(self.results_folder, "00_localize"))

@patch("picasso_workflow.analyse.io.load_movie")
def export_brightfield(self, mock_load):
frame = np.random.randint(0, 1000, size=(1, 32, 32))
mock_load.return_value = (frame, [])

parameters = {"filepath": "myfp.ome.tiff"}

parameters, results = self.ap.export_brightfield(0, parameters)

shutil.rmtree(
os.path.join(self.results_folder, "00_export_brightfield")
)

@patch("picasso_workflow.analyse.postprocess.undrift")
def undrift_rcc(self, mock_undrift_rcc):
nspots = 5
Expand Down
16 changes: 16 additions & 0 deletions picasso_workflow/tests/test_confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ def localize(self):
)
self.cr.ci.delete_page(pgid)

# @unittest.skip("")
def export_brightfield(self):
parameters = {}
results = {
"start time": "now",
"duration": 16.4,
"filepaths": ["myfp.png"],
}
self.cr.export_brightfield(0, parameters, results)

# clean up
pgid, pgtitle = self.cr.ci.get_page_properties(
self.cr.report_page_name
)
self.cr.ci.delete_page(pgid)

# @unittest.skip("")
def undrift_rcc(self):
parameters = {
Expand Down
6 changes: 6 additions & 0 deletions picasso_workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def localize(self):
"""Localizes Spots previously identified."""
pass

@abc.abstractmethod
def export_brightfield(self):
"""Opens a single-plane tiff image and saves it to png with
contrast adjustment."""
pass

@abc.abstractmethod
def undrift_rcc(self):
"""Undrifts localized data using redundant cross correlation."""
Expand Down

0 comments on commit 57ef00e

Please sign in to comment.