Skip to content

Commit

Permalink
More Works with BaseWorkFromImage
Browse files Browse the repository at this point in the history
  • Loading branch information
paugier committed Feb 22, 2024
1 parent a084c61 commit 1726c54
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 155 deletions.
84 changes: 8 additions & 76 deletions src/fluidimage/topologies/bos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from fluidimage.data_objects.piv import ArrayCoupleBOS, get_name_bos
from fluidimage.topologies import TopologyBase, prepare_path_dir_result
from fluidimage.util import imread, logger
from fluidimage.works.piv import WorkPIV
from fluidimage.works.bos import WorkBOS

from . import image2image

Expand Down Expand Up @@ -71,43 +71,8 @@ def create_default_params(cls):
"""
params = ParamContainer(tag="params")

params._set_child("images", attribs={"path": "", "str_subset": None})

params.images._set_doc(
"""
Parameters indicating the input image set.
path : str, {''}
String indicating the input images (can be a full path towards an image
file or a string given to `glob`).
str_subset : None
String indicating as a Python slicing how to select images from the serie of
images on the disk. If None, no selection so all images are going to be
processed.
"""
)

params._set_attrib("reference", 0)

params._set_doc(
"""
reference : str or int, {0}
Reference file (from which the displacements will be computed). Can be an
absolute file path, a file name or the index in the list of files found
from the parameters in ``params.images``.
"""
)

super()._add_default_params_saving(params)
params.saving.postfix = "bos"

WorkPIV._complete_params_with_default(params)
WorkBOS._complete_params_with_default(params)

params._set_internal_attr(
"_value_text",
Expand All @@ -128,9 +93,9 @@ def create_default_params(cls):
def __init__(self, params, logging_level="info", nb_max_workers=None):
self.params = params

self.serie = SerieOfArraysFromFiles(
params.images.path, params.images.str_subset
)
self.main_work = WorkBOS(params)
self.serie = self.main_work.serie
self.path_reference = self.main_work.path_reference

path_dir = Path(self.serie.path_dir)
path_dir_result, self.how_saving = prepare_path_dir_result(
Expand All @@ -140,31 +105,6 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
self.path_dir_result = path_dir_result
self.path_dir_src = Path(path_dir)

if not isinstance(params.reference, int):
reference = Path(params.reference).expanduser()
else:
reference = params.reference

if isinstance(reference, int):
names = self.serie.get_name_arrays()
names = sorted(names)
path_reference = path_dir / names[reference]

else:
reference = Path(reference)
if reference.is_file():
path_reference = reference
else:
path_reference = path_dir_result / reference
if not path_reference.is_file():
raise ValueError(
"Bad value of params.reference:" + path_reference
)

self.name_reference = path_reference.name
self.path_reference = path_reference
self.image_reference = imread(path_reference)

super().__init__(
path_dir_result=path_dir_result,
logging_level=logging_level,
Expand Down Expand Up @@ -194,8 +134,8 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
)

if params.preproc.im2im is not None:
im2im_func = image2image.TopologyImage2Image.init_im2im(
self, params.preproc
im2im_func = image2image.get_im2im_function_from_params(
params.preproc
)

self.add_work(
Expand Down Expand Up @@ -227,15 +167,7 @@ def save_bos_object(self, obj):

def calcul(self, tuple_image_path):
"""Compute a BOS field"""
image, name = tuple_image_path
array_couple = ArrayCoupleBOS(
names=(self.name_reference, name),
arrays=(self.image_reference, image),
params_mask=self.params.mask,
serie=self.serie,
paths=[self.path_reference, self.path_dir_src / name],
)
return WorkPIV(self.params).calcul(array_couple)
return self.main_work.calcul(tuple_image_path)

def fill_queue_paths(self, input_queue, output_queue):
"""Fill the first queue (paths)"""
Expand Down
75 changes: 17 additions & 58 deletions src/fluidimage/topologies/image2image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Topology for image2image preprocessing (:mod:`fluidimage.topologies.image2image`)
====================================================================================
"""Topology for image2image preprocessing
=========================================
.. autoclass:: TopologyImage2Image
:members:
Expand All @@ -13,17 +13,26 @@

from fluiddyn.io.image import imsave

from fluidimage import ParamContainer, SerieOfArraysFromFiles
from fluidimage import ParamContainer
from fluidimage.image2image import (
complete_im2im_params_with_default,
init_im2im_function,
)
from fluidimage.topologies import prepare_path_dir_result
from fluidimage.util import imread, logger
from fluidimage.works.image2image import WorkImage2Image

from .base import TopologyBase


def get_im2im_function_from_params(params_im2im):
"""Helper for other topologies"""
_, im2im_func = init_im2im_function(
im2im=params_im2im.im2im, args_init=params_im2im.args_init
)
return im2im_func


class TopologyImage2Image(TopologyBase):
"""Topology for images processing with a user-defined function
Expand Down Expand Up @@ -68,49 +77,8 @@ def create_default_params(cls):
"""
params = ParamContainer(tag="params")

complete_im2im_params_with_default(params)

params._set_child("images", attribs={"path": "", "str_subset": None})

params.images._set_doc(
"""
Parameters indicating the input image set.
path : str, {''}
String indicating the input images (can be a full path towards an image
file or a string given to `glob`).
str_subset : None
String indicating as a Python slicing how to select images from the serie of
images on the disk. If None, no selection so all images are going to be
processed.
"""
)

params._set_child(
"saving", attribs={"path": None, "how": "ask", "postfix": "pre"}
)

params.saving._set_doc(
"""Saving of the results.
path : None or str
Path of the directory where the data will be saved. If None, the path is
obtained from the input path and the parameter `postfix`.
how : str {'ask'}
'ask', 'new_dir', 'complete' or 'recompute'.
postfix : str
Postfix from which the output file is computed.
"""
)
super()._add_default_params_saving(params)
WorkImage2Image._complete_params_with_default(params)

params._set_internal_attr(
"_value_text",
Expand All @@ -131,9 +99,9 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
if params.im2im is None:
raise ValueError("params.im2im has to be set.")

self.serie = SerieOfArraysFromFiles(
params.images.path, params.images.str_subset
)
self.work = WorkImage2Image(params)
self.serie = self.work.serie
im2im_func = self.work.im2im_func

path_dir = self.serie.path_dir
path_dir_result, self.how_saving = prepare_path_dir_result(
Expand Down Expand Up @@ -168,8 +136,6 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
kind="io",
)

im2im_func = self.init_im2im(params)

self.add_work(
"im2im",
im2im_func,
Expand All @@ -181,13 +147,6 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
"save", self.save_image, input_queue=self.queue_results, kind="io"
)

def init_im2im(self, params_im2im):
"""Initialize the im2im function"""
self.im2im_obj, self.im2im_func = init_im2im_function(
im2im=params_im2im.im2im, args_init=params_im2im.args_init
)
return self.im2im_func

def imread(self, path):
"""Read an image"""
array = imread(path)
Expand Down
4 changes: 2 additions & 2 deletions src/fluidimage/topologies/piv.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
)

if params.preproc.im2im is not None:
im2im_func = image2image.TopologyImage2Image.init_im2im(
self, params.preproc
im2im_func = image2image.get_im2im_function_from_params(
params.preproc
)

self.add_work(
Expand Down
5 changes: 1 addition & 4 deletions src/fluidimage/topologies/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def create_default_params(cls, backend="python"):
)

params._set_child("im2im")

complete_im2im_params_with_default(params.im2im)

return params
Expand Down Expand Up @@ -243,9 +242,7 @@ def __init__(
)

if params.im2im.im2im is not None:
im2im_func = image2image.TopologyImage2Image.init_im2im(
self, params.im2im
)
im2im_func = image2image.get_im2im_function_from_params(params.im2im)

self.add_work(
"image2image",
Expand Down
4 changes: 2 additions & 2 deletions src/fluidimage/topologies/surface_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ def __init__(self, params, logging_level="info", nb_max_workers=None):
)

if params.preproc.im2im is not None:
im2im_func = image2image.TopologyImage2Image.init_im2im(
self, params.preproc
im2im_func = image2image.get_im2im_function_from_params(
params.preproc
)

self.add_work(
Expand Down
57 changes: 56 additions & 1 deletion src/fluidimage/works/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

from copy import deepcopy

from fluiddyn.util.serieofarrays import SeriesOfArrays
from fluiddyn.util.paramcontainer import ParamContainer
from fluiddyn.util.serieofarrays import SerieOfArraysFromFiles, SeriesOfArrays

from .. import imread

Expand All @@ -30,6 +31,13 @@ class BaseWork:
def __init__(self, params=None):
self.params = params

@classmethod
def create_default_params(cls):
"Create an object containing the default parameters (class method)."
params = ParamContainer(tag="params")
cls._complete_params_with_default(params)
return params


class BaseWorkFromSerie(BaseWork):
"""Base class for work taking as argument a SerieOfArraysFromFiles"""
Expand Down Expand Up @@ -116,6 +124,53 @@ def process_1_serie(self, index_serie: int = 0):
return self.calcul(self.get_serie(index_serie))


class BaseWorkFromImage(BaseWork):
"""Base class for work taking as argument an image"""

serie: SerieOfArraysFromFiles

@classmethod
def _complete_params_with_default(cls, params):

params._set_child("images", attribs={"path": "", "str_subset": None})

params.images._set_doc(
"""
Parameters indicating the input image set.
path : str, {''}
String indicating the input images (can be a full path towards an image
file or a string given to `glob`).
str_subset : None
String indicating as a Python slicing how to select images from the serie of
images on the disk. If None, no selection so all images will be processed.
"""
)

def _init_serie(self):
p_images = self.params.images
self.serie = SerieOfArraysFromFiles(p_images.path, p_images.str_subset)
return self.serie

def get_image_name(self, index_image: int = 0):
"""Get a serie as defined by params.series"""
if not hasattr(self, "_serie"):
self._init_serie()

return (
self.serie.get_array_from_index(index_image),
self.serie.get_name_arrays()[index_image],
)

def process_1_image(self, index_serie: int = 0):
"""Process one serie and return the result"""
return self.calcul(self.get_image_name(index_serie))


def load_image(path):
im = imread(path)
return im
Loading

0 comments on commit 1726c54

Please sign in to comment.