question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeWarning: divide by zero encountered in true_divide

See original GitHub issue

🐛Bug /home/xxxx/.local/lib/python3.8/site-packages/torchio/transforms/preprocessing/intensity/histogram_standardization.py:223: RuntimeWarning: divide by zero encountered in true_divide slopes = (s2 - s1) / (pc2 - pc1)

To reproduce

I use this data https://www.kaggle.com/c/rsna-miccai-brain-tumor-radiogenomic-classification/data

import numpy as np
import os
import pydicom
import cv2
import csv
import torchio

def main():

    max_DD = []
    nii_files = []
    for dir in ['../data/train/', '../data/train/']:
        for f in os.scandir(dir):
            for ff in os.scandir(os.path.join(dir,f.name)):
                nii_file = os.path.join(dir,f.name, ff.name,'0.nii')
                files = os.path.join(dir,f.name, ff.name)
                if os.path.isfile(nii_file):
                    image = torchio.ScalarImage(nii_file)
                else:
                    image = torchio.ScalarImage(files)

                    if image.data.min()==image.data.max():
                        print('empty image', files)

                    image = torchio.ToCanonical()(image)
                    image = torchio.Resample(image_interpolation = 'bspline')(image)
                    image.save(nii_file)

                nii_files.append(nii_file)
                print(nii_file)

                max_d = max(image.data.shape)
                print(max_d)
                max_DD.append(max_d)

    max_DD = np.array(max_DD)
    print('max', max_DD.min(), max_DD.mean(), max_DD.max())

    histogram_landmarks_path = '../data/landmarks.npy'

    landmarks = torchio.HistogramStandardization.train(
        nii_files,
        output_path=histogram_landmarks_path,
    )

if __name__ == '__main__':
    main()

System info

Output of python <(curl -s https://raw.githubusercontent.com/fepegar/torchio/master/print_system.py):

Platform:   Linux-5.4.0-65-generic-x86_64-with-glibc2.29
TorchIO:    0.18.45
PyTorch:    1.9.0+cu111
SimpleITK:  2.1.0 (ITK 5.2)
NumPy:      1.21.1
Python:     3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fepegarcommented, Aug 9, 2021

Thanks. It would be great if you could also provide a minimal working example using those files. I think your issue might be an empty image, but it’s hard to know without more information.

0reactions
fepegarcommented, Aug 31, 2021

Closing this for now. Happy to reopen if there is more interest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy divide by zero encountered in true_divide on np.where()
Importantly, true_opt is generated before calling the function. So if anything in it raises an error, the interpreter never gets to call np....
Read more >
How to Fix: invalid value encountered in true_divide - Statology
This tutorial explains how to address the following warning message in Python: RuntimeWarning: invalid value encountered in true_divide.
Read more >
How to Fix: Invalid value encountered in true_divide
Invalid value encountered in true_divide is a Runtime Warning occurs when we perform an invalid division operation between elements of NumPy ...
Read more >
How to get rid of warning-->RuntimeWarning: divide by zero ...
I have this sample code: x = 5 y = 0 z = x/y. But I got warning -->RuntimeWarning: divide by zero encountered...
Read more >
RuntimeWarning: divide by zero encountered in log10 #368
Describe the bug There are always runtime warnings of "divide by zero encountered in log10" in each run.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found