manuel-munoz-aguirre / PyHIST

A pipeline to segment tissue from the background in histological images
GNU General Public License v3.0
56 stars 13 forks source link

Segment #29

Open swvanderlaan opened 2 years ago

swvanderlaan commented 2 years ago

Hi,

Great package/script.

I am running your code like so:

python3 ~/git/PyHIST/pyhist.py --content-threshold 0.05 --patch-size 64 --output-downsample 16 --info "verbose" --save-tilecrossed-image SN1468.CD68.TIF
24-11-2021 12:48:00 [CRITICAL]: Experimental support only for .tif slides!
24-11-2021 12:48:00 [CRITICAL]: Compiling the graph segmentation algorithm...
Compilation of the segmentation algorithm failed. Please compile it before running this script. Exiting.

How should I fix this error?

Thanks

Sander

swvanderlaan commented 2 years ago

And if I manually compile, I get these messages.

user@macbook ~/git/PyHIST/src
$ ls
__init__.py          graph_segmentation   slide.py
__pycache__          parser_input.py      utility_functions.py
(base)
user@macbook ~/git/PyHIST/src
$ cd graph_segmentation/
(base)
user@macbook ~/git/PyHIST/src/graph_segmentation
$ ls
COPYING         README          disjoint-set.h  image.h         imutil.h        pnmfile.h       segment-image.h
Makefile        convolve.h      filter.h        imconv.h        misc.h          segment-graph.h segment.cpp
(base)
slaan3@MA012494 ~/git/PyHIST/src/graph_segmentation
$ make
g++ -g -O3 -I. -o segment segment.cpp -lm
In file included from segment.cpp:24:
In file included from ./segment-image.h:26:
./segment-graph.h:79:3: warning: 'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'? [-Wmismatched-new-delete]
  delete threshold;
  ^
        []
./segment-graph.h:57:22: note: allocated with 'new[]' here
  float *threshold = new float[num_vertices];
                     ^
1 warning generated.
(base)
user@macbook ~/git/PyHIST/src/graph_segmentation

If I then check the contents of ~/git/PyHIST/src/graph_segmentation, I see this:

ls -lh
total 336
-rw-r--r--  1 user  staff    18K Nov 24 12:43 COPYING
-rwxr-xr-x  1 user  staff   338B Nov 24 12:43 Makefile
-rw-r--r--  1 user  staff   782B Nov 24 12:43 README
-rwxr-xr-x  1 user  staff   1.9K Nov 24 12:43 convolve.h
-rwxr-xr-x  1 user  staff   1.8K Nov 24 12:43 disjoint-set.h
-rwxr-xr-x  1 user  staff   2.9K Nov 24 12:43 filter.h
-rwxr-xr-x  1 user  staff   2.2K Nov 24 12:43 image.h
-rwxr-xr-x  1 user  staff   4.8K Nov 24 12:43 imconv.h
-rwxr-xr-x  1 user  staff   1.6K Nov 24 12:43 imutil.h
-rwxr-xr-x  1 user  staff   1.7K Nov 24 12:43 misc.h
-rwxr-xr-x  1 user  staff   5.1K Nov 24 12:43 pnmfile.h
-rwxr-xr-x  1 user  staff    84K Nov 24 13:32 segment
-rw-r--r--  1 user  staff   2.1K Nov 24 12:43 segment-graph.h
-rw-r--r--  1 user  staff   4.2K Nov 24 12:43 segment-image.h
-rw-r--r--  1 user  staff   1.4K Nov 24 12:43 segment.cpp
drwxr-xr-x  3 user  staff    96B Nov 24 13:32 segment.dSYM

So apparently segment was created, and I can also execute it:

./segment
usage: ./segment sigma k min input(ppm) output(ppm)

If next I run the python script again, it still fails.

source $HOME/.bash_profile
(base)
user@macbook ~/git/user/slideToolKit/upgrade_study/CD68/SN1468.CD68
$ conda activate cp4
(cp4)
user@macbook ~/git/user/slideToolKit/upgrade_study/CD68/SN1468.CD68
$ python3 ~/git/PyHIST/pyhist.py --content-threshold 0.05 --patch-size 64 --output-downsample 16 --info "verbose" --save-tilecrossed-image SN1468.CD68.TIF
24-11-2021 13:35:53 [CRITICAL]: Experimental support only for .tif slides!
24-11-2021 13:35:53 [CRITICAL]: Compiling the graph segmentation algorithm...
Compilation of the segmentation algorithm failed. Please compile it before running this script. Exiting.
(cp4)
user@macbook ~/git/user/slideToolKit/upgrade_study/CD68/SN1468.CD68

How should I fix this?

swvanderlaan commented 2 years ago

The same happens with the example svs-image.

python3 ~/git/PyHIST/pyhist.py --content-threshold 0.05 --patch-size 64 --output-downsample 16 --info "verbose" --save-tilecrossed-image ~/git/PyHIST/use_case/GTEX-1117F-0126.svs
24-11-2021 13:39:13 [CRITICAL]: Compiling the graph segmentation algorithm...
Compilation of the segmentation algorithm failed. Please compile it before running this script. Exiting.
manuel-munoz-aguirre commented 2 years ago

Hi, This may be due to where the script is being launched. PyHIST makes the compilation check assuming the working directory is the root of the project. Can you try moving to the root of PyHIST/ and run it from there?

swvanderlaan commented 2 years ago

Yes. That was it! Thanks.

So practically: you need to run each within the folder where PyHIST is.

swvanderlaan commented 2 years ago

Could I change this behavior in the code?

I would like to work in a different working directory, so keep PyHIST in a shared software folder. I am thinking I could possibly change this part:

#     if not os.path.isfile("src/graph_segmentation/segment"):
    if not sys.path.append(os.path.join(os.path.dirname(__file__), "src/graph_segmentation/segment")):

        # If Windows, the user must compile the script manually, otherwise we attempt to compile it
        if platform.system() == "Windows":
            logging.critical("Please compile the segmentation algorithm before running this script. Exiting.")
            sys.exit(1)
        else:
            logging.critical("Compiling the graph segmentation algorithm...")
            try:
#                 subprocess.check_call(["make"], stdout=subprocess.PIPE, cwd="src/graph_segmentation/")
                subprocess.check_call(["make"], stdout=subprocess.PIPE, cwd=sys.path.append(os.path.join(os.path.dirname(__file__), "src/graph_segmentation")))
            except Exception:
                print("Compilation of the segmentation algorithm failed. Please compile it before running this script. Exiting.")
                sys.exit(1)

Based on what I read here: https://stackoverflow.com/a/8663119/4322048.

Obviously I am no Python-buff :-). If you have any more tips and suggestions how to edit your scripts: I welcome them!

manuel-munoz-aguirre commented 2 years ago

Yes, this would work, thanks for pointing it out. We had also realized this would be a nice change to make, and it's currently sitting in the dev branch, so that it can be incorporated together with a future release.

swvanderlaan commented 2 years ago

Did you update it to the master-branch?