DigitalSlideArchive / HistomicsTK

A Python toolkit for pathology image analysis algorithms.
https://digitalslidearchive.github.io/HistomicsTK/
Apache License 2.0
394 stars 117 forks source link

TypeError in htk.filters.shape.clog #858

Closed sdasara95 closed 4 years ago

sdasara95 commented 4 years ago

I was trying to use the clog method in shape filters and ran into this problem.

TypeError                                 Traceback (most recent call last)
~\Anaconda3\envs\random\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    116     try:
--> 117         num = operator.index(num)
    118     except TypeError:

TypeError: 'numpy.float64' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-130-9e43386719ce> in <module>
      6 print(type(sigma_min))
      7 # imLog = htk.filters.shape.clog(imNucleiStain, imFgndMask, sigma_min = min_radius*math.sqrt(2), sigma_max=max_radius*math.sqrt(2))
----> 8 imLog = htk.filters.shape.clog(imNucleiStain, imFgndMask,\
      9                                    sigma_min= sigma_min,\
     10                                    sigma_max= sigma_max)

c:\users\satya\histomicstk\histomicstk\filters\shape\clog.py in clog(im_input, im_mask, sigma_min, sigma_max)
     70     sigma_end = np.ceil(sigma_max)
     71 
---> 72     sigma_list = np.linspace(sigma_start, sigma_end,
     73                              sigma_end - sigma_start + 1)
     74 

<__array_function__ internals> in linspace(*args, **kwargs)

~\Anaconda3\envs\random\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    117         num = operator.index(num)
    118     except TypeError:
--> 119         raise TypeError(
    120             "object of type {} cannot be safely interpreted as an integer."
    121                 .format(type(num)))

TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer.

This problem is due to int type not being enforced after using sigma_end - sigma_start + 1. sigma_end and sigma_start are numpy.float64 objects, hence, sigma_end-sigma_start+1 is also numpy.float64 but it has to be int

c:\users\satya\histomicstk\histomicstk\filters\shape\clog.py in clog(im_input, im_mask, sigma_min, sigma_max)
     70     sigma_end = np.ceil(sigma_max)
     71 
---> 72     sigma_list = np.linspace(sigma_start, sigma_end,
     73                              sigma_end - sigma_start + 1)
     74 

Line 73 in clog.py should be int(sigma_end-sigma_start+1))

sdasara95 commented 4 years ago

This replicates the error.

import numpy as np
a = np.float64(8)
b = np.float64(9)
np.linspace(a,b,a)

Running this gives same error:

TypeError                                 Traceback (most recent call last)
~\Anaconda3\envs\random\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    116     try:
--> 117         num = operator.index(num)
    118     except TypeError:

TypeError: 'numpy.float64' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-137-d52d8440ef94> in <module>
      2 a = np.float64(8)
      3 b = np.float64(9)
----> 4 np.linspace(a,b,a)

<__array_function__ internals> in linspace(*args, **kwargs)

~\Anaconda3\envs\random\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    117         num = operator.index(num)
    118     except TypeError:
--> 119         raise TypeError(
    120             "object of type {} cannot be safely interpreted as an integer."
    121                 .format(type(num)))

TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer.

It can be corrected by changing :

np.linspace(a,b,a)

to

np.linspace(a,b,int(a))
sdasara95 commented 4 years ago

Integer type check has to be enforced for the value passed as num argument in np.linspace() https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

cooperlab commented 4 years ago

This is fixed in #861 . Try this branch and see if it works for you.

kheffah commented 4 years ago

Fixed by #861