'FileDataset' object has no attribute 'DoseGrid' - python

I have been trying to use the dicompyler-core to put together two dose grids from two different dose dicoms.
import pydicom
import numpy as np
import os
import glob
from dicompylercore import dicomparser, dvh, dvhcalc
from dicompylercore import dose
I keep receiving this error while trying to import dose
ImportError Traceback (most recent call last)
<ipython-input-120-6605335fa321> in <module>
5 import glob
6 from dicompylercore import dicomparser, dvh, dvhcalc
----> 7 from dicompylercore import dose
ImportError: cannot import name 'dose' from 'dicompylercore'
Because of this import error, I think that is why there is no attribute to be found when I try and add the two dose grids together
dose1 = dicomparser.DicomParser("RD.CW.dcm")
dose2 = dicomparser.DicomParser("RD.CCW.dcm")
grid_1 = dose.DoseGrid()
grid_2 = dose.DoseGrid()
grid_sum = grid_1 + grid_2
grid_sum.save_dcm("grid_sum.dcm")
It gives me this error
~\Anaconda3\lib\site-packages\pydicom\dataset.py in __getattr__(self, name)
550 if tag is None: # `name` isn't a DICOM element keyword
551 # Try the base class attribute getter (fix for issue 332)
--> 552 return super(Dataset, self).__getattribute__(name)
553 tag = Tag(tag)
554 if tag not in self._dict: # DICOM DataElement not in the Dataset
AttributeError: 'FileDataset' object has no attribute 'DoseGrid'
I am new to working with dicom files and with dicompylercore. I am unsure if this is related to the dicom files I am working itself or if there is some issue with the dicomplyercore package itself. Are there any suggestions on what I could do to fix this?

The dose module has been added to dicompylercore after the last PyPi release, so you have to install the version from GitHub, if you want to use it:
pip install git+https://github.com/dicompyler/dicompyler-core
I'm not sure how stable this version is, but you can ask about the next release on the development site.

Related

Getting "NameError: np is not defined", but np is defined both in main and in imported module

I wrote a module for my dataset classes, located in the same directory as my main file.
When i import it and try to instantiate a class i get a "np is not defined" error even though numpy is imported correctly in both the main file and the module.
I'm saying correctly because if i try both to call another numpy function from the main or to execute the module on alone no error rises.
This is the code in the main file:
import torch
import numpy as np
from myDatasets import SFCDataset
trainDs = SFCDataset(paths["train"])
and this is the module:
import torch
import numpy as np
from torch.utils.data import Dataset
#Single Fragment Classification Dataset
class SFCDataset(Dataset):
def __init__(self, path, transform=None, norms=False, areas=False, **kwargs, ):
super(SFCDataset, self).__init__()
self.data = torch.load(path)
self.fragments = []
self.labels = []
for item in self.data:
self.fragments.append(item[0])
self.labels.append(item[1])
self.fragments=np.array(self.fragments)
self.labels=np.array(self.labels)
if norms:
if areas:
self.fragments = np.transpose(self.fragments[:], (0,2,1,3)).reshape(-1,1024,9)[:,:,:7]
else:
self.fragments = np.transpose(self.fragments[:], (0,2,1,3)).reshape(-1,1024,9)[:,:,:6]
else:
if areas:
self.fragments = np.transpose(self.fragments[:], (0,2,1,3)).reshape(-1,1024,9)[:,:,[0,1,2,6]]
else:
self.fragments = np.transpose(self.fragments[:], (0,2,1,3)).reshape(-1,1024,9)[:,:,:3]
self.transform = transform
def __len__(self) -> int:
return len(self.data)
def __getitem__(self, index):
label = self.labels[index]
pointdata = self.fragments[index]
if self.transform:
pointdata = self.transform(pointdata)
return pointdata, label
if __name__ == "__main__":
print("huh")
path = "C:\\Users\\ale23\\OneDrive\\Desktop\\Università\\Tesi\\data\\dataset_1024_AB\\train_dataset_AED_norm_area.pt"
SFCDataset(path)
I don't know what else to try.
I'm on VSCode, using a 3.9.13 virtual enviroment.
edit:
this is the error i'm getting:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[75], line 5
2 import numpy as np
3 from myDatasets import SFCDataset
----> 5 trainDs = SFCDataset(paths["train"])
File c:\Users\ale23\OneDrive\Desktop\Università\Dottorato\EquivariantNN\myDatasets.py:17, in SFCDataset.__init__(self, path, transform, norms, areas, **kwargs)
15 self.fragments.append(item[0])
16 self.labels.append(item[1])
---> 17 self.fragments=np.array(self.fragments)
18 self.labels=np.array(self.labels)
20 if norms:
NameError: name 'np' is not defined
edit2: i edited some stuff like path names and parts occuring after the error to try to lighten up the code, sorry, i should have uploaded all the code as it is run now.
Edit3: I was trying to reproduce the error in some other way and was building a dummy module. I tried to import the class in that other module and running the dummy.py and it runs. That appears to be a problem with the fact i'm working on a notebook, is that possible?
the dummy module:
import numpy as np
def test():
print(np.array(1))
print(np.array(2))
print(np.sum(np.random.rand(2)))
from myDatasets import SFCDataset
trainDs=SFCDataset("C:\\Users\\ale23\\OneDrive\\Desktop\\Università\\Tesi\\data\\dataset_1024_AB\\train_dataset_AED_norm_area.pt")
this runs by calling "python testmodule.py" in the console
Edit 4:
Today i restarted my pc and run the same code as yesterday and the notebook works. Yesterday i tried to close vscode and restart it, but it did not help.
Maybe something is wrong with the virtual environment? I don't know where to look at honestly.
Anyways the program now runs with no errors, should i close this?
Thank you all for your time and help

Getting the following error while using scikit-image to read images "AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame' "

I am using scikit-image to load a random image from a folder. OpenCV is being used for operations later on..
Code is as follows (only relevant parts included)
import imageio
import cv2 as cv
import fileinput
from collections import Counter
from data.apple_dataset import AppleDataset
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
from torchvision.transforms import functional as F
import utility.utils as utils
import utility.transforms as T
from PIL import Image
import skimage.io
from skimage.viewer import ImageViewer
from matplotlib import pyplot as plt
%matplotlib inline
APPLE_IMAGE_PATH = r"__mypath__\samples\apples\images"
# Load a random image from the images folder
FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
apple_image = skimage.io.imread(random_apple_in_folder)
apple_image_cv = cv.imread(random_apple_in_folder)
apple_image_cv = cv.cvtColor(apple_image_cv, cv.COLOR_BGR2RGB)
Error is as follows
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-9575eed18f18> in <module>
11 FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
12 random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
---> 13 apple_image = skimage.io.imread(random_apple_in_folder)
14 apple_image_cv = cv.imread(random_apple_in_folder)
AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame'
How do i proceed from here? What should i change???
This is a bug in Pillow 7.1.0. You can upgrade Pillow with pip install -U pillow. See this bug report for more information:
https://github.com/scikit-image/scikit-image/issues/4548

Module has no attribute error in python3

contents of io.py
class IO:
def __init__(self):
self.ParsingFile = '../list'
def Parser(self):
f = open(ParsingFile, 'r')
print(f.read())
contents of main.py
import sys
sys.path.insert(0, "lib/")
try:
import io
except Exception:
print("Could not import one or more libraries.")
exit(1)
print("Libraries imported")
_io_ = io.IO()
When I run python3 main.py I get the following error:
Libraries imported
Traceback (most recent call last):
File "main.py", line 11, in <module>
_io_ = io.IO()
AttributeError: module 'io' has no attribute 'IO'
Any idea what's going wrong?
My file was called io. It seems that there already exists a package called io which caused the confusion.
Your package name (io) conflicts with the Python library's package with the same name, so you actually import a system package.
You can check this by printing io.__all__.
Changing io.py to something else is probably the best way to go to avoid similar problems. Otherwise, you can use an absolute path.
try
from io import IO
That worked for me when trying to import classes from another file
this has more information:
Python module import - why are components only available when explicitly imported?

ImportError when trying to import Prov Module in Python

I'm getting this error when trying to import a module from the Prov package.
Here is the contents of my file:
#!/usr/bin/env
import sys
egg_path='/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov'
sys.path.append(egg_path)
#... rest of code
import model as prov
def main():
# Create a new provenance document
d1 = ProvDocument() # d1 is now an empty provenance document
# Declaring namespaces for various prefixes used in the example
d1.add_namespace('now', 'http://www.provbook.org/nownews/')
d1.add_namespace('nowpeople', 'http://www.provbook.org/nownews/people/')
d1.add_namespace('bk', 'http://www.provbook.org/ns/#')
# Entity: now:employment-article-v1.html
e1 = d1.entity('now:employment-article-v1.html')
# Agent: nowpeople:Bob
d1.agent('nowpeople:Bob')
And here is the output:
Traceback (most recent call last):
File "prov.py", line 6, in <module>
import model as prov
File "/Library/Python/2.7/site-packages/prov-1.5.0-py2.7.egg/prov/model.py", line 25, in <module>
from prov import Error, serializers
ImportError: cannot import name Error
Any ideas or fixes? I installed Prov using easy_install prov.
You need to rename your module file prov.py. It prevents import of the third-party library because the module name conflicts.
Make sure prov.pyc is removed.
I found the error. The name of my file that I was trying to import into was also called prov.py . It was a circular dependency issue.
Thank you guys for such quick responses!

AttributeError: 'module' object has no attribute 'version' Canopy

Hi I am going to preface this with I could just be really dumb so don't overlook that, but suddenly when opening canopy today I wasn't able to run one of my typical scripts with the error AttributeError: 'module' object has no attribute ' version' when trying to load pandas. From what I can gather it seems when bumpy is called through pandas it fails. I checked my working directory for files named numpy.py to see if I idiotically named a file numpy but failed to find such a file. I also attempted to uninstall and reinstall both numpy and pandas from the package manager in canopy. Any suggestions?
%run "/Users/jim/Documents/ORAL-PAT-2.5-3.5plotly.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/jim/Documents/ORAL-PAT-2.5-3.5plotly.py in <module>()
1 #import the modules you need
----> 2 import pandas as pd
3 import numpy as np
4 import plotly.plotly as py
5 import plotly.tools as tls
/Users/jim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/__init__.py in <module>()
20
21 # numpy compat
---> 22 from pandas.compat.numpy_compat import *
23
24 try:
/Users/jim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/compat/numpy_compat.py in <module>()
13
14 # numpy versioning
---> 15 _np_version = np.version.short_version
16 _np_version_under1p8 = LooseVersion(_np_version) < '1.8'
17 _np_version_under1p9 = LooseVersion(_np_version) < '1.9'
AttributeError: 'module' object has no attribute 'version'
Just had the same problem after downgrading Pandas and upgrading again to fix another issue. This is just a hack, but you could try this:
Open ...pandas/compat/numpy_compat.py and replace np.version.short_version with np._np_version
Hope that helps!

Categories

Resources