[ismrmrd] 157/177: adapt example Python script to new Python API

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:02:14 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag v1.1.0.beta.1
in repository ismrmrd.

commit f75880a16386b833ec05a5111d27d0d030398296
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Thu Oct 23 18:07:50 2014 +0100

    adapt example Python script to new Python API
---
 examples/python/ismrmrd_create_dataset.py | 210 ++++++++++++++++--------------
 1 file changed, 110 insertions(+), 100 deletions(-)

diff --git a/examples/python/ismrmrd_create_dataset.py b/examples/python/ismrmrd_create_dataset.py
index 1a14ce2..83fa455 100644
--- a/examples/python/ismrmrd_create_dataset.py
+++ b/examples/python/ismrmrd_create_dataset.py
@@ -1,7 +1,6 @@
 # coding: utf-8
 import os
 import ismrmrd
-import ismrmrd_xsd
 import numpy as np
 import matplotlib.pyplot as plt
 
@@ -10,7 +9,8 @@ filename = 'testdata.h5'
 if os.path.isfile(filename):
     os.remove(filename)
 # Create an empty ISMRMRD dataset
-dset = ismrmrd.IsmrmrdDataset(filename, 'dataset')
+dset = ismrmrd.Dataset(filename, "dataset")
+dset.open(create_if_needed=True)
 
 # Synthesize the object
 nX, nY = 256, 256
@@ -42,25 +42,23 @@ for rep in range(nreps):
 rep0 = np.sqrt(np.sum(np.abs(K) ** 2, 2))
 plt.imshow(rep0[:,:,0])
 
-# Append a block of acquisitions, one repetition at a time (faster than one acquisition at a time)
-# Re-use the same Acquisition object because why not?
-acq = ismrmrd.Acquisition()
+
 for rep in range(nreps):
     for line in range(nY):
+        # Generate header
         head = ismrmrd.AcquisitionHeader()
+        counter = ismrmrd.EncodingCounters()
         head.version = 1
         head.number_of_samples = nX
         head.center_sample = nX / 2
         head.active_channels = ncoils
-        rd, pd, sd = ismrmrd.floatArray(3), ismrmrd.floatArray(3), ismrmrd.floatArray(3)
-        rd[0] = 1; pd[1] = 1; sd[2] = 1
-        head.read_dir = rd
-        head.phase_dir = pd
-        head.slice_dir = sd
+        head.read_dir  = [1., 0., 0.]
+        head.phase_dir = [0., 1., 0.]
+        head.slice_dir = [0., 0., 1.]
         head.scan_counter = rep * nY + line
-        head.idx.kspace_encode_step_1 = line
-        head.idx.repetition = rep
-
+        counter.kspace_encode_step_1 = line
+        counter.repetition = rep
+        head.idx = counter
         # Note: the correct API for setting Acquisition flags looks like this:
         #   acq.setFlag(ismrmrd.FlagBit(ismrmrd.ACQ_FIRST_IN_ENCODE_STEP1))
         # but using this API would require using only ugly "acq.setXXX" methods
@@ -74,92 +72,104 @@ for rep in range(nreps):
             head.flags |= 1 << ismrmrd.ACQ_LAST_IN_ENCODE_STEP1
             head.flags |= 1 << ismrmrd.ACQ_LAST_IN_SLICE
             head.flags |= 1 << ismrmrd.ACQ_LAST_IN_REPETITION
-
-        acq.setHead(head)
-        # copy the array to ensure it's a continuous block of memory
-        data = np.zeros((2 * nX, ncoils), dtype=np.float32)
-        data[::2] = np.array([c.real for c in np.array(K[:,line,:,rep])])
-        data[1::2] = np.array([c.imag for c in np.array(K[:,line,:,rep])])
-        acq.setData(data.flatten())
-        dset.appendAcquisition(acq)
-
+        
+        # Generate k-space data
+        data = (np.array([c.real for c in np.array(K[:,line,:,rep])]) +
+                1j * np.array([c.imag for c in np.array(K[:,line,:,rep])]))
+        
+        # Construct acquisition object from header
+        acq = ismrmrd.Acquisition(head=head)
+        
+        # Use view to fill in the internal data array
+        acq.data.ravel()[:] = data.ravel()[:]
+        
+        # Append to HDF5 dataset
+        dset.append_acquisition(acq)
 
 # Fill the XML header
-header = ismrmrd_xsd.ismrmrdHeader()
-
-# Experimental Conditions
-exp = ismrmrd_xsd.experimentalConditionsType()
-exp.H1resonanceFrequency_Hz = 128000000
-header.experimentalConditions = exp
-
-# Acquisition System Information
-sys = ismrmrd_xsd.acquisitionSystemInformationType()
-sys.receiverChannels = ncoils
-header.acquisitionSystemInformation = sys
-
-# Encoding
-encoding = ismrmrd_xsd.encoding()
-encoding.trajectory = ismrmrd_xsd.trajectoryType.cartesian
-
-# Encoded Space
-fov = ismrmrd_xsd.fieldOfView_mm()
-fov.x = 256
-fov.y = 256
-fov.z = 5
-
-matrix = ismrmrd_xsd.matrixSize()
-matrix.x = np.size(K, 0)
-matrix.y = np.size(K, 1)
-matrix.z = 1
-
-space = ismrmrd_xsd.encodingSpaceType()
-space.matrixSize = matrix
-space.fieldOfView_mm = fov
-
-# Set encoded and recon space (same)
-encoding.encodedSpace = space
-encoding.reconSpace = space
-
-# Encoding limits
-limits = ismrmrd_xsd.encodingLimitsType()
-
-limits0 = ismrmrd_xsd.limitType()
-limits0.minimum = 0
-limits0.center = np.size(K, 0) / 2
-limits0.maximum = np.size(K, 0) - 1
-limits.kspaceEncodingStep0 = limits0
-
-limits1 = ismrmrd_xsd.limitType()
-limits1.minimum = 0
-limits1.center = np.size(K, 1) / 2
-limits1.maximum = np.size(K, 1) - 1
-limits.kspaceEncodingStep1 = limits1
-
-limits_rep = ismrmrd_xsd.limitType()
-limits_rep.minimum = 0
-limits_rep.center = nreps / 2
-limits_rep.maximum = nreps - 1
-limits.repetition = limits_rep
-
-limits_slice = ismrmrd_xsd.limitType()
-limits_slice.minimum = 0
-limits_slice.center = 0
-limits_slice.maximum = 0
-limits.slice = limits_slice
-
-limits_rest = ismrmrd_xsd.limitType()
-limits_rest.minimum = 0
-limits_rest.center = 0
-limits_rest.maximum = 0
-limits.average = limits_rest
-limits.contrast = limits_rest
-limits.kspaceEncodingStep2 = limits_rest
-limits.phase = limits_rest
-limits.segment = limits_rest
-limits.set = limits_rest
-
-encoding.encodingLimits = limits
-header.encoding.append(encoding)
-
-dset.writeHeader(header.toxml('utf-8'))
-dset.close()
+try:
+    import ismrmrd_xsd
+    HAS_XSD = True
+except ImportError:
+    HAS_XSD = False
+
+if HAS_XSD:
+    header = ismrmrd_xsd.ismrmrdHeader()
+    
+    # Experimental Conditions
+    exp = ismrmrd_xsd.experimentalConditionsType()
+    exp.H1resonanceFrequency_Hz = 128000000
+    header.experimentalConditions = exp
+    
+    # Acquisition System Information
+    sys = ismrmrd_xsd.acquisitionSystemInformationType()
+    sys.receiverChannels = ncoils
+    header.acquisitionSystemInformation = sys
+    
+    # Encoding
+    encoding = ismrmrd_xsd.encoding()
+    encoding.trajectory = ismrmrd_xsd.trajectoryType.cartesian
+    
+    # Encoded Space
+    fov = ismrmrd_xsd.fieldOfView_mm()
+    fov.x = 256
+    fov.y = 256
+    fov.z = 5
+    
+    matrix = ismrmrd_xsd.matrixSize()
+    matrix.x = np.size(K, 0)
+    matrix.y = np.size(K, 1)
+    matrix.z = 1
+    
+    space = ismrmrd_xsd.encodingSpaceType()
+    space.matrixSize = matrix
+    space.fieldOfView_mm = fov
+    
+    # Set encoded and recon space (same)
+    encoding.encodedSpace = space
+    encoding.reconSpace = space
+    
+    # Encoding limits
+    limits = ismrmrd_xsd.encodingLimitsType()
+    
+    limits0 = ismrmrd_xsd.limitType()
+    limits0.minimum = 0
+    limits0.center = np.size(K, 0) / 2
+    limits0.maximum = np.size(K, 0) - 1
+    limits.kspaceEncodingStep0 = limits0
+    
+    limits1 = ismrmrd_xsd.limitType()
+    limits1.minimum = 0
+    limits1.center = np.size(K, 1) / 2
+    limits1.maximum = np.size(K, 1) - 1
+    limits.kspaceEncodingStep1 = limits1
+    
+    limits_rep = ismrmrd_xsd.limitType()
+    limits_rep.minimum = 0
+    limits_rep.center = nreps / 2
+    limits_rep.maximum = nreps - 1
+    limits.repetition = limits_rep
+    
+    limits_slice = ismrmrd_xsd.limitType()
+    limits_slice.minimum = 0
+    limits_slice.center = 0
+    limits_slice.maximum = 0
+    limits.slice = limits_slice
+    
+    limits_rest = ismrmrd_xsd.limitType()
+    limits_rest.minimum = 0
+    limits_rest.center = 0
+    limits_rest.maximum = 0
+    limits.average = limits_rest
+    limits.contrast = limits_rest
+    limits.kspaceEncodingStep2 = limits_rest
+    limits.phase = limits_rest
+    limits.segment = limits_rest
+    limits.set = limits_rest
+    
+    encoding.encodingLimits = limits
+    header.encoding.append(encoding)
+    
+    dset.write_header(header.toxml('utf-8'))
+
+dset.close()
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/ismrmrd.git



More information about the debian-science-commits mailing list