1
Hello, I am working with a code that uses two classes in two files: Radoncdt and CDT. The first one needs the second one. However, when trying to use one of the functions of the CDT class, I get the error:
module 'cdt' has no attribute 'CDT'
Follows the code of the classes:
import numpy as np
import cdt as CDT
from scipy import interp
from skimage.transform import radon, iradon
class RadonCDT:
def __init__(self,theta=np.arange(180)):
self.theta=theta
def transform(self,I):
"""
transform calculates calculates the Radon transform of the input image
and calculate its CDT with respect to the Radon transform of the template
input:
I: A two-dimensional distribution
output:
The Radon-CDT transformation of I
"""
assert not((1.0*I<0).sum())
# Force I to be a positive probability distribution
eps=1e-5#This small dc level is needed for a numerically unique solution of the transport map
I=I+eps
I=I/I.sum()
radonI=radon(I,theta=self.theta,circle=False)
self.dim=radonI.shape[0]
Ihat=np.zeros_like(radonI)
self.cdt=CDT.CDT(dim=self.dim)
for i in range(len(self.theta)):
Ihat[:,i]=self.cdt.transform(radonI[:,i])
return Ihat
def itransform(self,Ihat):
"""
itransform calculates the inverse of the Radon-CDT. It receives the Radon-CDT
and finds the corresponding two-dimensional distribution I from it.
input:
Ihat: Radon-CDT of I
output:
I: The original distribution
"""
radonI=np.zeros_like(Ihat)
for i in range(len(self.theta)):
radonI[:,i]=self.cdt.itransform(Ihat[:,i])
I=iradon(radonI,theta=self.theta,circle=False)
return I
And the other class:
import numpy as np
from scipy import interp
class CDT:
def __init__(self,dim):
template=np.ones((dim,))
template=template/template.sum()
self.template=template
self.dim=dim
self.template_CDF=np.cumsum(template)
self.x=np.arange(self.dim) #Discretization of the domain of template
self.xtilde=np.linspace(0,1,self.dim)
self.template_CDF_inverse=interp(self.xtilde,self.template_CDF,self.x)
def transform(self,I):
"""
transform calculates the transport map, f, that morphs the one-dimensional distribution
I into the template.
input:
I: A one dimensional distributions of size self.dim
output:
The CDT transformation of I
"""
assert self.dim==len(I)
assert not((1.0*I<0).sum())
# Force I to be a positive probability distribution
eps=1e-5#This small dc level is needed for a numerically unique solution of the transport map
I=I+eps
I=I/I.sum()
#Calculate its CDF
I_CDF=np.cumsum(I)
I_CDF_inverse = interp(self.xtilde,I_CDF, self.x)
u = interp(self.x,self.template_CDF_inverse,self.template_CDF_inverse-I_CDF_inverse)
Ihat= u*np.sqrt(self.template)
return Ihat
def itransform(self,Ihat):
"""
itransform calculates the inverse of the CDT. It receives a signal in the CDT space
and finds the corresponding one dimensional distribution I from it.
input:
u: Transport displacement map
I0: The template used for calculating the CDT
output:
I: The original distribution
"""
u=Ihat/np.sqrt(self.template)
f=self.x-u
fprime=np.gradient(f)
I = interp(self.x,f, self.template/fprime)
I = I/I.sum()
return I
In the main code, my mistake happens here:
import transportBasedTransforms.radonCDT as RCDT
theta=np.arange(180)
rcdt=RCDT.RadonCDT(theta)
# Load images and calculate their Radon-CDT
I=[]
rI=[]
Ihat=[]
for i in range(2):
I.append(rgb2gray(imread('./Data/I%d.bmp'%(i))))
rI.append(radon(I[i],theta,circle=False))
Ihat.append(rcdt.transform(I[i]))
I’ve tried to change the way I import, I’ve put them all in the same file, but I think it’s something else.
Can you review the indentations in your question? It seems that when you pasted the code here on the platform some lines got strange. Also put the name of the files in which each code snippet is, please.
– Woss
Instead of
import cdt as CDT
, you tried tofrom cdt import CDT
????– Paulo Marques
Ready! The class files are in radonCDT.py and Cdt.py. I tried putting
from cdt import CDT
yes, but I get the same mistake.– Sara