Jump to content

loop class objects


rbrown

Recommended Posts

I think this is what it is called...

I having a brain fart...

And I don't play with classes that much...

Plus I have been playing with trying to get pdf's to output correctly so my brain is toasty....

 

I'm trying to parse DICOM (medical file format) files and I have this class and I want to setup an array to pull what fields I want.

 

 

<?php
require '../dicomparser/nanodicom.php';
$filename = 'B4IBG5A0';
// 5) Load simple and print certain value
try
{
	//echo "5) Load simple and print certain value\n";
	$dicom = Nanodicom::factory($filename);
	$dicom->parse();
	//echo $dicom->profiler_diff('parse').'<br>';
	echo 'Patient Name: '.$dicom->value(0x0010, 0x0010).'<br>';
	echo 'Operators Name: '.$dicom->value(0x0010, 0x0040).'<br>';
	echo 'Patient ID: '.$dicom->value(0x0010,0x0020).'<br>';

	unset($dicom);
}
catch (Nanodicom_Exception $e)
{
	echo 'File failed. '.$e->getMessage()."\n";
}
//}

?>

 

So what I want to do is have the array contain 'Patient Name' => '(0x0010, 0x0010)' etc...

And have it something like this so I don't have to echo all the lines I want.

 

foreach ($array as $key => $value) {
echo $key': '.$dicom->value.$value.'<br>';
}

 

I can't get it to work... I remember figuring it out once before but that was over a year ago... And I don't have the code I wrote. I know it's something simple stupid. like $$ or [] i'm not getting the combination right.

 

Thanks in advance...

 

 

 

 

 

 

Link to comment
Share on other sites

Thanks...

But I needed to do this to get it to work:

 

$myArray['PatientName'] = $dicom->value(0x0010, 0x0010);
foreach($myArray as $key => $value) {
	echo $key.': '.$value."<br>";
}

 

Wasn't planning on having to have the "$dicom->value" in the array.

Can't get it to work any other way than above.

 

I'm not getting the difference between the above and this...

 

$myArray['PatientName'] = '0x0010, 0x0010';
foreach($myArray as $key => $value) {
  $show_it = $dicom->value($value);
   echo $key.': '.$show_it.'<br>';
}

 

the above doesn't work and I tried a bunch of ways with the () in the array and out and ' " qoutes and trying to set the "$show_it" var different ways, and the echo statement without the "$show_it" var thinking maybe that was it.

 

If i have to, then I have to add "$dicom->value" to the array, but I was trying to get away from adding it to the array. I wanted to build the array based on the "VR" I need from the list of over 1500 values.

 

also the actual DICOM dictionary values don't contain the "0x" prefix he has that in there for the parsing. Example:

Attribute Name, VR, Tag

Patient ID, LO, (0010,0020)

 

so to make it easier when they add to the DICOM dictionary I would like to have it so I can just up date the database without having to parse the dictionary and adding a ton of stuff to it.

 

So I really wanted to build it on the fly.

So once I get the "VR" set from the database, I can parse the "Tag" and output something like this:

 

$parsed_tag1 = $prefix.$Parsed_tag1;

$parsed_tag2 = $prefix.$Parsed_tag2;

echo $key.': '.$dicom->value(.$parsed_tag1.', '.$parsed_tag1.);

 

 

Hope this makes sense....

 

 

 

Link to comment
Share on other sites

Here is the orginal example code I'm playing with:

 

	// 5) Load simple and print certain value
try
{
	echo "5) Load simple and print certain value\n";
	$dicom = Nanodicom::factory($filename);
	$dicom->parse();
	echo $dicom->profiler_diff('parse')."\n";
	echo 'Patient Name: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
	unset($dicom);
}
catch (Nanodicom_Exception $e)
{
	echo 'File failed. '.$e->getMessage()."\n";
}

 

So I have a the database with 3 fields with data like this.

Attribute_Name RV Tag

Patient ID LO (0010,0020)

Patient Orientation CS (0020,0020)

Patient Orientation Code Sequence SQ (0054,0410)

Patient Orientation Modifier Code Sequence SQ (0054,0412)

Patient Position CS (0018,5100)

Patient Setup Number IS (300A,0182)

Patient Setup Sequence SQ (300A,0180)

Patient State LO (0038,0500)

Patient Support Angle DS (300A,0122)

Patient Support Angle Tolerance DS (300A,004C)

Patient Support Rotation Direction CS (300A,0123)

Patient Transport Arrangements LO (0040,1004)

Patient's Name PN (0010,0010)

Patient's Primary Language Code Modifier Sequence SQ (0010,0102)

Patient's Primary Language Code Sequence SQ (0010,0101)

Patient's Telephone Numbers SH (0010,2154)

Patient's Address LO (0010,1040)

Patient's Age AS (0010,1010)

Patient's Birth Date DA (0010,0030)

Patient's Birth Name PN (0010,1005)

Patient's Birth Time TM (0010,0032)

 

And here is the code on how I want it to work:

 

$mode = 'LO';
$prefix = '0x';
$chars =array("(", ")", " ");

$sql = "SELECT * FROM premier_forms.dicom WHERE RV = '$Mode' ";
$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs)) {
$row['Tag'] = str_replace($chars, "", $row['Tag']);
$tag_value = explode(',', $row['Tag']);
$tag_value_1 = $prefix.$tag_value['0'];
$tag_value_2 = $prefix.$tag_value['0'];

try
{
	$dicom = Nanodicom::factory($filename);
	$dicom->parse();
	echo $row['Attribute_Name'].': '.$dicom->value($tag_value_1, $tag_value_2)."<br>";
	unset($dicom);
}
catch (Nanodicom_Exception $e)
{
	echo 'File failed. '.$e->getMessage()."\n";
}
}

 

Does that make more sense?

 

Link to comment
Share on other sites

As far as I can tell, everything looks right, assuming you don't want to loop through all of your MySQL results.

 

I have no idea what the Nanodicom class is doing, so I can't tell if that's the issue.

 

What kind of output are you getting? What kind of output are you expecting?

Link to comment
Share on other sites

When I run the code,

All it is ouputting is:

 

Patient ID:

 

And it should be:

Patient ID: 2507

 

so it's not liking the:

 

$dicom->value($tag_value_1, $tag_value_2)

 

part of:

 

echo $row['Attribute_Name'].': '.$dicom->value($tag_value_1, $tag_value_2)."<br>";

 

but it doesn't throw an error.

 

If I just use:

 

echo 'Patient ID: '.$dicom->value(0x0010,0x0020)."<br>";

 

OR

If I create an array like this:

$myArray['Patient ID'] =$dicom->value(0x0010,0x0020);

$myArray['Patient Name'] =$dicom->value(0x0010, 0x0010);

 

and loop trough it, that works... to but that it defeats the purpose of having the database I would have to type in all 1500 plus settings.

 

 

So it's ignoring the $tag_value_1, $tag_value_2 values when I set it that way.

 

 

I can't send the test dicom file because it is a real patient file I'm testing with... HIPPA laws.

The sample files I have don't have any patient data in them.

 

 

 

 

 

 

 

Link to comment
Share on other sites

Also tried

$filename = 'B4IBG5A0';
$mode = 'LO';
$prefix = '0x';
$chars =array("(", ")", " ");

$sql = "SELECT * FROM premier_dicom.dicom_data WHERE VR = '".$mode."' ";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
	$row['Tag'] = str_replace($chars, "", $row['Tag']);
	$tag_value = explode(',', $row['Tag']);
	$tag_value_1 = $prefix.$tag_value['0'];
	$tag_value_2 = $prefix.$tag_value['0'];
echo $tag_value_1.'<br>';
echo $tag_value_2.'<br>';
	$myarray[$row['Attribute_Name']] = $dicom->value($tag_value_1, $tag_value_2);

try
{
$dicom = Nanodicom::factory($filename);
$dicom->parse();



print_r($myarray);
foreach($myarray as $key => $value) {
	echo $key.': '.$value."<br>";
	//echo 'Patient ID: '.$dicom->value(0x0010,0x0020)."<br>";
	unset($dicom);
}
}
catch (Nanodicom_Exception $e)
{
echo 'File failed. '.$e->getMessage()."\n";
}
}

 

 

 

Get:

Notice: Undefined variable: dicom in C:\a\UniServer\www\includes\dicom_test.php on line 24

And  can see the two values before the script stops.

 

 

So then I moved the setting for the array to inside the class:

$filename = 'B4IBG5A0';
$mode = 'LO';
$prefix = '0x';
$chars =array("(", ")", " ");

$sql = "SELECT * FROM premier_dicom.dicom_data WHERE VR = '".$mode."' ";
$rs = mysql_query($sql);


try
{
$dicom = Nanodicom::factory($filename);
$dicom->parse();

while($row = mysql_fetch_array($rs)) {
	$row['Tag'] = str_replace($chars, "", $row['Tag']);
	$tag_value = explode(',', $row['Tag']);
	$tag_value_1 = $prefix.$tag_value['0'];
	$tag_value_2 = $prefix.$tag_value['0'];
echo $tag_value_1.'<br>';
echo $tag_value_2.'<br>';
	$myarray[$row['Attribute_Name']] = $dicom->value($tag_value_1, $tag_value_2);
}
print_r($myarray);
foreach($myarray as $key => $value) {
	echo $key.': '.$value."<br>";
	//echo 'Patient ID: '.$dicom->value(0x0010,0x0020)."<br>";
	unset($dicom);
}
}
catch (Nanodicom_Exception $e)
{
echo 'File failed. '.$e->getMessage()."\n";
}

 

Then I get:

all the values showing...

 

0x0040

0x0040

0x0028

0x0028

etc...

 

 

and the print_r show the $key being set but not the $value part of the array.

 

Array ( [Acquisition Device Processing Code] => [Acquisition Device Processing Description] => [Admission ID] => [Admitting Diagnoses Description] => [Application Setup Manufacturer] => [Application Setup Name] =>

etc....

and then it shows the $keys

 

 

Acquisition Device Processing Description:

Admission ID:

Admitting Diagnoses Description:

Application Setup Manufacturer:

Application Setup Name:

Applicator Description:

Attenuation Correction Method:

Authorization Equipment Certification Number:

Beam Name:

etc....

 

 

So being able to set the values inside the class value is where the problem is.

 

 

 

Link to comment
Share on other sites

Assuming $tag_value_1 and $tag_value_2 are being filled with the results you're expecting, the issue appears to be with your class.

 

We can't help you on that one, sadly.

Link to comment
Share on other sites

Hi Robert,

 

I am the creator of Nanodicom. Please try this code: https://gist.github.com/963030

 

Posted below as well

 

<?php
try
{
// Load and parse the file
$dicom = Nanodicom::factory($filename);
$dicom->parse();

$mode = 'LO';

// Do the Query
$sql = "SELECT * FROM premier_forms.dicom WHERE RV = '$mode' ";
$rs = mysql_query($sql);

// Browse result
while($row = mysql_fetch_array($rs))
{
	// Get element and group from column
	list($element, $group) = sscanf($row['Tag'], "(%X,%X)");

	// Print the value
	echo $row['Attribute_Name'].': '.$dicom->value($element, $group).'<br>';
}

// Release memory
unset($dicom);
}
catch (Nanodicom_Exception $e)
{
// Catach exception
echo 'File failed. '.$e->getMessage()."\n";
}

 

A couple of observations:

- $dicom->value accepts hex values (or their equivalent in integer), you were passing an string.

- you should parse the dicom file only once.

 

Please let me know if that helps.

 

Thanks,

 

Nano.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.