Browse Source

added script

main
Holger Frey 4 years ago
parent
commit
d8473aa693
  1. 4
      pyproject.toml
  2. 104
      sensospot_images/__init__.py

4
pyproject.toml

@ -26,6 +26,7 @@ classifiers = [
requires = [ requires = [
"sensospot_data", "sensospot_data",
"Pillow >= 8.0", "Pillow >= 8.0",
"click",
] ]
requires-python = ">=3.7" requires-python = ">=3.7"
@ -47,6 +48,9 @@ dev = [
"pre-commit", "pre-commit",
] ]
[tool.flit.scripts]
sensospot_images = "sensospot_images:run"
[tool.black] [tool.black]
line-length = 79 line-length = 79
py37 = true py37 = true

104
sensospot_images/__init__.py

@ -5,18 +5,24 @@ Creating nice spot images from scans
__version__ = "0.0.1" __version__ = "0.0.1"
from pathlib import Path
import sys import sys
from pathlib import Path
from datetime import datetime
import click
from sensospot_data import parse_file from sensospot_data import parse_file
from sensospot_data.parameters import _search_measurement_params_file from sensospot_data.parameters import _search_measurement_params_file
from .images import recalculate, get_position, annotate_image, load_array_image, crop from .images import (
crop,
recalculate,
get_position,
annotate_image,
load_array_image,
)
from .parameters import get_spot_parameters, get_array_parameters from .parameters import get_spot_parameters, get_array_parameters
def calulate_pixel_size(data_frame, array_definition): def calulate_pixel_size(data_frame, array_definition):
first = get_position(data_frame.iloc[0], actual=False) first = get_position(data_frame.iloc[0], actual=False)
last = get_position(data_frame.iloc[-1], actual=False) last = get_position(data_frame.iloc[-1], actual=False)
@ -47,7 +53,7 @@ def get_example_data_path(input_dir):
tif_files = input_path.glob("*.tif") tif_files = input_path.glob("*.tif")
example_tif = next(tif_files) example_tif = next(tif_files)
return example_tif.with_suffix(".csv") return example_tif.with_suffix(".csv")
def get_filename_prefix(input_dir): def get_filename_prefix(input_dir):
file_path = get_example_data_path(input_dir) file_path = get_example_data_path(input_dir)
@ -60,17 +66,18 @@ def retrieve_spot_parameters(input_dir, scale):
parameters_path = _search_measurement_params_file(input_dir) parameters_path = _search_measurement_params_file(input_dir)
if parameters_path is None: if parameters_path is None:
sys.exit(f"Could not find parameter files in {input_dir}") sys.exit(f"Could not find parameter files in {input_dir}")
array_parameters = get_array_parameters(parameters_path) array_parameters = get_array_parameters(parameters_path)
spot_parameters = get_spot_parameters(parameters_path, array_parameters) spot_parameters = get_spot_parameters(parameters_path, array_parameters)
example_data_path = get_example_data_path(input_dir) example_data_path = get_example_data_path(input_dir)
example_data = parse_file(example_data_path) example_data = parse_file(example_data_path)
pixel_size = calulate_pixel_size(example_data, array_parameters) pixel_size = calulate_pixel_size(example_data, array_parameters)
return recalculate(spot_parameters, scale / pixel_size) return recalculate(spot_parameters, scale / pixel_size)
def search_image_files(input_dir, wells, exposures): def search_image_files(input_dir, wells, exposures):
input_path = Path(input_dir) input_path = Path(input_dir)
prefix = get_filename_prefix(input_path) prefix = get_filename_prefix(input_path)
@ -78,6 +85,7 @@ def search_image_files(input_dir, wells, exposures):
pattern = tmp_pattern.replace("***", "*").replace("**", "*") pattern = tmp_pattern.replace("***", "*").replace("**", "*")
return input_path.glob(pattern) return input_path.glob(pattern)
def create_file_map(input_dir, wells, exposures): def create_file_map(input_dir, wells, exposures):
file_map = {} file_map = {}
for tif_path in search_image_files(input_dir, wells, exposures): for tif_path in search_image_files(input_dir, wells, exposures):
@ -89,19 +97,31 @@ def create_file_map(input_dir, wells, exposures):
file_map[csv_path].append(tif_path) file_map[csv_path].append(tif_path)
return file_map return file_map
def process_image(image_file, spot_parameters, spot_data, scale): def process_image(image_file, spot_parameters, spot_data, scale):
img = load_array_image(image_file, scale=scale) img = load_array_image(image_file, scale=scale)
annotate_image(img, spot_parameters, spot_data, scale) annotate_image(img, spot_parameters, spot_data, scale)
return img return img
def create_crops(output_path, img, image_path, spot_parameters, array_data, scale):
def create_crops(
output_path, img, image_path, spot_parameters, array_data, scale
):
base_name = image_path.stem base_name = image_path.stem
for index, spot_data in array_data.iterrows(): for index, spot_data in array_data.iterrows():
cropped_img = crop(img, spot_parameters, spot_data, scale) cropped_img = crop(img, spot_parameters, spot_data, scale)
new_path = output_path / f"{base_name}_{index + 1:03}.tif" new_path = output_path / f"{base_name}_{index + 1:03}.tif"
cropped_img.save(new_path) cropped_img.save(new_path)
def process(input_dir, output_dir, scale=3, wells="*", exposures="*", add_single_spots=False):
def process(
input_dir,
output_dir,
wells="*",
exposures="*",
scale=3,
add_single_spots=False,
):
spot_parameters = retrieve_spot_parameters(input_dir, scale) spot_parameters = retrieve_spot_parameters(input_dir, scale)
file_map = create_file_map(input_dir, wells, exposures) file_map = create_file_map(input_dir, wells, exposures)
output_path = Path(output_dir) output_path = Path(output_dir)
@ -114,5 +134,65 @@ def process(input_dir, output_dir, scale=3, wells="*", exposures="*", add_single
img = process_image(image_path, spot_parameters, array_data, scale) img = process_image(image_path, spot_parameters, array_data, scale)
img.save(output_path / image_path.name) img.save(output_path / image_path.name)
if add_single_spots: if add_single_spots:
create_crops(output_path, img, image_path, spot_parameters, array_data, scale) create_crops(
output_path,
img,
image_path,
spot_parameters,
array_data,
scale,
)
@click.command()
@click.argument(
"source",
type=click.Path(
exists=True,
file_okay=False,
dir_okay=True,
readable=True,
writable=True,
),
)
@click.option(
"-o",
"--output",
default=None,
help="Output directory name, defaults to folder on desktop",
)
@click.option(
"-w",
"--wells",
default="*",
help="restrict to this wells, * = all",
)
@click.option(
"-e",
"--Exposures",
default="*",
help="restrict to this exposure ids, * = all",
)
@click.option(
"-s",
"--scale",
type=int,
default=3,
help="scale-up of images",
)
@click.option(
"--spots",
default=False,
is_flag=True,
help="include cropped images of spots",
)
def run(source, output=None, wells="*", exposures="*", scale=3, spots=False):
if output is None or not Path(output).is_dir():
default = Path.home() / "Desktop"
if not default.is_dir():
default = Path.home()
now = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
output = default / now
output.mkdir(exist_ok=True)
process(source, output, wells, exposures, scale, spots)

Loading…
Cancel
Save