Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems processing some images #182

Closed
ns-rse opened this issue Jun 16, 2022 · 1 comment
Closed

Problems processing some images #182

ns-rse opened this issue Jun 16, 2022 · 1 comment

Comments

@ns-rse
Copy link
Collaborator

ns-rse commented Jun 16, 2022

Description

Some of the images @derollins tried to processed failed to complete. One such sample is 20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021.spm

To Reproduce

Running off of dev branch (commit f671bf4)

  • Install the dev branch in a virtual environment.
  • Place the file in its own directory.
  • Copy config/example.py to this directory and modify the config file so that base_dir: ./
  • `run_topostats -c ./example.py

Expected behavior

Would expect it to run to completion.

Output

It doesn't, it crashes with...

[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] Threshold       : 7.502686318963248
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Applying Gaussian filter (mode : nearest; Gaussian blur (nm) : 2.0).
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Deriving mask.
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Created boolean image
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Tidying borders
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Labelling Regions
Minimum Grain Size : None
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021] : Region properties calculated
/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/core/fromnumeric.py:3474: RuntimeWarning: Mean of empty slice.
  return _methods._mean(a, axis=axis, dtype=dtype,
/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/core/_methods.py:189: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Processing : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Extracting filename from : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Filename : 20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Loading image from : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
Processing images from /home/neil/work/projects/topostats/TopoStats/tmp/problem_images, results are under output:   0%|                                      | 0/21 [00:00<?, ?it/s]
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] [20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020] : Loaded image from : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Extracting filename from : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
[Thu, 16 Jun 2022 15:14:03] [INFO    ] [topostats] Loading image from : /home/neil/work/projects/topostats/TopoStats/tmp/problem_images/20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00020.spm
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/home/neil/work/projects/topostats/TopoStats/topostats/run_topostats.py", line 161, in process_scan
    grains.find_grains()
  File "/home/neil/work/projects/topostats/TopoStats/topostats/grains.py", line 199, in find_grains
    self.calc_minimum_grain_size(image=self.images["labelled_regions"])
  File "/home/neil/work/projects/topostats/TopoStats/topostats/grains.py", line 132, in calc_minimum_grain_size
    1.5 * (np.quantile(grain_areas, 0.75) - np.quantile(grain_areas, 0.25))
  File "<__array_function__ internals>", line 180, in quantile
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4371, in quantile
    return _quantile_unchecked(
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4383, in _quantile_unchecked
    r, k = _ureduce(a,
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/lib/function_base.py", line 3702, in _ureduce
    r = func(a, **kwargs)
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4552, in _quantile_ureduce_func
    result = _quantile(arr,
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/lib/function_base.py", line 4663, in _quantile
    previous = np.take(arr, previous_indexes, axis=DATA_AXIS)
  File "<__array_function__ internals>", line 180, in take
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/core/fromnumeric.py", line 190, in take
    return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
  File "/home/neil/.virtualenvs/topostats/lib/python3.10/site-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
IndexError: cannot do a non-empty take from an empty axes.
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/neil/.virtualenvs/topostats/bin/run_topostats", line 33, in <module>
    sys.exit(load_entry_point('topostats', 'console_scripts', 'run_topostats')())
  File "/home/neil/work/projects/topostats/TopoStats/topostats/run_topostats.py", line 272, in main
    for img, result in pool.imap_unordered(processing_function, img_files):
  File "/usr/lib/python3.10/multiprocessing/pool.py", line 870, in next
    raise value
IndexError: cannot do a non-empty take from an empty axes.

If applicable please include the output error, this can be a copy and paste of the output (preferable) or a screenshot.
If applicable, add screenshots to help explain your problem.
Output files can be attached to this bug report.

(Not required if using Docker) Your computer configuration (please complete the following information):

 ❱ python --version
Python 3.10.5
 ❱ git show --oneline -s
3d25d04 (HEAD -> dev, origin/dev, ns-rse/problem-images) Merge pull request #180 from AFM-SPM/ns-rse/195-raw-height-data
 ❱ pip freeze
alabaster==0.7.12
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
astroid==2.11.2
asttokens==2.0.5
attrs==21.4.0
Babel==2.10.1
backcall==0.2.0
beautifulsoup4==4.11.1
black==22.3.0
bleach==4.1.0
bokeh==2.4.2
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.12
click==8.1.3
colorcet==3.0.0
coverage==6.3.2
cycler==0.11.0
debugpy==1.6.0
decorator==5.1.1
defusedxml==0.7.1
dill==0.3.4
docstring-to-markdown==0.10
docutils==0.17.1
entrypoints==0.4
executing==0.8.3
fastjsonschema==2.15.3
fonttools==4.31.2
holoviews==1.14.8
hvplot==0.7.3
idna==3.3
imageio==2.16.1
imagesize==1.3.0
iniconfig==1.1.1
ipykernel==6.13.0
ipython==8.2.0
ipython-genutils==0.2.0
ipywidgets==7.7.0
isort==5.10.1
jedi==0.18.1
jedi-language-server==0.35.1
Jinja2==3.1.1
joblib==1.1.0
jsonschema==4.4.0
jupyter==1.0.0
jupyter-client==7.3.0
jupyter-console==6.4.3
jupyter-core==4.10.0
jupyterlab-pygments==0.2.2
jupyterlab-widgets==1.1.0
kiwisolver==1.4.2
lazy-object-proxy==1.7.1
Markdown==3.3.6
markdown-it-py==2.1.0
MarkupSafe==2.1.1
matplotlib==3.5.1
matplotlib-inline==0.1.3
mccabe==0.7.0
mdit-py-plugins==0.3.0
mdurl==0.1.1
mistune==0.8.4
mypy-extensions==0.4.3
myst-parser==0.17.2
nbclient==0.6.2
nbconvert==6.5.0
nbformat==5.3.0
nest-asyncio==1.5.5
networkx==2.7.1
notebook==6.4.11
numpy==1.22.3
packaging==21.3
pandas==1.4.1
pandocfilters==1.5.0
panel==0.12.7
param==1.12.0
parso==0.8.3
pathspec==0.9.0
patsy==0.5.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.0.1
platformdirs==2.5.1
pluggy==1.0.0
prometheus-client==0.14.1
prompt-toolkit==3.0.28
psutil==5.9.0
ptyprocess==0.7.0
pure-eval==0.2.2
py==1.11.0
pycparser==2.21
pyct==0.4.8
pydantic==1.8.2
pygls==0.11.3
Pygments==2.11.2
pylint==2.13.4
pyparsing==3.0.7
pyrsistent==0.18.1
-e git+ssh://[email protected]/ns-rse/pySPM.git@69ecd202b615088948b5637516531d912d846e48#egg=pySPM
pytest==7.1.1
pytest-cov==3.0.0
pytest-mpl==0.15.1
python-dateutil==2.8.2
pytz==2022.1
pyviz-comms==2.2.0
PyWavelets==1.3.0
PyYAML==6.0
pyzmq==22.3.0
qtconsole==5.3.0
QtPy==2.1.0
requests==2.27.1
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.6
scikit-image==0.19.2
scikit-learn==1.0.2
scipy==1.8.0
seaborn==0.11.2
Send2Trash==1.8.0
six==1.16.0
snowballstemmer==2.2.0
soupsieve==2.3.2.post1
Sphinx==4.5.0
sphinx-markdown-tables==0.0.15
sphinx-rtd-theme==1.0.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-mermaid==0.7.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
spym==0.5.0
stack-data==0.2.0
statsmodels==0.13.2
tabulate==0.8.9
terminado==0.13.3
threadpoolctl==3.1.0
tifffile==2022.3.25
tinycss2==1.1.1
tomli==2.0.1
-e git+ssh://[email protected]/AFM-SPM/TopoStats.git@3d25d04bd7c8d0a84ecc8415beb5277096069990#egg=topostats
tornado==6.1
tqdm==4.63.1
traitlets==5.1.1
typeguard==2.13.3
typing_extensions==4.1.1
urllib3==1.26.9
wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.6.0
wrapt==1.14.0
xarray==2022.3.0
yapf==0.32.0

Additional context

The problem arises when calling the self.calc_minimum_grain_size(image=self.images["labelled_regions"]) to calculate the minimum grain size. The method looks like this...

    def calc_minimum_grain_size(self, image: np.array) -> float:
        """Calculate the minimum grain size.

        Very small objects are first removed via thresholding before calculating the lower extreme.

        Parameters
        ----------
        image : np.array
            Numpy array of regions of interest after labelling.

        Returns
        -------
        float
            Threshold for minimum grain size.
        """
        self.get_region_properties()
        grain_areas = np.array([grain.area for grain in self.region_properties])
        grain_areas = grain_areas[grain_areas > threshold(grain_areas, method=self.threshold_method)]
        self.minimum_grain_size = np.median(grain_areas) - (
            1.5 * (np.quantile(grain_areas, 0.75) - np.quantile(grain_areas, 0.25))
        )
        LOGGER.info(f"Minimum Grain Size calculated : {self.minimum_grain_size}")

grain_areas is initially a single value of 234, that is there is a single grain in self.region_properties with that area.

Then after thresholding which currently uses the otsu method to exclude small areas of dirt/noise the value of grain_areas is None since there are no grains that exceed the threshold calculated by this method.

In turn the self.minimum_grain_size can not be calculated and no more calculations can be done.

Images from the Filter stage can be found here (Restricted Access).

@derollins has advised that a better image is 20220601_NDP52_FL_10ng_PLO_HEPES_20mM_NaCl_50mM.0_00021.spm and so I will investigate further but these sorts of errors will need handling sanely and cleanly and shouldn't prevent other images in a given directory from being processed.

Judicious use of try: ... except: throughout at stages where failures can occur would be sensible to allow processing of all images to be attempted.

@ns-rse
Copy link
Collaborator Author

ns-rse commented Jul 14, 2022

#183 resolved this problem.

@ns-rse ns-rse closed this as completed Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant