Python Forum
Using variables as the assigned value and not the string
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using variables as the assigned value and not the string
#1
Hello,

I am new to python and I have a novice question. Here is my code:
############Code Begin#############
import dicom
import os
import numpy
from matplotlib import pyplot, cm

fwd_hd = dicom.read_file("fwd_abcd/i00001.CFMRI.1")
rvs_hd = dicom.read_file("rvs_abcd/i00001.CFMRI.1")
parameters = "Rows", "Columns", "IDSeriesTime"
for parameter in parameters:
varfwd = "fwd_hd.%s" % parameter
varrvs = "rvs_hd.%s" % parameter
if varfwd != varrvs:
print("forward and reverse variable %s DOES NOT MATCH!!!") % parameter
############Code End#############

fwd_hd.Columns = 90
rvs_hd.Columns = 60

I want to compare varfwd and varrvs, but not the string 'fwd_hd.Columns' and 'rvs_hd.Columns'...

Which is what this code does.

I want to compare the value of the variable fwd_hd.Columns and rvs_hd.Columns.

Can anyone help me figure out what I have done wrong? I have tried for several hours to solve it.

Thanks a lot!
Reply
#2
Re-post. Use code tags around code, see BBCODE
and preserve indentation by pasting using ctrl-shift-v
Reply
#3
Here it is with the code preserved.

Hello,

I am new to python and I have a novice question. Here is my code:
############Code Begin#############
import dicom
import os
import numpy
from matplotlib import pyplot, cm

fwd_hd = dicom.read_file("fwd_abcd/i00001.CFMRI.1")
rvs_hd = dicom.read_file("rvs_abcd/i00001.CFMRI.1")
parameters = "Rows", "Columns", "IDSeriesTime"
#parameters = ["Rows"]
for parameter in parameters:
	varfwd = "fwd_hd.%s" % parameter
	varrvs = "rvs_hd.%s" % parameter
	if varfwd != varrvs:
############Code End#############
fwd_hd.Columns = 90
rvs_hd.Columns = 60

I want to compare varfwd and varrvs, but not the string 'fwd_hd.Columns' and 'rvs_hd.Columns'...

Which is what this code does.

I want to compare the value of the variable fwd_hd.Columns and rvs_hd.Columns.

Can anyone help me figure out what I have done wrong? I have tried for several hours to solve it.

Thanks a lot!
Reply
#4
(Aug-25-2017, 04:31 PM)ajacobson Wrote: I want to compare the value of the variable fwd_hd.Columns and rvs_hd.Columns.
?
if fwd_hd.Columns == rvs_hd.Columns:
    print("they're the same")
Reply
#5
Thanks for looking at this, but I need to compare them in a loop using each of the parameter names. Is there a way to easily do that? Similar to what I originally had.

I really appreciate your help!
Reply
#6
I don't know what format the object is in, can you access it like a dict?
if fwd_hd[parameter] == rvs_hd[parameter]:
    # stuff
Reply
#7
Thanks for the tip. It moved beyond the original error but now I am getting this:

import dicom
import os
import numpy
from matplotlib import pyplot, cm

fwd_hd = dicom.read_file("fwd_abcd/i00001.CFMRI.1")
rvs_hd = dicom.read_file("rvs_abcd/i00001.CFMRI.1")
parameters = "Rows", "Columns"
for parameter in parameters:
	if fwd_hd[parameter] == rvs_hd[parameter]:			
		print("forward and reverse variable %s DOES NOT MATCH!!!") % parameter
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/site-packages/dicom/dataset.py", line 276, in __getitem__
tag = Tag(key)
File "/usr/lib/python2.7/site-packages/dicom/tag.py", line 32, in Tag
raise ValueError("Tags cannot be instantiated from a single string")
ValueError: Tags cannot be instantiated from a single string


And when I simply type: fwd_hd[parameter]

I get the same error.

Any suggestions? I am checking on the format.
Reply
#8
Quote:
Error:
File "/usr/lib/python2.7/site-packages/dicom/dataset.py", line 276, in __getitem__ tag = Tag(key) File "/usr/lib/python2.7/site-packages/dicom/tag.py", line 32, in Tag raise ValueError("Tags cannot be instantiated from a single string") ValueError: Tags cannot be instantiated from a single string

Accessing it like a dict uses __getitem__, which this class uses to mean you're doing something with Tags.  I don't know what Tag means in this context, but whatever it is, you can't create a Tag using only a string.

Let's guess that you're using this module: http://pydicom.readthedocs.io/en/stable/...guide.html
There's an example of getting the tagid using a string, so maybe you can do this?
for parameter in parameters:
    print(fwd_hd.data_element(parameter).value)
Reply
#9
Thanks so much! That works perfectly!
Reply
#10
I have moved further in this script and changed it a bit. Now I am importing data from a text file and comparing my dicom data to the values in the text file. I ran into another similar issue that I have not been able to solve.
import dicom
import os
import numpy
from matplotlib import pyplot, cm


epi_hd = dicom.read_file("epi/i00001.1")


def getVarFromFile(epi_template2):
    import imp
    f = open(epi_template2)
    global data
    data = imp.load_source('data', '', f)
    f.close()

getVarFromFile('/path/to/epi_template2')
parameters = "SAR",	"Rows", "Modality",	"Columns"
for parameter in parameters:
	if (epi_hd.data_element(parameter).value) != (data.parameter):
		print("%s: EPI PARAMETER MISMATCH!!!!!") % parameter 
The issue is with looping through the parameter values.

(epi_hd.data_element(parameter).value) gives me the correct value

but...

(data.parameter) is not recognized even though data.SAR returns the correct value.

error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parameter'

Does anyone have any helpful suggestions?

Thanks a lot
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020