def send_reconstructed_images(connection, data_array,acq_header):
    # the fonction creates an new ImageHeader for each 4D dataset [RO,E1,E2,CHA]
    # copy information from the acquisitonHeader
    # fill additionnal fields
    # and send the reconstructed image and ImageHeader to the next gadget
    # some field are not correctly filled like image_type that floattofix point doesn't recognize , why ?
    
    dims=data_array.shape     

    base_header=ismrmrd.ImageHeader()
    base_header.version=2
    ndims_image=(dims[0], dims[1], dims[2], dims[3])
    base_header.channels = ndims_image[3]       
    base_header.matrix_size = (data_array.shape[0],data_array.shape[1],data_array.shape[2])    
    base_header.position = acq_header.position
    base_header.read_dir = acq_header.read_dir
    base_header.phase_dir = acq_header.phase_dir
    base_header.slice_dir = acq_header.slice_dir
    base_header.patient_table_position = acq_header.patient_table_position
    base_header.acquisition_time_stamp = acq_header.acquisition_time_stamp
    base_header.image_index = 0 
    base_header.image_series_index = 0
    base_header.data_type = ismrmrd.DATATYPE_CXFLOAT
    base_header.image_type= ismrmrd.IMTYPE_COMPLEX
    base_header.repetition=acq_header.idx.repetition
  
    I=np.zeros((dims[0], dims[1], dims[2], dims[3]))
    for slc in range(0, dims[6]):
             for n in range(0, dims[5]):
               for s in range(0, dims[4]):
                   I=data_array[:,:,:,:,s,n,slc]
                   base_header.image_type= ismrmrd.IMTYPE_COMPLEX
                   base_header.slice=slc              
                   image_array= ismrmrd.image.Image.from_array(I, headers=base_header)
                   connection.send(image_array)
Ejemplo n.º 2
0
    def process(self, recondata):

        print(np.shape(recondata[0].data.data))

        image = cifftn(recondata[0].data.data, axes=(0, 1, 2))
        image = np.reshape(
            image,
            (image.shape[0], image.shape[1], image.shape[2], image.shape[3]))

        #Create a new image header and transfer value
        acq = np.ravel(recondata[0].data.headers)[0]

        img_head = ismrmrd.ImageHeader()

        img_head.channels = acq.active_channels
        img_head.slice = acq.idx.slice
        img_head.matrix_size = (image.shape[0], image.shape[1], image.shape[2])
        img_head.position = acq.position
        img_head.read_dir = acq.read_dir
        img_head.phase_dir = acq.phase_dir
        img_head.slice_dir = acq.slice_dir
        img_head.patient_table_position = acq.patient_table_position
        img_head.acquisition_time_stamp = acq.acquisition_time_stamp
        img_head.image_index = 0
        img_head.image_series_index = 0
        img_head.data_type = ismrmrd.DATATYPE_CXFLOAT

        #Return image to Gadgetron
        self.put_next(img_head, image)
        print("Slice ", img_head.slice)
        print("----------------------------------------------")
        return 0
def create_array_of_image_header(image,acq,  dims_header):

        headers_list = []
        base_header=ismrmrd.ImageHeader()
        base_header.version=2
        ndims_image=np.shape(image)
        base_header.channels = ndims_image[3]       
        base_header.matrix_size = (image.shape[0],image.shape[1],image.shape[2])
        print((image.shape[0],image.shape[1],image.shape[2]))
        base_header.position = acq.position
        base_header.read_dir = acq.read_dir
        base_header.phase_dir = acq.phase_dir
        base_header.slice_dir = acq.slice_dir
        base_header.patient_table_position = acq.patient_table_position
        base_header.acquisition_time_stamp = acq.acquisition_time_stamp
        base_header.image_index = 0 
        base_header.image_series_index = 0
        base_header.data_type = ismrmrd.DATATYPE_CXFLOAT
        base_header.image_type= ismrmrd.IMTYPE_MAGNITUDE
        
        for slc in range(0, dims_header[4]):
           for n in range(0, dims_header[3]):
              for s in range(0, dims_header[2]):                 
                     headers_list.append(base_header)

        array_headers_test = np.array(headers_list,dtype=np.dtype(object)) 
        array_headers_test=np.reshape(array_headers_test, (dims_header[2], dims_header[3], dims_header[4])) 
        
        return array_headers_test
Ejemplo n.º 4
0
    def process(self, acq, data, *args):
        if self.myBuffer is None:
            channels = acq.active_channels
            if self.enc.encodingLimits.slice != None:
                nslices = self.enc.encodingLimits.slice.maximum + 1
            else:
                nslices = 1
            eNz = self.enc.encodedSpace.matrixSize.z
            eNy = self.enc.encodedSpace.matrixSize.y
            eNx = self.enc.encodedSpace.matrixSize.x

            self.myBuffer = np.zeros(
                (int(eNx / 2), eNy, eNz, nslices, channels),
                dtype=np.complex64)

        line_offset = self.enc.encodedSpace.matrixSize.y / 2 - self.enc.encodingLimits.kspace_encoding_step_1.center
        self.myBuffer[:,
                      int(acq.idx.kspace_encode_step_1 + line_offset),
                      int(acq.idx.kspace_encode_step_2),
                      int(acq.idx.slice), :] = data

        if (acq.flags & (1 << 7)):  #Is this the last scan in slice
            image = transform.transform_kspace_to_image(self.myBuffer,
                                                        dim=(0, 1, 2))
            image = image * np.product(
                image.shape) * 100  #Scaling for the scanner
            #Create a new image header and transfer value
            img_head = ismrmrd.ImageHeader()
            img_head.channels = acq.active_channels
            img_head.slice = acq.idx.slice
            img_head.matrix_size[0] = self.myBuffer.shape[0]
            img_head.matrix_size[1] = self.myBuffer.shape[1]
            img_head.matrix_size[2] = self.myBuffer.shape[2]
            img_head.position = acq.position
            img_head.read_dir = acq.read_dir
            img_head.phase_dir = acq.phase_dir
            img_head.slice_dir = acq.slice_dir
            img_head.patient_table_position = acq.patient_table_position
            img_head.acquisition_time_stamp = acq.acquisition_time_stamp
            img_head.image_index = self.myCounter
            img_head.image_series_index = self.mySeries
            img_head.data_type = ismrmrd.DATATYPE_CXFLOAT
            self.myCounter += 1
            if self.myCounter > 5:
                self.mySeries += 1
                self.myCounter = 1

            #Return image to Gadgetron
            self.put_next(img_head, image.astype('complex64'), *args)

        #print "Returning to Gadgetron"
        return 0  #Everything OK
Ejemplo n.º 5
0
def test_new_instance():
    img = ismrmrd.Image()
    eq_(type(img.getHead()), ismrmrd.ImageHeader)
    eq_(img.getHead().data_type, ismrmrd.DATATYPE_CXFLOAT)
    eq_(type(img.data), np.ndarray)
    eq_(img.data.dtype, np.complex64)

    attr = "this is a fake attribute string"
    head = ismrmrd.ImageHeader()
    head.attribute_string_len = len(attr)       # must set attribute_string_len
    head.data_type = ismrmrd.DATATYPE_CXFLOAT   # must set data_type
    img = ismrmrd.Image(head, attribute_string=attr)
    eq_(img.attribute_string, attr)
Ejemplo n.º 6
0
def test_set_head():
    img = ismrmrd.Image()
    nchan, nx, ny, nz = 8, 256, 128, 32
    head = ismrmrd.ImageHeader()
    head.data_type = ismrmrd.DATATYPE_CXDOUBLE
    head.channels = nchan
    head.matrix_size[0] = nx
    head.matrix_size[1] = ny
    head.matrix_size[2] = nz

    img.setHead(head)

    eq_(img.data.shape, (nchan, nz, ny, nx))
    eq_(img.data_type, ismrmrd.DATATYPE_CXDOUBLE)
    eq_(img.data.dtype, np.complex128)
Ejemplo n.º 7
0
def gen_image_to_send(header: ismrmrd.xsd.ismrmrdHeader,
                      acqs: typing.List[ismrmrd.Acquisition] = None,
                      connection: gadgetron.external.Connection = None):
    # target picture size, should get get from header in the future
    recon_matrix = header.encoding[0].reconSpace.matrixSize
    ic = 1  # channel
    iz = 1  # z size
    iy = recon_matrix.y  # height
    ix = recon_matrix.x  # width
    dpi = 100  # data per inch
    figure = plt.gcf()  # type: Figure
    figure.set_size_inches(ix / dpi, iy / dpi)

    print(rf'--------------------plot begin-----------------------')
    t = np.arange(0.0, 2.0, 0.01)
    s = np.sin(2 * np.pi * t)

    plt.plot(t, s)
    plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
    plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
    plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$', fontsize=20)
    plt.xlabel('time (s)')
    plt.ylabel('volts (mV)')

    plt.savefig("result.png", dpi=dpi)  # 3x100, 2x100
    print(rf'--------------------plot end-----------------------')

    h = ismrmrd.ImageHeader()  # type: ismrmrd.ImageHeader
    #h.data_type = ismrmrd.DATATYPE_USHORT
    h.data_type = ismrmrd.DATATYPE_FLOAT
    h.image_type = ismrmrd.IMTYPE_MAGNITUDE
    h.channels = ic
    h.matrix_size = (ix, iy, iz)

    i = ismrmrd.Image(h)

    gray_scale_img = PILImage.open('result.png').convert(
        'L')  # gray scale image uint 8
    img_data = np.array(gray_scale_img)

    #TODO how to stand this code?
    #i.data[:] = img_data[:, ::-1].data[:]
    i.data[:] = img_data.data[:]
    return i
Ejemplo n.º 8
0
    def process(self, recondata):

        print(np.shape(recondata[0].data.data))

        image = transform.transform_kspace_to_image(recondata[0].data.data,dim=(0,1,2))
        print(np.shape(image))
        #image = np.reshape(image,(image.shape[0],image.shape[1],image.shape[2],image.shape[3]))

        #Create a new image header and transfer value
        print(type(recondata[0].data.headers))
        print(np.shape(recondata[0].data.headers))
        acq = np.ravel(recondata[0].data.headers)[0]
        print(type(acq))

        print(acq.active_channels)
        print(acq.idx.slice)

        dims=np.shape(recondata[0].data.data)
        print(dims)

        for s in range(dims[6]):

          img_head = ismrmrd.ImageHeader()
          
          img_head.channels = acq.active_channels
          img_head.slice = s #acq.idx.slice
          img_head.matrix_size = (image.shape[0],image.shape[1],image.shape[2])
          img_head.position = acq.position
          img_head.read_dir = acq.read_dir
          img_head.phase_dir = acq.phase_dir
          img_head.slice_dir = acq.slice_dir
          img_head.patient_table_position = acq.patient_table_position
          img_head.acquisition_time_stamp = acq.acquisition_time_stamp
          img_head.image_index = 0 
          img_head.image_series_index = 0
          img_head.data_type = ismrmrd.DATATYPE_CXFLOAT

          #Return image to Gadgetron
          print(np.shape(image[:,:,:,:,0,0,s]))
          self.put_next(img_head,image[:,:,:,:,:,:,s])
          print("Slice ", img_head.slice)
          print("----------------------------------------------")
          return 0    
Ejemplo n.º 9
0
    def process(self, recondata):

        print(np.shape(recondata[0].data.data))

        image = cifftn(recondata[0].data.data, axes=(0, 1, 2))
        image = np.reshape(
            image,
            (image.shape[0], image.shape[1], image.shape[2], image.shape[3]))

        #Create a new image header and transfer value
        acq = np.ravel(recondata[0].data.headers)[0]

        img_head = ismrmrd.ImageHeader()
        img_head.version = 1
        img_head.measurement_uid = acq.measurement_uid
        img_head.channels = acq.active_channels
        img_head.slice = acq.idx.slice
        img_head.matrix_size = (image.shape[0], image.shape[1], image.shape[2])
        img_head.field_of_view = (self.enc.reconSpace.fieldOfView_mm.x,
                                  self.enc.reconSpace.fieldOfView_mm.y,
                                  self.enc.reconSpace.fieldOfView_mm.z)
        img_head.position = acq.position
        img_head.read_dir = acq.read_dir
        img_head.phase_dir = acq.phase_dir
        img_head.slice_dir = acq.slice_dir
        img_head.patient_table_position = acq.patient_table_position
        img_head.acquisition_time_stamp = acq.acquisition_time_stamp
        img_head.average = acq.idx.average
        img_head.slice = acq.idx.slice
        img_head.contrast = acq.idx.contrast
        img_head.phase = acq.idx.phase
        img_head.repetition = acq.idx.repetition
        img_head.set = acq.idx.set
        img_head.image_index = next(self.image_indices)
        img_head.image_series_index = 0
        img_head.data_type = ismrmrd.DATATYPE_CXFLOAT

        #Return image to Gadgetron
        self.put_next(img_head, image)
        print("Slice ", img_head.slice)
        print("----------------------------------------------")
        return 0
    def process(self, recondata):

        # receive kspace data and
        # extract acq_data and acq_header
        array_acq_headers = recondata[0].data.headers
        kspace_data = recondata[0].data.data

        try:
            if recondata[0].ref.data is not None:
                print("reference data exist")
                print(
                    np.shape(recondata[0].ref.data)
                )  # only for repetition 0 # il faut creer le bucket recon grappa
                reference = recondata[0].ref.data
                data = recondata[0].data.data
                print(np.shape(reference))
                print(np.shape(data))
                self.array_calib = reference
                np.save('/tmp/gadgetron/reference', reference)
                np.save('/tmp/gadgetron/data', data)
        except:
            print("reference data not exist")

        # grappa
        array_data = recondata[0].data.data
        dims = np.shape(recondata[0].data.data)

        kspace_data_tmp = np.ndarray(dims, dtype=np.complex64)

        for slc in range(0, dims[6]):
            for n in range(0, dims[5]):
                for s in range(0, dims[4]):

                    kspace = array_data[:, :, :, :, s, n, slc]
                    calib = self.array_calib[:, :, :, :, s, n, slc]

                    calib = np.squeeze(calib, axis=2)
                    kspace = np.squeeze(kspace, axis=2)

                    sx, sy, ncoils = kspace.shape[:]
                    cx, cy, ncoils = calib.shape[:]

                    # Here's the actual reconstruction
                    res = grappa(kspace,
                                 calib,
                                 kernel_size=(5, 5),
                                 coil_axis=-1)

                    # Here's the resulting shape of the reconstruction.  The coil
                    # axis will end up in the same place you provided it in
                    sx, sy, ncoils = res.shape[:]
                    kspace_data_tmp[:, :, 0, :, s, n, slc] = res

        # ifft, this is necessary for the next gadget
        #image = transform.transform_kspace_to_image(kspace_data_tmp,dim=(0,1,2))
        image = transform.transform_kspace_to_image(kspace_data_tmp,
                                                    dim=(0, 1, 2))

        # create a new IsmrmrdImageArray
        array_data = IsmrmrdImageArray()

        # attache the images to the IsmrmrdImageArray
        array_data.data = image

        # get dimension for the acq_headers
        dims_header = np.shape(recondata[0].data.headers)

        # get one header with typical info
        acq = np.ravel(array_acq_headers)[0]

        print("acq.idx.repetition", acq.idx.repetition)

        if (acq.idx.repetition == 0):
            np.save('/tmp/gadgetron/image', image)

        headers_list = []
        base_header = ismrmrd.ImageHeader()
        base_header.version = 2
        ndims_image = np.shape(image)
        base_header.channels = ndims_image[3]
        base_header.matrix_size = (image.shape[0], image.shape[1],
                                   image.shape[2])
        print((image.shape[0], image.shape[1], image.shape[2]))
        base_header.position = acq.position
        base_header.read_dir = acq.read_dir
        base_header.phase_dir = acq.phase_dir
        base_header.slice_dir = acq.slice_dir
        base_header.patient_table_position = acq.patient_table_position
        base_header.acquisition_time_stamp = acq.acquisition_time_stamp
        base_header.image_index = 0
        base_header.image_series_index = 0
        base_header.data_type = ismrmrd.DATATYPE_CXFLOAT
        base_header.image_type = ismrmrd.IMTYPE_MAGNITUDE
        print("ready to list")

        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    #for e2 in range(0, dims_header[1]):
                    #  for e1 in range(0, dims_header[0]):
                    headers_list.append(base_header)

        array_headers_test = np.array(headers_list, dtype=np.dtype(object))
        print(type(array_headers_test))
        print(np.shape(array_headers_test))
        array_headers_test = np.reshape(
            array_headers_test,
            (dims_header[2], dims_header[3], dims_header[4]))
        print(type(array_headers_test))
        print(np.shape(array_headers_test))

        print("---> ok 0")
        # how to copy acquisition header into image header in python  ?
        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    # for e2 in range(0, dims_header[1]):
                    # for e1 in range(0, dims_header[0]):
                    array_headers_test[s, n, slc].slice = slc
                    #print(s,n,slc)
                    #print(type(array_headers_test[s,n,slc]))
                    #print(array_headers_test[s,n,slc].slice)

        #print("---> ok 1")
        # print(np.shape(array_image_header))
        # attache the image headers to the IsmrmrdImageArray
        array_data.headers = array_headers_test

        #print("---> ok 2")
        # Return image to Gadgetron
        #print(np.shape(array_data.data))
        #print(np.shape(array_data.headers))

        #print(type(array_data.data))
        #print(type(array_data.headers))
        #print(type(array_data))

        # send the data to the next gadget

        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    #print("send out image %d-%d-%d" % (s, n, slc))
                    a = array_data.data[:, :, :, :, s, n, slc]
                    #array_data.headers[s,n,slc].slice=slc
                    #print(a.shape, array_data.headers[s,n,slc].slice)
                    self.put_next(array_data.headers[s, n, slc], a)

        #self.put_next( [IsmrmrdReconBit(array_data.headers, array_data.data)] ,  )

        print("----------------------------------------------")
        return 0
Ejemplo n.º 11
0
    def process(self, acq, data, *args):

        if self.buffer is None:
            # Matrix size
            eNx = self.enc.encodedSpace.matrixSize.x
            eNy = self.enc.encodedSpace.matrixSize.y
            eNz = self.enc.encodedSpace.matrixSize.z
            rNx = self.enc.reconSpace.matrixSize.x
            rNy = self.enc.reconSpace.matrixSize.y
            rNz = self.enc.reconSpace.matrixSize.z

            # Field of View
            eFOVx = self.enc.encodedSpace.fieldOfView_mm.x
            eFOVy = self.enc.encodedSpace.fieldOfView_mm.y
            eFOVz = self.enc.encodedSpace.fieldOfView_mm.z
            rFOVx = self.enc.reconSpace.fieldOfView_mm.x
            rFOVy = self.enc.reconSpace.fieldOfView_mm.y
            rFOVz = self.enc.reconSpace.fieldOfView_mm.z

            channels = acq.active_channels

            if data.shape[1] != rNx:
                raise (
                    "Error, Recon gadget expects data to be on correct matrix size in RO direction"
                )

            if (rNz != 1):
                rasie("Error Recon Gadget only supports 2D for now")

            self.buffer = np.zeros((channels, rNy, rNx), dtype=np.complex64)
            self.samp_mask = np.zeros(self.buffer.shape[1:])
            self.header_proto = ismrmrd.ImageHeader()
            self.header_proto.matrix_size[0] = rNx
            self.header_proto.matrix_size[1] = rNy
            self.header_proto.matrix_size[2] = rNz
            self.header_proto.field_of_view[0] = rFOVx
            self.header_proto.field_of_view[1] = rFOVy
            self.header_proto.field_of_view[0] = rFOVz

        #Now put data in buffer
        line_offset = self.buffer.shape[
            1] / 2 - self.enc.encodingLimits.kspace_encoding_step_1.center
        self.buffer[:, acq.idx.kspace_encode_step_1 + line_offset, :] = data
        self.samp_mask[acq.idx.kspace_encode_step_1 + line_offset, :] = 1

        #If last scan in buffer, do FFT and fill image header
        if acq.isFlagSet(ismrmrd.ACQ_LAST_IN_ENCODE_STEP1) or acq.isFlagSet(
                ismrmrd.ACQ_LAST_IN_SLICE):
            img_head = copy.deepcopy(self.header_proto)
            img_head.position = acq.position
            img_head.read_dir = acq.read_dir
            img_head.phase_dir = acq.phase_dir
            img_head.slice_dir = acq.slice_dir
            img_head.patient_table_position = acq.patient_table_position
            img_head.acquisition_time_stamp = acq.acquisition_time_stamp
            img_head.slice = acq.idx.slice
            img_head.channels = 1

            scale = self.samp_mask.size / (1.0 * np.sum(self.samp_mask[:]))

            #We have not yet calculated unmixing coefficients
            if self.unmix is None:
                self.calib_buffer.append((img_head, self.buffer.copy()))
                self.buffer[:] = 0
                self.samp_mask[:] = 0

                if len(self.calib_buffer) >= self.calib_frames:
                    cal_data = np.zeros(self.calib_buffer[0][1].shape,
                                        dtype=np.complex64)
                    for c in self.calib_buffer:
                        cal_data = cal_data + c[1]

                    mask = np.squeeze(np.sum(np.abs(cal_data), 0))
                    mask = np.ones(mask.shape) * (np.abs(mask) > 0.0)
                    target = None  #cal_data[0:8,:,:]

                    coil_images = transform.transform_kspace_to_image(cal_data,
                                                                      dim=(1,
                                                                           2))
                    (csm, rho) = coils.calculate_csm_walsh(coil_images)

                    if self.method == 'grappa':
                        self.unmix, self.gmap = grappa.calculate_grappa_unmixing(
                            cal_data,
                            self.acc_factor,
                            data_mask=mask,
                            kernel_size=(4, 5),
                            csm=csm)
                    elif self.method == 'sense':
                        self.unmix, self.gmap = sense.calculate_sense_unmixing(
                            self.acc_factor, csm)
                    else:
                        raise Exception('Unknown parallel imaging method: ' +
                                        str(self.method))

                    for c in self.calib_buffer:
                        recon = transform.transform_kspace_to_image(
                            c[1], dim=(1, 2)) * np.sqrt(scale)
                        recon = np.squeeze(np.sum(recon * self.unmix, 0))
                        self.put_next(c[0], recon, *args)

                return 0

            if self.unmix is None:
                raise Exception(
                    "We should never reach this point without unmixing coefficients"
                )

            recon = transform.transform_kspace_to_image(
                self.buffer, dim=(1, 2)) * np.sqrt(scale)
            recon = np.squeeze(np.sum(recon * self.unmix, 0))
            self.buffer[:] = 0
            self.samp_mask[:] = 0
            self.put_next(img_head, recon, *args)
        return 0
Ejemplo n.º 12
0
            im = np.squeeze(np.sqrt(np.sum(np.abs(im)**2, 3)))
            images.append(im)

l = len(images)
fig = plt.figure()
for n, im in enumerate(images):
    a = fig.add_subplot(1, 5, n)
    plt.imshow(im)
fig.set_size_inches(16, 4)

# grab the first acquisition for extra info
acqh = dset.readAcquisition(0).getHead()

for n, img in enumerate(images):
    hdr = ismrmrd.ImageHeader()
    hdr.acquisition_time_stamp = acqh.acquisition_time_stamp
    hdr.flags = 0
    hdr.measurement_uid = acqh.measurement_uid
    hdr.phase_dir = acqh.phase_dir
    hdr.physiology_time_stamp = acqh.physiology_time_stamp
    hdr.position = acqh.position
    hdr.read_dir = acqh.read_dir
    hdr.slice_dir = acqh.slice_dir
    hdr.channels = 1
    hdr.image_data_type = ismrmrd.DATA_FLOAT
    hdr.image_type = ismrmrd.TYPE_MAGNITUDE
    hdr.image_index = n
    hdr.slice = n

    dset.appendImageHeader(hdr, "image_%d.hdr" % n)
Ejemplo n.º 13
0
    def process(self, recondata):

        # receive kspace data and
        # extract acq_data and acq_header
        array_acq_headers = recondata[0].data.headers
        kspace_data = recondata[0].data.data

        try:
            if recondata[0].ref.data is not None:
                print("reference data exist")
                print(
                    np.shape(recondata[0].ref.data)
                )  # only for repetition 0 # il faut creer le bucket recon grappa
        except:
            print("reference data not exist")
        # ifft
        image = transform.transform_kspace_to_image(kspace_data, dim=(0, 1, 2))

        # create a new IsmrmrdImageArray
        array_data = IsmrmrdImageArray()

        # attache the images to the IsmrmrdImageArray
        array_data.data = image

        # get dimension for the acq_headers
        dims_header = np.shape(recondata[0].data.headers)

        # get one header with typical info
        acq = np.ravel(array_acq_headers)[0]

        headers_list = []
        base_header = ismrmrd.ImageHeader()
        base_header.version = 2
        ndims_image = np.shape(image)
        base_header.channels = ndims_image[3]
        base_header.matrix_size = (image.shape[0], image.shape[1],
                                   image.shape[2])
        print((image.shape[0], image.shape[1], image.shape[2]))
        base_header.position = acq.position
        base_header.read_dir = acq.read_dir
        base_header.phase_dir = acq.phase_dir
        base_header.slice_dir = acq.slice_dir
        base_header.patient_table_position = acq.patient_table_position
        base_header.acquisition_time_stamp = acq.acquisition_time_stamp
        base_header.image_index = 0
        base_header.image_series_index = 0
        base_header.data_type = ismrmrd.DATATYPE_CXFLOAT
        base_header.image_type = ismrmrd.IMTYPE_MAGNITUDE
        print("ready to list")

        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    #for e2 in range(0, dims_header[1]):
                    #  for e1 in range(0, dims_header[0]):
                    headers_list.append(base_header)

        array_headers_test = np.array(headers_list, dtype=np.dtype(object))
        print(type(array_headers_test))
        print(np.shape(array_headers_test))
        array_headers_test = np.reshape(
            array_headers_test,
            (dims_header[2], dims_header[3], dims_header[4]))
        print(type(array_headers_test))
        print(np.shape(array_headers_test))

        print("---> ok 0")
        # how to copy acquisition header into image header in python  ?
        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    # for e2 in range(0, dims_header[1]):
                    # for e1 in range(0, dims_header[0]):
                    array_headers_test[s, n, slc].slice = slc
                    #print(s,n,slc)
                    #print(type(array_headers_test[s,n,slc]))
                    #print(array_headers_test[s,n,slc].slice)

        #print("---> ok 1")
        # print(np.shape(array_image_header))
        # attache the image headers to the IsmrmrdImageArray
        array_data.headers = array_headers_test

        #print("---> ok 2")
        # Return image to Gadgetron
        print(np.shape(array_data.data))
        print(np.shape(array_data.headers))

        print(type(array_data.data))
        print(type(array_data.headers))
        print(type(array_data))

        # send the data to the next gadget

        for slc in range(0, dims_header[4]):
            for n in range(0, dims_header[3]):
                for s in range(0, dims_header[2]):
                    #print("send out image %d-%d-%d" % (s, n, slc))
                    a = array_data.data[:, :, :, :, s, n, slc]
                    #array_data.headers[s,n,slc].slice=slc
                    #print(a.shape, array_data.headers[s,n,slc].slice)
                    self.put_next(array_data.headers[s, n, slc], a)

        #self.put_next( [IsmrmrdReconBit(array_data.headers, array_data.data)] ,  )

        print("----------------------------------------------")
        return 0
Ejemplo n.º 14
0
def test_header():
    head = ismrmrd.ImageHeader()
    assert ctypes.sizeof(head) == 198