SIRF  3.4.0
gadgetron_x.h
Go to the documentation of this file.
1 /*
2 SyneRBI Synergistic Image Reconstruction Framework (SIRF)
3 Copyright 2015 - 2020 Rutherford Appleton Laboratory STFC
4 
5 This is software developed for the Collaborative Computational
6 Project in Synergistic Reconstruction for Biomedical Imaging (formerly CCP PETMR)
7 (http://www.ccpsynerbi.ac.uk/).
8 
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 */
20 
30 #ifndef GADGETRON_EXTENSIONS
31 #define GADGETRON_EXTENSIONS
32 
33 #define WIN32_LEAN_AND_MEAN
34 
35 #include <cmath>
36 #include <list>
37 #include <string>
38 
39 #include <ismrmrd/ismrmrd.h>
40 #include <ismrmrd/dataset.h>
41 
42 #include "sirf/Gadgetron/cgadgetron_shared_ptr.h"
44 
45 #include "sirf/common/JacobiCG.h"
46 #include "sirf/iUtilities/LocalisedException.h"
47 #include "sirf/Gadgetron/cgadgetron_shared_ptr.h"
49 
51 
52 #if GADGETRON_TOOLBOXES_AVAILABLE
54 #endif
55 
56 #define N_TRIALS 5
57 
58 namespace sirf {
59 
60  class GadgetronClientConnector;
61 
66  class GTConnector {
67  public:
68  GTConnector();
69  GadgetronClientConnector& operator()();
70  gadgetron::shared_ptr<GadgetronClientConnector> sptr();
71  private:
72  gadgetron::shared_ptr<GadgetronClientConnector> sptr_con_;
73  };
74 
81  class GadgetHandle {
82  public:
83  GadgetHandle(std::string id, gadgetron::shared_ptr<aGadget> sptr_g) :
84  id_(id), sptr_g_(sptr_g) {}
85  std::string id() const
86  {
87  return id_;
88  }
89  aGadget& gadget()
90  {
91  return *sptr_g_.get();
92  }
93  const aGadget& gadget() const
94  {
95  return *sptr_g_.get();
96  }
97  gadgetron::shared_ptr<aGadget> gadget_sptr()
98  {
99  return sptr_g_;
100  }
101  private:
102  std::string id_;
103  gadgetron::shared_ptr<aGadget> sptr_g_;
104  };
105 
134  class GadgetChain { //: public anObject {
135  public:
136  GadgetChain() : host_("localhost"), port_("9002")
137  {
138  //class_ = "GadgetChain";
139  }
140  static const char* class_name()
141  {
142  return "GadgetChain";
143  }
144  void set_host(const std::string host)
145  {
146  host_ = host;
147  }
148  void set_port(const std::string port)
149  {
150  port_ = port;
151  }
152  // apparently caused crash in linux
153  //virtual ~GadgetChain() {}
154  // adds reader gadget
155  void add_reader(std::string id, gadgetron::shared_ptr<aGadget> sptr_g)
156  {
157  readers_.push_back(gadgetron::shared_ptr<GadgetHandle>
158  (new GadgetHandle(id, sptr_g)));
159  }
160  // adds writer gadget
161  void add_writer(std::string id, gadgetron::shared_ptr<aGadget> sptr_g)
162  {
163  writers_.push_back(gadgetron::shared_ptr<GadgetHandle>
164  (new GadgetHandle(id, sptr_g)));
165  }
166  // sdds finishig gadget
167  void set_endgadget(gadgetron::shared_ptr<aGadget> sptr_g)
168  {
169  endgadget_ = sptr_g;
170  }
171  // adds any other gadget
172  void add_gadget(std::string id, gadgetron::shared_ptr<aGadget> sptr_g)
173  {
174  gadgets_.push_back(gadgetron::shared_ptr<GadgetHandle>
175  (new GadgetHandle(id, sptr_g)));
176  }
177  gadgetron::shared_ptr<aGadget> gadget_sptr(std::string id);
178  // returns string containing the definition of the chain in xml format
179  std::string xml() const;
180  protected:
181  std::string host_;
182  std::string port_;
183  private:
184  std::list<gadgetron::shared_ptr<GadgetHandle> > readers_;
185  std::list<gadgetron::shared_ptr<GadgetHandle> > writers_;
186  std::list<gadgetron::shared_ptr<GadgetHandle> > gadgets_;
187  gadgetron::shared_ptr<aGadget> endgadget_;
188  };
189 
198  public:
200  reader_(new IsmrmrdAcqMsgReader),
201  writer_(new IsmrmrdAcqMsgWriter)
202  {
203  //class_ = "AcquisitionsProcessor";
204  sptr_acqs_.reset();
205  add_reader("reader", reader_);
206  add_writer("writer", writer_);
207  }
208  // apparently caused crash in linux
209  //virtual ~AcquisitionsProcessor() {}
210  static const char* class_name()
211  {
212  return "AcquisitionsProcessor";
213  }
214 
215  void process(MRAcquisitionData& acquisitions);
216  gadgetron::shared_ptr<MRAcquisitionData> get_output()
217  {
218  if(!sptr_acqs_->sorted())
219  sptr_acqs_->sort_by_time();
220 
221  return sptr_acqs_;
222  }
223 
224  private:
225  gadgetron::shared_ptr<IsmrmrdAcqMsgReader> reader_;
226  gadgetron::shared_ptr<IsmrmrdAcqMsgWriter> writer_;
227  gadgetron::shared_ptr<MRAcquisitionData> sptr_acqs_;
228  };
229 
238  public:
239 
241  reader_(new IsmrmrdAcqMsgReader),
242  writer_(new IsmrmrdImgMsgWriter)
243  {
244  //class_ = "ImagesReconstructor";
245  sptr_images_.reset();
246  add_reader("reader", reader_);
247  add_writer("writer", writer_);
248  gadgetron::shared_ptr<ImageFinishGadget> endgadget(new ImageFinishGadget);
249  set_endgadget(endgadget);
250  }
251  static const char* class_name()
252  {
253  return "ImagesReconstructor";
254  }
255 
256  void process(MRAcquisitionData& acquisitions);
257  gadgetron::shared_ptr<GadgetronImageData> get_output()
258  {
259  return sptr_images_;
260  }
261 
262  private:
263  gadgetron::shared_ptr<IsmrmrdAcqMsgReader> reader_;
264  gadgetron::shared_ptr<IsmrmrdImgMsgWriter> writer_;
265  gadgetron::shared_ptr<GadgetronImageData> sptr_images_;
266  };
267 
275  class ImagesProcessor : public GadgetChain {
276  public:
277  ImagesProcessor(bool dicom = false, std::string prefix = "image") :
278  dicom_(dicom), prefix_(prefix),
279  reader_(new IsmrmrdImgMsgReader)
280  {
281  //class_ = "ImagesProcessor";
282  //gadgetron::shared_ptr<ImageFinishGadget> endgadget;
283  gadgetron::shared_ptr<aGadget> endgadget;
284  if (dicom) {
285  writer_.reset(new DicomImageMessageWriter);
286  endgadget.reset(new DicomFinishGadget);
287  }
288  else {
289  writer_.reset(new IsmrmrdImgMsgWriter);
290  endgadget.reset(new ImageFinishGadget);
291  }
292  add_reader("reader", reader_);
293  add_writer("writer", writer_);
294  set_endgadget(endgadget);
295  }
296  static const char* class_name()
297  {
298  return "ImagesProcessor";
299  }
300 
301  void check_connection();
302  void process(const GadgetronImageData& images);
303  gadgetron::shared_ptr<GadgetronImageData> get_output()
304  {
305  return sptr_images_;
306  }
307 
308  private:
309  bool dicom_;
310  std::string prefix_;
311  gadgetron::shared_ptr<IsmrmrdImgMsgReader> reader_;
312  gadgetron::shared_ptr<ImageMessageWriter> writer_;
313 // gadgetron::shared_ptr<IsmrmrdImgMsgWriter> writer_;
314  gadgetron::shared_ptr<GadgetronImageData> sptr_images_;
315  };
316 
345  public:
353  class BFOperator : public Operator<GadgetronImageData> {
354  public:
355  BFOperator(gadgetron::shared_ptr<MRAcquisitionModel> sptr_am) : sptr_am_(sptr_am) {}
356  virtual gadgetron::shared_ptr<GadgetronImageData>
357  apply(GadgetronImageData& image_data)
358  {
359  gadgetron::shared_ptr<MRAcquisitionData> sptr_fwd =
360  sptr_am_->fwd(image_data);
361  gadgetron::shared_ptr<GadgetronImageData> sptr_bwd =
362  sptr_am_->bwd(*sptr_fwd);
363  return sptr_bwd;
364  }
365  private:
366  gadgetron::shared_ptr<MRAcquisitionModel> sptr_am_;
367  };
368 
369  MRAcquisitionModel() {}
370  /*
371  The constructor records, by copying shared pointers, the two supplied
372  arguments as templates, to be used for obtaining scanner and image
373  discretisation data.
374  */
376  gadgetron::shared_ptr<MRAcquisitionData> sptr_ac,
377  gadgetron::shared_ptr<GadgetronImageData> sptr_ic
378  )
379  {
380  this->set_up(sptr_ac, sptr_ic);
381  }
383  gadgetron::shared_ptr<MRAcquisitionData> sptr_ac,
384  gadgetron::shared_ptr<GadgetronImageData> sptr_ic,
385  gadgetron::shared_ptr<CoilSensitivitiesVector> sptr_csms,
386  std::string acqs_info
387  ) : sptr_csms_(sptr_csms), acqs_info_(acqs_info)
388  {
389  this->set_up(sptr_ac, sptr_ic);
390  }
391 
400  float norm(int num_iter = 2, int verb = 0)
401  {
402  gadgetron::shared_ptr<MRAcquisitionModel> sptr_am
403  (new MRAcquisitionModel(sptr_acqs_, sptr_imgs_, sptr_csms_, acqs_info_));
404 
405  BFOperator bf(sptr_am);
407  jcg.set_num_iterations(num_iter);
408  gadgetron::unique_ptr<GadgetronImageData> sptr_id = sptr_imgs_->clone();
409  GadgetronImageData& image_data = *sptr_id;
410  image_data.fill(1.0);
411  float lmd = jcg.largest(bf, image_data, verb);
412  return std::sqrt(lmd);
413  }
414 
415  // make sure ic contains "true" images (and not e.g. G-factors)
416  void check_data_role(const GadgetronImageData& ic);
417 
418  // Records the acquisition template to be used.
419  void set_acquisition_template
420  (gadgetron::shared_ptr<MRAcquisitionData> sptr_ac)
421  {
422  sptr_acqs_ = sptr_ac;
423  }
424  // Records the image template to be used.
425  void set_image_template
426  (gadgetron::shared_ptr<GadgetronImageData> sptr_ic)
427  {
428  check_data_role(*sptr_ic);
429  sptr_imgs_ = sptr_ic;
430  }
431  // Returns shared pointer to the acquisitions template used.
432  gadgetron::shared_ptr<const MRAcquisitionData> acq_template_sptr() const
433  {
434  return sptr_acqs_;
435  }
436  // Returns shared pointer to the images template used.
437  gadgetron::shared_ptr<const GadgetronImageData> image_template_sptr() const
438  {
439  return sptr_imgs_;
440  }
441  // Records the coil sensitivities maps to be used.
442  void set_csm(gadgetron::shared_ptr<CoilSensitivitiesVector> sptr_csms)
443  {
444  sptr_csms_ = sptr_csms;
445  }
446 
447  void set_encoder(gadgetron::shared_ptr<sirf::FourierEncoding> sptr_enc)
448  {
449  sptr_enc_ = sptr_enc;
450  }
451 
452  // Records templates
453  void set_up(gadgetron::shared_ptr<MRAcquisitionData> sptr_ac,
454  gadgetron::shared_ptr<GadgetronImageData> sptr_ic);
455 
456  // Forward projects the whole ImageContainer using
457  // coil sensitivity maps in the second argument.
459  MRAcquisitionData& ac);
460 
461  // Backprojects the whole AcquisitionContainer using
462  // coil sensitivity maps in the second argument.
463  void bwd(GadgetronImageData& ic, const CoilSensitivitiesVector& cc,
464  const MRAcquisitionData& ac);
465 
466  // Forward projects the whole ImageContainer using
467  // coil sensitivity maps referred to by sptr_csms_.
468  gadgetron::shared_ptr<MRAcquisitionData> fwd(GadgetronImageData& ic)
469  {
470 
471  if (!sptr_acqs_.get())
472  throw LocalisedException
473  ("Acquisition data template not set", __FILE__, __LINE__);
474  if (!sptr_csms_.get() || sptr_csms_->items() < 1)
475  throw LocalisedException
476  ("Coil sensitivity maps not found", __FILE__, __LINE__);
477  check_data_role(ic);
478  gadgetron::unique_ptr<MRAcquisitionData> uptr_acqs =
479  sptr_acqs_->clone();
480 
481  fwd(ic, *sptr_csms_, *uptr_acqs);
482 
483  return gadgetron::shared_ptr<MRAcquisitionData>(std::move(uptr_acqs));
484  }
485 
486  // Backprojects the whole AcquisitionContainer using
487  // coil sensitivity maps referred to by sptr_csms_.
488  gadgetron::shared_ptr<GadgetronImageData> bwd(const MRAcquisitionData& ac)
489  {
490  if (!sptr_imgs_.get())
491  throw LocalisedException
492  ("image data template not set", __FILE__, __LINE__);
493  if (!sptr_csms_.get() || sptr_csms_->items() < 1)
494  throw LocalisedException
495  ("coil sensitivity maps not found", __FILE__, __LINE__);
496  gadgetron::shared_ptr<GadgetronImageData> sptr_imgs =
497  sptr_imgs_->new_images_container();
498  bwd(*sptr_imgs, *sptr_csms_, ac);
499  return sptr_imgs;
500  }
501 
502  private:
503  std::string acqs_info_;
504  gadgetron::shared_ptr<MRAcquisitionData> sptr_acqs_;
505  gadgetron::shared_ptr<GadgetronImageData> sptr_imgs_;
506  gadgetron::shared_ptr<CoilSensitivitiesVector> sptr_csms_;
507  gadgetron::shared_ptr<FourierEncoding> sptr_enc_;
508  };
509 
510 }
511 
512 #endif
Shared pointer wrap-up for the abstract gadget class aGadget.
Definition: gadgetron_x.h:81
Definition: LocalisedException.h:32
Class for GadgetIsmrmrdAcquisitionMessageWriter gadget xml-definition generator.
Definition: gadget_lib.h:139
A particular type of Gadget chain that has ImageData on input and output.
Definition: gadgetron_x.h:275
Abstract base class for a gadget xml-definition generator.
Definition: gadget_lib.h:45
A class for MR acquisition modelling.
Definition: gadgetron_x.h:344
File for cartesian fourier encoding and trajectory setting.
A coil sensitivities container based on the GadgetronImagesVector class.
Definition: gadgetron_data_containers.h:1261
Shared pointer wrap-up for GadgetronClientConnector.
Definition: gadgetron_x.h:66
Definition: Operator.h:7
Class for DicomImageWriter gadget xml-definition generator.
Definition: gadget_lib.h:217
Class for the product of backward and forward projectors of the MR acquisition model.
Definition: gadgetron_x.h:353
Definition: gadget_lib.h:647
Specification file for the library of SIRF generators of xml-definitions of Gadgetron gadgets...
Class for MRIImageReader gadget xml-definition generator.
Definition: gadget_lib.h:164
Definition: JacobiCG.h:37
Abstract data container.
Definition: GeometricalInfo.cpp:141
Class for MRIImageWriter gadget xml-definition generator.
Definition: gadget_lib.h:193
A particular type of Gadget chain that has AcquisitionData on input and output.
Definition: gadgetron_x.h:197
float norm(int num_iter=2, int verb=0)
Method computing the norm of the MR acquisition model operator A.
Definition: gadgetron_x.h:400
Specification file for data container classes for Gadgetron data.
Class for communicating with Gadgetron server.
Definition: gadgetron_client.h:292
Class for GadgetIsmrmrdAcquisitionMessageReader gadget xml-definition generator.
Definition: gadget_lib.h:113
Gadget chain class.
Definition: gadgetron_x.h:134
Definition: gadget_lib.h:658
A particular type of Gadget chain that has AcquisitionData on input and ImageData on output...
Definition: gadgetron_x.h:237
Abstract Gadgetron image data container class.
Definition: gadgetron_data_containers.h:632
Abstract MR acquisition data container class.
Definition: gadgetron_data_containers.h:216
Specification file for non-cartesian Fourier encoding.