Skip to content

Commit

Permalink
Merge branch 'main' into miplib_refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
elainehoml committed Dec 1, 2022
2 parents a4e1513 + dbd442a commit 2d951e7
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 27 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ jobs:
pip install build
- name: Build package
run: python -m build
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
# - name: Publish distribution 📦 to Test PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
# repository_url: https://test.pypi.org/legacy/
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# quoll

[![DOI](https://zenodo.org/badge/507917208.svg)](https://zenodo.org/badge/latestdoi/507917208)
[![napari hub](https://img.shields.io/endpoint?url=https://api.napari-hub.org/shields/napari-quoll)](https://napari-hub.org/plugins/napari-quoll)

Image quality assessment for electron tomography


# Installation

## Users who prefer graphical user interfaces (i.e. Napari)
Find Quoll's Napari plugin (napari-quoll) [here](https://github.com/rosalindfranklininstitute/napari-quoll)

## Users (pip)

1. Create a new conda environment, name it whatever you'd like, but don't forget to activate it
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "quoll"
version = "0.0.1"
version = "0.0.2"
dependencies = [
"jpype1==0.7.5",
"pims==0.4.1",
Expand Down
35 changes: 35 additions & 0 deletions src/quoll/frc/frc_calibration_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2022 Rosalind Franklin Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


import numpy as np


""" When adding calibration functions here, ensure that your function starts
with `calibration_func` or else it will not be discovered in the CLI/Napari"""

def calibration_func_RFI(frequencies: list) -> list:
""" Calibration function to match 1 image FRC to 2 image FRC.
Redone for EM images.
Args:
frequencies (list): x-values in the FRC curve
Returns:
list: frequencies with correction factor applied
"""
correction = 2.066688385682453 + 0.09896293544729941 * np.log(0.08470690336138625 * frequencies)
corrected_frequencies = frequencies / correction
return corrected_frequencies
33 changes: 15 additions & 18 deletions src/quoll/frc/oneimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from miplib.processing import windowing

from quoll.io import reader, tiles
from quoll.frc import frc_calibration_functions as cf


def miplib_oneimg_FRC_calibrated(
Expand Down Expand Up @@ -102,27 +103,18 @@ def miplib_oneimg_FRC_calibrated(
return result


def calibration_func(frequencies: list) -> list:
""" Calibration function to match 1 image FRC to 2 image FRC.
Redone for EM images.
Args:
frequencies (list): x-values in the FRC curve
Returns:
list: frequencies with correction factor applied
"""
correction = 2.066688385682453 + 0.09896293544729941 * np.log(0.08470690336138625 * frequencies)
corrected_frequencies = frequencies / correction
return corrected_frequencies


def calc_frc_res(Image: reader.Image):
def calc_frc_res(
Image: reader.Image,
calibration_func: Optional[Callable] = None
):
"""Calculates one image FRC resolution for quoll Image object
Args:
Image (reader.Image): Quoll.io.reader.Image instance
calibration_func (callable): function that applies a correction factor to the
frequencies of the FRC curve to match the 1 img
FRC to the 2 img FRC. If None, no calibration is
applied.
Raises:
ValueError: if Image is not square
Expand Down Expand Up @@ -150,6 +142,7 @@ def calc_local_frc(
Image: reader.Image,
tile_size: int,
tiles_dir: Optional[str] = None,
calibration_func: Optional[Callable] = None
):
""" Calculates local FRC on a quoll Image
Expand All @@ -161,7 +154,10 @@ def calc_local_frc(
tile_size (int): length of one side of the square tile in pixels
tiles_dir (str): path to directory holding tiles, none by default.
Tiles only saved if tiles_dir is not none.
calibration_func (callable): function that applies a correction factor to the
frequencies of the FRC curve to match the 1 img
FRC to the 2 img FRC. If None, no calibration is
applied.
Returns:
pandas DataFrame: df containing the resolutions in physical units
of all tiles
Expand All @@ -184,6 +180,7 @@ def calc_local_frc(
)
result = calc_frc_res(Img)
resolutions["Resolution"][i] = result.resolution["resolution"]

except:
resolutions["Resolution"][i] = np.nan

Expand Down
42 changes: 41 additions & 1 deletion src/quoll/ui/frc_oneimg_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@

import argparse
import os
from inspect import getmembers

from matplotlib.pyplot import get
from quoll.frc import oneimg
from quoll.io import reader
from quoll.frc import frc_calibration_functions as cf


def get_calibration_functions() -> dict:
""" Gets a dict of available calibration functions in `frc.frc_calibration_functions`
Valid calibration functions all start with "calibration_func"
Returns:
dict: Dict of available calibration functions with keys = strings of the
calibration function and values = the functions themselves
"""
calibration_functions = {None: None}

for member in getmembers(cf):
if member[0].startswith("calibration_func"):
calibration_functions[member[0]] = member[1]

return calibration_functions


def get_args_oneimgFRC():
Expand All @@ -36,6 +57,19 @@ def get_args_oneimgFRC():
default="nm",
help="Physical unit of pixel size, default is nm"
)
parser.add_argument(
"-cf", "--calibration_func",
default=None,
choices=get_calibration_functions().keys(),
help="Function defining correction to match 1 image FRC to \
2 image FRC. This function is instrument specific, so only \
needs to be determined once for each instrument. \
Defaults to None (no calibration applied) \
This function applies a correction factor to return frequencies \
on the 1-img FRC curve to match the 2-img version. \
To set a custom calibration function please add your function to \
frc.frc_calibration_functions.py."
)
parser.add_argument(
"-ts", "--tile_size",
type=int,
Expand Down Expand Up @@ -83,8 +117,13 @@ def oneimgfrc():
unit=args.unit
)

cal_funcs = get_calibration_functions()

if args.tile_size == 0:
result = oneimg.calc_frc_res(Img)
result = oneimg.calc_frc_res(
Img,
calibration_func=cal_funcs[args.calibration_func]
)
print(f"Resolution of {Img.filename} is {result.resolution['resolution']} {Img.unit}")

if args.save_csv is True:
Expand All @@ -96,6 +135,7 @@ def oneimgfrc():
Img,
tile_size=args.tile_size,
tiles_dir=args.tiles_dir,
calibration_func=cal_funcs[args.calibration_func]
)

print(results_df.Resolution.describe())
Expand Down
52 changes: 50 additions & 2 deletions tests/test_frc_oneimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# from miplib.data.io import read as miplibread
from miplib.data.containers.image import Image as miplibImage
from quoll.frc import oneimg
from quoll.frc import frc_calibration_functions as cf
from quoll.io import reader


Expand All @@ -36,7 +37,10 @@ def test_calc_frc_res(self):
Tests that the one-image FRC can be calculated from quoll.io.reader.Image
"""
Img = reader.Image("./data/042.tif", pixel_size=3.3724)
result = oneimg.calc_frc_res(Img)
result = oneimg.calc_frc_res(
Image=Img,
calibration_func=cf.calibration_func_RFI
)
self.assertAlmostEqual(result.resolution["resolution"], 32.1159278)
self.assertIsNotNone(result)

Expand All @@ -58,7 +62,11 @@ def test_calc_local_frc(self):
unit="nm"
)

results_df = oneimg.calc_local_frc(Img, tile_size=512, tiles_dir="./data/tiles2/")
results_df = oneimg.calc_local_frc(
Img,
tile_size=512,
tiles_dir="./data/tiles2/",
calibration_func=cf.calibration_func_RFI)

# Check that tiles are created and saved
self.assertNotEqual(len(os.listdir("./data/tiles2")), 0)
Expand All @@ -79,3 +87,43 @@ def test_calc_local_frc(self):
# check that the resolution heatmap is not empty
self.assertGreater(np.mean(resolution_heatmap), 0)

def test_set_calibration_func(self):
"""
Tests that calibration function can be set to a custom function
In this test, calibration function is set to return the exact same
value, so the result should be equal to uncalibrated.
"""
Img = reader.Image("./data/042.tif")
result_calibrated = oneimg.calc_frc_res(
Image=Img,
calibration_func=lambda x: x # just return original value
)
result_uncalibrated = oneimg.calc_frc_res(
Image=Img,
calibration_func=None
)
self.assertAlmostEqual(
result_calibrated.resolution["resolution"],
result_uncalibrated.resolution["resolution"])
self.assertIsNotNone(result_calibrated.resolution["resolution"])
self.assertIsNotNone(result_uncalibrated.resolution["resolution"])
def test_set_calibration_func(self):
"""
Tests that calibration function can be set to a custom function
In this test, calibration function is set to return the exact same
value, so the result should be equal to uncalibrated.
"""
Img = reader.Image("./data/042.tif")
result_calibrated = oneimg.calc_frc_res(
Image=Img,
calibration_func=lambda x: x # just return original value
)
result_uncalibrated = oneimg.calc_frc_res(
Image=Img,
calibration_func=None
)
self.assertAlmostEqual(
result_calibrated.resolution["resolution"],
result_uncalibrated.resolution["resolution"])
self.assertIsNotNone(result_calibrated.resolution["resolution"])
self.assertIsNotNone(result_uncalibrated.resolution["resolution"])
16 changes: 16 additions & 0 deletions tests/test_frc_oneimg_cli_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ def test_oneimgfrc_tiles_saveplots(self):
os.remove("./data/SerialFIB57_2048x2048_heatmap.tif")
shutil.rmtree("./data/tiles", ignore_errors=True)

@mock.patch(
'sys.argv',
["oneimgfrc",
"./data/042.tif",
"3.3724",
"-cf",
"calibration_func_RFI",
"--save_csv"],
)
def test_oneimgfrc_RFICalibrationFunc(self):
frc_oneimg_ui.oneimgfrc()
self.assertTrue(os.path.exists("./data/042_oneimgfrc.csv"))
with open("./data/042_oneimgfrc.csv", "r") as f:
self.assertAlmostEqual(float(f.read()), 32.11592783389754)
os.remove("./data/042_oneimgfrc.csv")


if __name__ == "__main__":
unittest.main()

0 comments on commit 2d951e7

Please sign in to comment.