int GadgetInstrumentationStreamController::put_ismrmrd_image_array(boost::python::object rec) { auto m1 = new GadgetContainerMessage<IsmrmrdImageArray>(boost::python::extract<IsmrmrdImageArray>(rec)()); if (stream_.put(m1) == -1) { GERROR("Failed to put IsmrmrdImageArray on stream, too long wait, %d\n", ACE_OS::last_error() == EWOULDBLOCK); m1->release(); return GADGET_FAIL; } return GADGET_OK; }
bool create_folder_with_all_permissions(const std::string& workingdirectory) { if ( !boost::filesystem::exists(workingdirectory) ) { boost::filesystem::path workingPath(workingdirectory); try { boost::filesystem::create_directories(workingPath); } catch (...) { GERROR("Error creating the working directory.\n"); return false; } // set the permission for the folder #ifdef _WIN32 try { boost::filesystem::permissions(workingPath, all_all); } catch(...) { GERROR("Error changing the permission of the working directory.\n"); return false; } #else // in case an older version of boost is used in non-win system // the system call is used int res = chmod(workingPath.string().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH); if ( res != 0 ) { GERROR("Error changing the permission of the working directory.\n"); return false; } #endif // _WIN32 } return true; }
int rwv_n_bytes(rwv_fun fun, int fd, struct iovec *vector, size_t count, int nbytes) { int i, n, nrw; struct iovec *v, *v_orig; int original_count = count; int total_bytes; assert(fd >= 0); assert(vector != NULL); assert(count > 0); /* I copy the vector, because I've to work on it */ v = calloc(count, sizeof(struct iovec)); assert(v != NULL); v_orig = v; int total = 0; for(i = 0; i < count; i ++) { v[i].iov_base = vector[i].iov_base; v[i].iov_len = vector[i].iov_len; total += v[i].iov_len; } total_bytes = nbytes; nrw = 0; while(nbytes > 0) { n = fun(fd, v, count); if( (n == -1) || (n == 0) ) { if(n == -1) { GERROR("function error: '%s'", strerror(errno)); } if(n == 0 || (n == -1 && errno != EINTR)) { break; } n = 0; } nrw += n; nbytes -= n; /* I've read/write all */ if(nrw == total_bytes) break; for(i = 0; i < count; i++) { if(v[i].iov_len <= n) n -= v[i].iov_len; else break; } v[i].iov_base = v[i].iov_base + n; v[i].iov_len -= n; v += i; count -= i; } GDEBUG(1, "[fd = %d; vector = %p; count = %d] read/write %d/%d bytes.", fd, vector, original_count, nrw, total_bytes); free(v_orig); return nrw; }
int GenericReconFieldOfViewAdjustmentGadget::process(Gadgetron::GadgetContainerMessage< IsmrmrdImageArray >* m1) { if (perform_timing.value()) { gt_timer_.start("GenericReconFieldOfViewAdjustmentGadget::process"); } GDEBUG_CONDITION_STREAM(verbose.value(), "GenericReconFieldOfViewAdjustmentGadget::process(...) starts ... "); process_called_times_++; IsmrmrdImageArray* recon_res_ = m1->getObjectPtr(); // print out recon info if (verbose.value()) { GDEBUG_STREAM("----> GenericReconFieldOfViewAdjustmentGadget::process(...) has been called " << process_called_times_ << " times ..."); std::stringstream os; recon_res_->data_.print(os); GDEBUG_STREAM(os.str()); } if (!debug_folder_full_path_.empty()) { gt_exporter_.export_array_complex(recon_res_->data_, debug_folder_full_path_ + "data_before_FOV_adjustment"); } // ---------------------------------------------------------- // FOV adjustment // ---------------------------------------------------------- GADGET_CHECK_RETURN(this->adjust_FOV(*recon_res_) == GADGET_OK, GADGET_FAIL); if (!debug_folder_full_path_.empty()) { gt_exporter_.export_array_complex(recon_res_->data_, debug_folder_full_path_ + "data_after_FOV_adjustment"); } // make sure the image header is consistent with data size_t N = recon_res_->headers_.get_number_of_elements(); for (size_t n = 0; n < N; n++) { recon_res_->headers_(n).matrix_size[0] = recon_res_->data_.get_size(0); recon_res_->headers_(n).matrix_size[1] = recon_res_->data_.get_size(1); recon_res_->headers_(n).matrix_size[2] = recon_res_->data_.get_size(2); } GDEBUG_CONDITION_STREAM(verbose.value(), "GenericReconFieldOfViewAdjustmentGadget::process(...) ends ... "); // ---------------------------------------------------------- // send out results // ---------------------------------------------------------- if (this->next()->putq(m1) == -1) { GERROR("GenericReconFieldOfViewAdjustmentGadget::process, passing data on to next gadget"); return GADGET_FAIL; } if (perform_timing.value()) { gt_timer_.stop(); } return GADGET_OK; }
int ImageFinishGadget::process(GadgetContainerMessage<ISMRMRD::ImageHeader>* m1) { if (!this->controller_) { GERROR("Cannot return result to controller, no controller set"); return -1; } GadgetContainerMessage<GadgetMessageIdentifier>* mb = new GadgetContainerMessage<GadgetMessageIdentifier>(); mb->getObjectPtr()->id = GADGET_MESSAGE_ISMRMRD_IMAGE; mb->cont(m1); int ret = this->controller_->output_ready(mb); if ((ret < 0)) { GERROR("Failed to return massage to controller\n"); return GADGET_FAIL; } return GADGET_OK; }
int GadgetInstrumentationStreamController::return_recondata(ACE_Message_Block* mb){ GILLock lock; auto m1 = AsContainerMessage<IsmrmrdReconData>(mb); try{ python_gadget_.attr("put_next")(*m1->getObjectPtr()); } catch(boost::python::error_already_set const &) { GERROR("Passing recondata on to python wrapper gadget failed\n"); std::string python_error = pyerr_to_string(); GDEBUG(python_error.c_str()); return GADGET_FAIL; } return GADGET_OK; }
/** * @brief CLogBackend::backend * @param w * @param revents * * 1.处理缓存的日志 * (1)日志超量需要写入文件 * (2)日志超时需要写入文件 * 2.从共享内存读取日志,try_lock失败一定次数后强制阻塞等待锁定 */ void CLogBackend::backend(ev::timer &w, int32 revents) { if ( EV_ERROR & revents ) { GERROR() << "backend error:" << strerror(errno) << "\n"; w.stop(); } CBackend::backend(); m_log_worker.read_shm_log(); //TODO 写入文件 }
int GadgetInstrumentationStreamController::put_config(const char* config) { size_t l = std::strlen(config); ACE_Message_Block* mb = new ACE_Message_Block(l+1); memcpy(mb->wr_ptr(),config,l+1); mb->wr_ptr(l+1); mb->set_flags(Gadget::GADGET_MESSAGE_CONFIG); if (stream_.put(mb) == -1) { GERROR("Failed to put configuration on stream, too long wait, %d\n", ACE_OS::last_error () == EWOULDBLOCK); mb->release(); return GADGET_FAIL; } return GADGET_OK; }
void CloudBus::send_node_info() { size_t buf_len = calculate_node_info_length(node_info_); try { char* buffer = new char[4+4+buf_len]; *((uint32_t*)buffer) = buf_len+4; *((uint32_t*)(buffer + 4)) = GADGETRON_CLOUDBUS_NODE_INFO; if (connected_) { serialize(node_info_,buffer + 8,buf_len); this->peer().send_n(buffer,buf_len+8); } delete [] buffer; } catch (...) { GERROR("Failed to send gadgetron node info\n"); throw; } print_nodes(); }
int DistributeGadget::process_config(ACE_Message_Block* m) { started_nodes_ = 0; node_parameters_ = std::string(m->rd_ptr()); //Grab the original XML conifguration std::string xml = controller_->get_xml_configuration(); GadgetronXML::GadgetStreamConfiguration cfg; GadgetronXML::deserialize(xml.c_str(),cfg); //Delete Gadgets up to this Gadget std::vector<GadgetronXML::Gadget>::iterator it = cfg.gadget.begin(); while ((it->name != std::string(this->module()->name())) && (it != cfg.gadget.end())) it++; it++; cfg.gadget.erase(cfg.gadget.begin(),it); //Delete Gadgets after collector it = cfg.gadget.begin(); while ((it->name != collector.value()) && (it != cfg.gadget.end())) it++; it++; cfg.gadget.erase(it,cfg.gadget.end()); std::stringstream o; GadgetronXML::serialize(cfg,o); node_xml_config_ = o.str(); Gadget* tmp = this; while (tmp->next()) { if (std::string(tmp->module()->name()) == collector.value()) break; tmp = dynamic_cast<Gadget*>(tmp->next()); } collect_gadget_ = tmp; if (!collect_gadget_) { GERROR("Failed to locate collector Gadget with name %s\n", collector.value().c_str()); return GADGET_FAIL; } else { collect_gadget_->set_parameter("pass_through_mode","true"); } return GADGET_OK; }
int CloudBusRelayAcceptor::handle_input (ACE_HANDLE fd) { CloudBusNodeController *controller; ACE_NEW_RETURN (controller, CloudBusNodeController, -1); auto_ptr<CloudBusNodeController> p (controller); controller->set_acceptor(this); if (this->acceptor_.accept (controller->peer ()) == -1) { GERROR("Failed to accept controller connection\n"); return -1; } p.release (); controller->reactor (this->reactor ()); if (controller->open () == -1) controller->handle_close (ACE_INVALID_HANDLE, 0); return 0; }
int CloudBus::svc(void) { while (true) { if (connected_) { // } else { std::string connect_addr(relay_inet_addr_); if (connect_addr == "localhost") { connect_addr = node_info_.address; } ACE_INET_Addr server(relay_port_,connect_addr.c_str()); ACE_SOCK_Connector connector; if (connector.connect(this->peer(),server) == 0) { ACE_TCHAR peer_name[MAXHOSTNAMELENGTH]; ACE_INET_Addr peer_addr; if ((this->peer().get_remote_addr (peer_addr) == 0) && (peer_addr.addr_to_string (peer_name, MAXHOSTNAMELENGTH) == 0)) { GDEBUG("CloudBus connected to relay at %s\n", peer_name); if (this->reactor ()->register_handler(this, ACE_Event_Handler::READ_MASK) != 0) { GERROR("Failed to register read handler\n"); return -1; } mtx_.acquire(); connected_ = true; mtx_.release(); if (!query_mode_) { send_node_info(); } } } } //Sleep for 5 seconds ACE_Time_Value tv (5); ACE_OS::sleep (tv); } return 0; }
int GenericReconPartialFourierHandlingGadget::process(Gadgetron::GadgetContainerMessage< IsmrmrdImageArray >* m1) { GDEBUG_CONDITION_STREAM(verbose.value(), "GenericReconPartialFourierHandlingGadget::process(...) starts ... "); process_called_times_++; IsmrmrdImageArray* recon_res_ = m1->getObjectPtr(); // print out recon info if (verbose.value()) { GDEBUG_STREAM("----> GenericReconPartialFourierHandlingGadget::process(...) has been called " << process_called_times_ << " times ..."); std::stringstream os; recon_res_->data_.print(os); GDEBUG_STREAM(os.str()); } // some images do not need partial fourier handling processing if (recon_res_->meta_[0].length(skip_processing_meta_field.value().c_str())>0) { if (this->next()->putq(m1) == -1) { GERROR("GenericReconPartialFourierHandlingGadget::process, passing incoming image array on to next gadget"); return GADGET_FAIL; } return GADGET_OK; } // call the partial foureir size_t encoding = (size_t)recon_res_->meta_[0].as_long("encoding", 0); GADGET_CHECK_RETURN(encoding<num_encoding_spaces_, GADGET_FAIL); // perform SNR unit scaling SamplingLimit sampling_limits[3]; sampling_limits[0].min_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_RO", 0); sampling_limits[0].center_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_RO", 1); sampling_limits[0].max_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_RO", 2); sampling_limits[1].min_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E1", 0); sampling_limits[1].center_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E1", 1); sampling_limits[1].max_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E1", 2); sampling_limits[2].min_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E2", 0); sampling_limits[2].center_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E2", 1); sampling_limits[2].max_ = (uint16_t)recon_res_->meta_[0].as_long("sampling_limits_E2", 2); size_t RO = recon_res_->data_.get_size(0); size_t E1 = recon_res_->data_.get_size(1); size_t E2 = recon_res_->data_.get_size(2); size_t CHA = recon_res_->data_.get_size(3); size_t N = recon_res_->data_.get_size(4); size_t S = recon_res_->data_.get_size(5); size_t SLC = recon_res_->data_.get_size(6); // ---------------------------------------------------------- // pf kspace sampling range // ---------------------------------------------------------- // if image padding is performed, those dimension may not need partial fourier handling startRO_ = sampling_limits[0].min_; endRO_ = sampling_limits[0].max_; startE1_ = 0; endE1_ = E1 - 1; startE2_ = 0; endE2_ = E2 - 1; if (std::abs((double)(sampling_limits[1].max_ - E1 / 2) - (double)(E1 / 2 - sampling_limits[1].min_)) > acceFactorE1_[encoding]) { startE1_ = sampling_limits[1].min_; endE1_ = sampling_limits[1].max_; } if ((E2>1) && (std::abs((double)(sampling_limits[2].max_ - E2 / 2) - (double)(E2 / 2 - sampling_limits[2].min_)) > acceFactorE2_[encoding])) { startE2_ = sampling_limits[2].min_; endE2_ = sampling_limits[2].max_; } long lenRO = endRO_ - startRO_ + 1; long lenE1 = endE1_ - startE1_ + 1; long lenE2 = endE2_ - startE2_ + 1; if (lenRO == RO && lenE1 == E1 && lenE2 == E2) { GDEBUG_CONDITION_STREAM(verbose.value(), "lenRO == RO && lenE1 == E1 && lenE2 == E2"); if (this->next()->putq(m1) == -1) { GERROR("GenericReconPartialFourierHandlingGadget::process, passing data on to next gadget"); return GADGET_FAIL; } return GADGET_OK; } // ---------------------------------------------------------- // go to kspace // ---------------------------------------------------------- if (E2 > 1) { Gadgetron::hoNDFFT<typename realType<T>::Type>::instance()->fft3c(recon_res_->data_, kspace_buf_); } else { Gadgetron::hoNDFFT<typename realType<T>::Type>::instance()->fft2c(recon_res_->data_, kspace_buf_); } /*if (!debug_folder_full_path_.empty()) { gt_exporter_.exportArrayComplex(kspace_buf_, debug_folder_full_path_ + "kspace_before_pf"); }*/ // ---------------------------------------------------------- // pf handling // ---------------------------------------------------------- GADGET_CHECK_RETURN(this->perform_partial_fourier_handling() == GADGET_OK, GADGET_FAIL); /*if (!debug_folder_full_path_.empty()) { gt_exporter_.exportArrayComplex(pf_res_, debug_folder_full_path_ + "kspace_after_pf"); }*/ // ---------------------------------------------------------- // go back to image domain // ---------------------------------------------------------- if (E2 > 1) { Gadgetron::hoNDFFT<typename realType<T>::Type>::instance()->ifft3c(pf_res_, recon_res_->data_); } else { Gadgetron::hoNDFFT<typename realType<T>::Type>::instance()->ifft2c(pf_res_, recon_res_->data_); } /*if (!debug_folder_full_path_.empty()) { gt_exporter_.exportArrayComplex(recon_res_->data_, debug_folder_full_path_ + "data_after_pf"); }*/ GDEBUG_CONDITION_STREAM(verbose.value(), "GenericReconPartialFourierHandlingGadget::process(...) ends ... "); // ---------------------------------------------------------- // send out results // ---------------------------------------------------------- if (this->next()->putq(m1) == -1) { GERROR("GenericReconPartialFourierHandlingGadget::process, passing data on to next gadget"); return GADGET_FAIL; } return GADGET_OK; }
int PartialFourierAdjustROGadget ::process(GadgetContainerMessage<ISMRMRD::AcquisitionHeader>* m1, GadgetContainerMessage< hoNDArray< std::complex<float> > >* m2) { bool is_noise = ISMRMRD::FlagBit(ISMRMRD::ISMRMRD_ACQ_IS_NOISE_MEASUREMENT).isSet(m1->getObjectPtr()->flags); size_t channels = m1->getObjectPtr()->active_channels; size_t samples = m1->getObjectPtr()->number_of_samples; size_t centre_column = m1->getObjectPtr()->center_sample; if (!is_noise) { // adjust the center echo int az = addPrePostZeros(centre_column, samples); if ( az!= 0 && samples < maxRO_ ) { GadgetContainerMessage< hoNDArray< std::complex<float> > >* m3 = new GadgetContainerMessage< hoNDArray< std::complex<float> > >(); if (!m3) { return GADGET_FAIL; } std::vector<size_t> data_out_dims = *m2->getObjectPtr()->get_dimensions(); data_out_dims[0] = maxRO_; try { m3->getObjectPtr()->create(&data_out_dims); } catch(...) { GDEBUG("Unable to create new data array for downsampled data\n"); return GADGET_FAIL; } m3->getObjectPtr()->fill(0); std::complex<float>* pM3 = m3->getObjectPtr()->get_data_ptr(); std::complex<float>* pM2 = m2->getObjectPtr()->get_data_ptr(); size_t c; if ( az == 1 ) // pre zeros { for ( c=0; c<channels; c++ ) { memcpy(pM3+c*maxRO_+maxRO_-samples, pM2+c*samples, sizeof( std::complex<float> )*samples); } } if ( az == 2 ) // post zeros { for ( c=0; c<channels; c++ ) { memcpy(pM3+c*maxRO_, pM2+c*samples, sizeof( std::complex<float> )*samples); } } m2->release(); //We are done with this data m1->cont(m3); m1->getObjectPtr()->number_of_samples = data_out_dims[0]; } if (this->next()->putq(m1) == -1) { GERROR("NoiseAdjustGadget::process, passing data on to next gadget"); return -1; } } else { if (this->next()->putq(m1) == -1) { GERROR("NoiseAdjustGadget::process, passing data on to next gadget"); return -1; } } return GADGET_OK; }
int IsmrmrdDumpGadget::process_config(ACE_Message_Block* mb) { ISMRMRD::IsmrmrdHeader ismrmrd_header; ISMRMRD::deserialize(mb->rd_ptr(), ismrmrd_header); std::string measurement_id = ""; std::string ismrmrd_filename = ""; if ( ismrmrd_header.measurementInformation ) { if ( ismrmrd_header.measurementInformation->measurementID ) { measurement_id = *ismrmrd_header.measurementInformation->measurementID; } } GDEBUG("Measurement ID: %s\n", measurement_id.c_str()); bf::path p(folder.value()); if (!exists(p)) { try { bf::create_directory(p); } catch(...) { GERROR("Error caught trying to create folder %s\n", folder.value().c_str()); return GADGET_FAIL; } } else { if (!is_directory(p)) { GERROR("Specified path is not a directory\n"); return GADGET_FAIL; } } if ( file_prefix.value().empty() ) { ismrmrd_filename = "ISMRMRD_DUMP"; } else { ismrmrd_filename = file_prefix.value(); } if (append_id.value() && measurement_id.size()) { ismrmrd_filename.append("_"); ismrmrd_filename.append(measurement_id); } //Generate filename if (append_timestamp.value()) { ismrmrd_filename.append("_"); ismrmrd_filename.append(get_date_time_string()); } ismrmrd_filename.append(".h5"); p /= ismrmrd_filename; ismrmrd_filename = p.string(); ismrmrd_dataset_ = boost::shared_ptr<ISMRMRD::Dataset>(new ISMRMRD::Dataset(ismrmrd_filename.c_str(), "dataset")); std::string xml_config(mb->rd_ptr()); try { ismrmrd_dataset_->writeHeader(xml_config); } catch (...) { GDEBUG("Failed to write XML header to HDF file\n"); return GADGET_FAIL; } return GADGET_OK; }
int DistributeGadget::process(ACE_Message_Block* m) { int node_index = this->node_index(m); if (single_package_mode.value()) { node_index = ++started_nodes_; } if (node_index < 0) { GERROR("Negative node index received"); return GADGET_FAIL; } //If we are not supposed to use this node for compute, add one to make sure we are not on node 0 if (!use_this_node_for_compute.value()) { node_index = node_index+1; } if (node_index == 0) { //process locally if (this->next()->putq(m) == -1) { m->release(); GERROR("DistributeGadget::process, passing data on to next gadget\n"); return GADGET_FAIL; } return GADGET_OK; } //At this point, the node index is positive, so we need to find a suitable connector. mtx_.acquire(); auto n = node_map_.find(node_index); mtx_.release(); GadgetronConnector* con = 0; if (n != node_map_.end()) { //We have a suitable connection already. con = n->second; } else { std::vector<GadgetronNodeInfo> nl; CloudBus::instance()->get_node_info(nl); GDEBUG("Number of network nodes found: %d\n", nl.size()); GadgetronNodeInfo me; me.address = "127.0.0.1";//We may have to update this me.port = CloudBus::instance()->port(); me.uuid = CloudBus::instance()->uuid(); me.active_reconstructions = CloudBus::instance()->active_reconstructions(); //This would give the current node the lowest possible priority if (!use_this_node_for_compute.value()) { me.active_reconstructions = UINT32_MAX; } for (auto it = nl.begin(); it != nl.end(); it++) { if (it->active_reconstructions < me.active_reconstructions) { me = *it; } //Is this a free node if (me.active_reconstructions == 0) break; } con = new DistributionConnector(this); GadgetronXML::GadgetStreamConfiguration cfg; try { deserialize(node_xml_config_.c_str(), cfg); } catch (const std::runtime_error& e) { GERROR("Failed to parse Node Gadget Stream Configuration: %s\n", e.what()); return GADGET_FAIL; } //Configuration of readers for (auto i = cfg.reader.begin(); i != cfg.reader.end(); ++i) { GadgetMessageReader* r = controller_->load_dll_component<GadgetMessageReader>(i->dll.c_str(), i->classname.c_str()); if (!r) { GERROR("Failed to load GadgetMessageReader from DLL\n"); return GADGET_FAIL; } con->register_reader(i->slot, r); } for (auto i = cfg.writer.begin(); i != cfg.writer.end(); ++i) { GadgetMessageWriter* w = controller_->load_dll_component<GadgetMessageWriter>(i->dll.c_str(), i->classname.c_str()); if (!w) { GERROR("Failed to load GadgetMessageWriter from DLL\n"); return GADGET_FAIL; } con->register_writer(i->slot, w); } char buffer[10]; sprintf(buffer,"%d",me.port); if (con->open(me.address,std::string(buffer)) != 0) { GERROR("Failed to open connection to node %s : %d\n", me.address.c_str(), me.port); return GADGET_FAIL; } if (con->send_gadgetron_configuration_script(node_xml_config_) != 0) { GERROR("Failed to send XML configuration to compute node\n"); return GADGET_FAIL; } if (con->send_gadgetron_parameters(node_parameters_) != 0) { GERROR("Failed to send XML parameters to compute node\n"); return GADGET_FAIL; } mtx_.acquire(); node_map_[node_index] = con; mtx_.release(); } if (!con) { //Zero pointer for the connection means that either a) connection creation failed or b) using local chain. //Either way, we will send it down the chain. if (!use_this_node_for_compute.value()) { GERROR("This node cannot be used for computing and no other node is available\n"); m->release(); return GADGET_FAIL; } if (this->next()->putq(m) == -1) { m->release(); GERROR("DistributeGadget::process, passing data on to next gadget\n"); return GADGET_FAIL; } else { return GADGET_OK; } } else { //Let's make sure that we did not send a close message to this connector already auto c = std::find(closed_connectors_.begin(),closed_connectors_.end(),con); if (c != closed_connectors_.end()) { //This is a bad situation, we need to bail out. m->release(); GERROR("The valid connection for incoming data has already been closed. Distribute Gadget is not configured properly for this type of data\n"); return GADGET_FAIL; } //If nodes receive their data sequentially (default), we should see if we should be closing the previos connection if (nodes_used_sequentially.value() && !single_package_mode.value()) { //Is this a new connection, if so, send previous one a close if (prev_connector_ && prev_connector_ != con) { GDEBUG("Sending close to previous connector, not expecting any more data for this one\n"); auto mc = new GadgetContainerMessage<GadgetMessageIdentifier>(); mc->getObjectPtr()->id = GADGET_MESSAGE_CLOSE; if (prev_connector_->putq(mc) == -1) { GERROR("Unable to put CLOSE package on queue of previous connection\n"); return -1; } closed_connectors_.push_back(prev_connector_); } } //Update previous connection prev_connector_ = con; //We have a valid connector auto m1 = new GadgetContainerMessage<GadgetMessageIdentifier>(); m1->getObjectPtr()->id = message_id(m); m1->cont(m); if (con->putq(m1) == -1) { GERROR("Unable to put package on connector queue\n"); m1->release(); return GADGET_FAIL; } if (single_package_mode.value()) { auto m2 = new GadgetContainerMessage<GadgetMessageIdentifier>(); m2->getObjectPtr()->id = GADGET_MESSAGE_CLOSE; if (con->putq(m2) == -1) { GERROR("Unable to put CLOSE package on queue\n"); return -1; } closed_connectors_.push_back(con); } } return 0; }
static epoch_t checkfun(int type, void *arg) { PyObject *pKw = PyDict_New(); PyObject *pArg; PyObject *pRetVal; epoch_t retval = -1; struct binfmt_req *bf; switch(type) { case CHECKPATH: pArg = PyString_FromString((char*)arg); PyDict_SetItemString(pKw, "path", pArg); break; case CHECKSOCKET: pArg = PyInt_FromLong((long)arg); PyDict_SetItemString(pKw, "socket", pArg); break; case CHECKFSTYPE: pArg = PyString_FromString((char*)arg); PyDict_SetItemString(pKw, "fstype", pArg); break; case CHECKSC: pArg = PyInt_FromLong(*((long*)arg)); PyDict_SetItemString(pKw, "sc", pArg); break; case CHECKBINFMT: bf = (struct binfmt_req*) arg; pArg = PyTuple_New(3); if (bf->path) PyTuple_SET_ITEM(pArg, 0, PyString_FromString(bf->path)); if (bf->interp) PyTuple_SET_ITEM(pArg, 1, PyString_FromString(bf->interp)); PyTuple_SET_ITEM(pArg, 2, PyInt_FromLong(bf->flags)); PyDict_SetItemString(pKw, "binfmt", pArg); break; default: GERROR("Unknown check type %d", type); retval = 0; break; } if (!retval) { Py_DECREF(pKw); return retval; } pRetVal = PyObject_Call(ps.checkfun, pEmptyTuple, pKw); if (!pRetVal) { PyErr_Print(); return 0; } retval = PyInt_AsLong(pRetVal); Py_DECREF(pArg); Py_DECREF(pKw); Py_DECREF(pRetVal); return retval; }
int WhiteNoiseInjectorGadget::process(GadgetContainerMessage<ISMRMRD::AcquisitionHeader>* m1, GadgetContainerMessage< hoNDArray< std::complex<float> > >* m2) { bool is_noise = ISMRMRD::FlagBit(ISMRMRD::ISMRMRD_ACQ_IS_NOISE_MEASUREMENT).isSet(m1->getObjectPtr()->flags); bool is_scc_correction = ISMRMRD::FlagBit(ISMRMRD::ISMRMRD_ACQ_IS_SURFACECOILCORRECTIONSCAN_DATA).isSet(m1->getObjectPtr()->flags); bool is_ref = ISMRMRD::FlagBit(ISMRMRD::ISMRMRD_ACQ_IS_PARALLEL_CALIBRATION).isSet(m1->getObjectPtr()->flags); bool is_ref_kspace = ISMRMRD::FlagBit(ISMRMRD::ISMRMRD_ACQ_IS_PARALLEL_CALIBRATION_AND_IMAGING).isSet(m1->getObjectPtr()->flags); size_t channels = m1->getObjectPtr()->active_channels; size_t samples = m1->getObjectPtr()->number_of_samples; if (!is_noise && !is_scc_correction ) { bool add_noise = true; if ( is_ref && !is_ref_kspace && (is_seperate_||is_external_) ) { add_noise = add_noise_ref_; if ( !add_noise ) { GDEBUG_STREAM("WhiteNoiseInjectorGadget, noise is not added to the ref acquisitions ... "); } } if ( add_noise ) { if ( !noise_.dimensions_equal(m2->getObjectPtr()) ) { noise_.create(m2->getObjectPtr()->get_dimensions()); noise_fl_.create(m2->getObjectPtr()->get_dimensions()); } if ( !randn_->gen(noise_) ) { GERROR_STREAM("WhiteNoiseInjectorGadget, randn_->gen(noise_) failed ... "); return GADGET_FAIL; } if ( !noise_fl_.copyFrom(noise_) ) { GERROR_STREAM("WhiteNoiseInjectorGadget, noise_fl_.copyFrom(noise_) failed ... "); return GADGET_FAIL; } try { Gadgetron::add(*m2->getObjectPtr(), noise_fl_, *m2->getObjectPtr()); } catch(...) { GERROR_STREAM("WhiteNoiseInjectorGadget, Gadgetron::add(*m2->getObjectPtr(), noise_, *m2->getObjectPtr()) failed ... "); return GADGET_FAIL; } } } if (this->next()->putq(m1) == -1) { GERROR("WhiteNoiseInjectorGadget::process, passing data on to next gadget"); return -1; } return GADGET_OK; }
int MatlabBufferGadget::process(GadgetContainerMessage<IsmrmrdReconData>* m1) { // Initialize a string for matlab commands std::string cmd; auto recon_data = m1->getObjectPtr(); mwSize nencoding_spaces = recon_data->rbit_.size(); const char* fieldnames[2] = {"data","reference"}; auto reconArray = mxCreateStructArray(1,&nencoding_spaces,2,fieldnames); //auto reconArray = mxCreateCellArray(1,&nencoding_spaces); for (int i = 0; i < recon_data->rbit_.size(); i++){ auto mxrecon = BufferToMatlabStruct(&recon_data->rbit_[i].data_); mxSetField(reconArray,i,"data",mxrecon); if (recon_data->rbit_[i].ref_){ auto mxref = BufferToMatlabStruct(recon_data->rbit_[i].ref_.get_ptr()); mxSetField(reconArray,i,"reference",mxref); } } engPutVariable(engine_, "recon_data", reconArray); cmd = "[imageQ,bufferQ] = matgadget.run_process(recon_data); matgadget.emptyQ(); whos()"; send_matlab_command(cmd); // Get the size of the gadget's queue mxArray *imageQ = engGetVariable(engine_, "imageQ"); if (imageQ == NULL) { GERROR("Failed to get the imageQ from matgadget\n"); return GADGET_FAIL; } size_t qlen = mxGetNumberOfElements(imageQ); GDEBUG("Image Queue size: %d \n", qlen); const mwSize* dims = mxGetDimensions(imageQ); mwSize ndims = mxGetNumberOfDimensions(imageQ); //Read all Image bytes for (mwIndex idx = 0; idx < qlen; idx++) { mxArray *res_hdr = mxGetField(imageQ, idx, "bytes"); mxArray *res_data = mxGetField(imageQ, idx, "image"); GadgetContainerMessage<ISMRMRD::ImageHeader>* m3 = new GadgetContainerMessage<ISMRMRD::ImageHeader>(); ISMRMRD::ImageHeader *hdr_new = m3->getObjectPtr(); memcpy(hdr_new, mxGetData(res_hdr), sizeof(ISMRMRD::ImageHeader)); auto image= MatlabToHoNDArray<std::complex<float>>(res_data); auto m4 = new GadgetContainerMessage< hoNDArray< std::complex<float> > >(image); auto dims = *image->get_dimensions(); delete image; m3->cont(m4); if (this->next()->putq(m3) < 0) { GDEBUG("Failed to put Image message on queue\n"); return GADGET_FAIL; } } //Match engGetVariable with mxDestroy___s mxArray* bufferQ = engGetVariable(engine_,"bufferQ"); qlen = mxGetNumberOfElements(bufferQ); GDEBUG("Buffer Queue size: %d \n", qlen); for (mwIndex idx = 0; idx <qlen; idx++){ IsmrmrdReconData output_data; IsmrmrdReconBit bit; bit.data_ = MatlabStructToBuffer(mxGetField(bufferQ,idx,"data")); auto ref = mxGetField(bufferQ,idx,"reference"); if (ref){ GDEBUG("Adding reference"); bit.ref_ = MatlabStructToBuffer(ref); } output_data.rbit_.push_back(bit); auto m3 = new GadgetContainerMessage<IsmrmrdReconData>(output_data.rbit_); if (this->next()->putq(m3) < 0){ GDEBUG("Failed to put Buffer message on queue\n"); return GADGET_FAIL; } } mxDestroyArray(bufferQ); mxDestroyArray(imageQ); //mxDestroyArray(reconArray); //We're not supposed to delete this? // We are finished with the incoming messages m1 and m2 m1->release(); return GADGET_OK; }
int AddMetaData::process(GadgetContainerMessage<DcmFileFormat> * m1) { DcmFileFormat * dcm = m1->getObjectPtr(); GadgetContainerMessage<std::string> * f; GadgetContainerMessage<ISMRMRD::MetaContainer>* meta; if(dcm) { f=AsContainerMessage<std::string>(m1->cont()); } else { GERROR("No filename set for DICOM file\n"); return GADGET_FAIL; } if(f) { meta= AsContainerMessage<ISMRMRD::MetaContainer>(f->cont()); } else { GERROR("No meta data found for DICOM\n"); return GADGET_FAIL; } unsigned int BUFSIZE = 1024; char *buf = new char[BUFSIZE]; const char* checkbuf; OFCondition status; DcmTagKey key; DcmDataset *dataset = dcm->getDataset(); float rescaleIntercept;//= meta->getObjectPtr()->as_double(GADGETRON_IMAGE_SCALE_OFFSET); float rescaleSlope;//= meta->getObjectPtr()->as_double(GADGETRON_IMAGE_SCALE_RATIO); static bool studyUIDmade=false; std::time_t rawtime; std::time(&rawtime); std::tm *timeinfo = std::localtime(&rawtime); if(meta->getObjectPtr()->exists("GADGETRON_IMAGE_SCALE_OFFSET") && meta->getObjectPtr()->exists("GADGETRON_IMAGE_SCALE_RATIO")) { rescaleIntercept=meta->getObjectPtr()->as_double(GADGETRON_IMAGE_SCALE_OFFSET); rescaleIntercept=meta->getObjectPtr()->as_double(GADGETRON_IMAGE_SCALE_OFFSET); // Window Center key.set(0x0028, 0x1050); dataset->remove(key); // Window Width key.set(0x0028, 0x1051); dataset->remove(key); rescaleIntercept = -1.0*rescaleIntercept*rescaleSlope; rescaleSlope= 1.0/rescaleSlope; key.set(0x0028,0x1052); ACE_OS::snprintf(buf, BUFSIZE, "%f", rescaleIntercept);// WRITE_DCM_STRING(key, buf); key.set(0x0028,0x1053); ACE_OS::snprintf(buf, BUFSIZE, "%f", rescaleSlope);//meta->getObjectPtr()->as_double("Intercept")); WRITE_DCM_STRING(key, buf); key.set(0x0028, 0x0030); ACE_OS::snprintf(buf, BUFSIZE, "%.6f\\%.6f", pixel_spacing_Y, pixel_spacing_X); WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x1030); //Study Description dataset->findAndGetString(key, checkbuf, false); if(checkbuf==NULL || !strcmp(checkbuf, "XXXXXXXX")) { ACE_OS::snprintf(buf, BUFSIZE, "%s", "Gadgetron^IEV"); WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x103E); //Series Description //if(!dataset->tagExistsWithValue(key)) //{ std::string type; if(meta) type=meta->getObjectPtr()->as_str(GADGETRON_DATA_ROLE); else type="MRI Images"; ACE_OS::snprintf(buf, BUFSIZE, "%s", type.c_str()); WRITE_DCM_STRING(key, buf); //} key.set(0x0020,0x0010);//Study ID dataset->findAndGetString(key, checkbuf, false); if(checkbuf==NULL || !strcmp(checkbuf, "XXXXXXXX")) { WRITE_DCM_STRING(key, "1"); // be sure to use the same one for all series you generate } //Study UID should be created in IEVChannelSumGadget. key.set(0x0020,0x000D);//Study UID dataset->findAndGetString(key, checkbuf, false); if(checkbuf==NULL || !strcmp(checkbuf, "XXXXXXXX")) { WRITE_DCM_STRING(key, meta->getObjectPtr()->as_str("StudyInstanceUID")); // be sure to use the same one for all series you generate } std::strftime(buf, 100, "%Y%m%d", timeinfo); key.set(0x0008,0x0020);//Study Date dataset->findAndGetString(key, checkbuf, false); if(checkbuf==NULL || !strcmp(checkbuf, "19000101")) { WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x0030);//Study Time dataset->findAndGetString(key, checkbuf, false); if(checkbuf==NULL || !strcmp(checkbuf, "121212")) { WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x0021);//Series Date if(!dataset->tagExistsWithValue(key)) { WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x0012);//Instance Creation Date if(!dataset->tagExistsWithValue(key)) { WRITE_DCM_STRING(key, buf); } std::strftime(buf, 100, "%H%M%S", timeinfo); key.set(0x0008,0x0031);//Series Time if(!dataset->tagExistsWithValue(key)) { WRITE_DCM_STRING(key, buf); } key.set(0x0008,0x0013);//Instance Creation Time if(!dataset->tagExistsWithValue(key)) { WRITE_DCM_STRING(key, buf); } key.set(0x0018,0x0081);//Echo Time WRITE_DCM_STRING(key, meta->getObjectPtr()->as_str("TE")); delete[] buf; //add try catch if(-1==this->next()->putq(m1)) { m1->release(); GERROR("Unable to pass on message\n"); return GADGET_FAIL; } return GADGET_OK; }
void _um_mod_init(char *initargs) { const char *name; char *tmphs, *cwd; int i; PyObject *pName, *pTmpObj, *pTmpFunc, *pTmpDict; GMESSAGE("umpyew init"); if (!strlen(initargs)) { GERROR("You must specify the Python module name."); return; } name = initargs; s.name="Prototypal Python bindings for *MView"; s.code=0x07; s.syscall=(sysfun *)calloc(scmap_scmapsize,sizeof(sysfun)); s.socket=(sysfun *)calloc(scmap_sockmapsize,sizeof(sysfun)); Py_Initialize(); Py_InitModule("umpyew", pEmbMethods); pEmptyTuple = PyTuple_New(0); pName = PyString_FromString(name); cwd = getcwd(NULL, 0); if (cwd) { setenv("PYTHONPATH", cwd, 0); free(cwd); } pModule = PyImport_Import(pName); Py_DECREF(pName); if (!pModule) { GERROR("Error loading Python module %s.\nIt has been searched for in the following path:\n%s", name, getenv("PYTHONPATH")); PyErr_Print(); return; } /* * Add ctl */ if ((ps.ctl = PyObject_GetAttrString(pModule, "modCtl")) && PyCallable_Check(ps.ctl)) s.ctl = ctl; else { GDEBUG(2, "function modCheckFun not defined in module %s", name); s.ctl = umpyew_alwayszero; Py_XDECREF(ps.ctl); } /* * Add checkfun */ if ((ps.checkfun = PyObject_GetAttrString(pModule, "modCheckFun")) && PyCallable_Check(ps.checkfun)) s.checkfun = checkfun; else { GDEBUG("2, function modCheckFun not defined in module %s", name); /* This makes the module almost useless, but we respect its author's will. */ s.checkfun = (epoch_t(*)())umpyew_alwayszero; Py_XDECREF(ps.checkfun); } /* * Add ctlhs */ MCH_ZERO(&(s.ctlhs)); pTmpObj = PyObject_GetAttrString(pModule, "modCtlHistorySet"); if (pTmpObj && PyList_Check(pTmpObj)) for (i = 0; i < PyList_Size(pTmpObj); i++) if ((tmphs = PyString_AsString(PyList_GET_ITEM(pTmpObj, i)))) { if (!strcmp(tmphs, "proc")) MCH_SET(MC_PROC, &(s.ctlhs)); else if (!strcmp(tmphs, "module")) MCH_SET(MC_MODULE, &(s.ctlhs)); else if (!strcmp(tmphs, "mount")) MCH_SET(MC_MOUNT, &(s.ctlhs)); } Py_XDECREF(pTmpObj); /* * Call modInit, if present */ pTmpObj = PyObject_GetAttrString(pModule, "modInit"); if (pTmpObj && PyCallable_Check(pTmpObj)) PyObject_CallObject(pTmpObj, pEmptyTuple); Py_XDECREF(pTmpObj); /* * Add system calls */ ps.syscall = calloc(scmap_scmapsize, sizeof(PyObject*)); PYTHON_SYSCALL(open, sysOpen); PYTHON_SYSCALL(close, sysClose); PYTHON_SYSCALL(access, sysAccess); PYTHON_SYSCALL(mkdir, sysMkdir); PYTHON_SYSCALL(rmdir, sysRmdir); PYTHON_SYSCALL(chmod, sysChmod); PYTHON_SYSCALL(chown, sysChown); PYTHON_SYSCALL(lchown, sysLchown); PYTHON_SYSCALL(unlink, sysUnlink); PYTHON_SYSCALL(link, sysLink); PYTHON_SYSCALL(symlink, sysSymlink); PYTHON_SYSCALL(stat64, sysStat64); PYTHON_SYSCALL(lstat64, sysLstat64); PYTHON_SYSCALL(fstat64, sysFstat64); PYTHON_SYSCALL(statfs64, sysStatfs64); PYTHON_SYSCALL(fstatfs64, sysStatfs64); PYTHON_SYSCALL(readlink, sysReadlink); PYTHON_SYSCALL(lseek, sysLseek); PYTHON_SYSCALL(utime, sysUtime); PYTHON_SYSCALL(utimes, sysUtimes) PYTHON_SYSCALL(read, sysRead); PYTHON_SYSCALL(write, sysWrite); PYTHON_SYSCALL(pread64, sysPread64); PYTHON_SYSCALL(pwrite64, sysPwrite64); add_service(&s); }
int NoiseAdjustGadget::process(GadgetContainerMessage<ISMRMRD::AcquisitionHeader>* m1, GadgetContainerMessage< hoNDArray< std::complex<float> > >* m2) { bool is_noise = m1->getObjectPtr()->isFlagSet(ISMRMRD::ISMRMRD_ACQ_IS_NOISE_MEASUREMENT); unsigned int channels = m1->getObjectPtr()->active_channels; unsigned int samples = m1->getObjectPtr()->number_of_samples; //TODO: Remove this if ( measurement_id_.empty() ) { unsigned int muid = m1->getObjectPtr()->measurement_uid; std::ostringstream ostr; ostr << muid; measurement_id_ = ostr.str(); } if ( is_noise ) { if (noiseCovarianceLoaded_) { m1->release(); //Do not accumulate noise when we have a loaded noise covariance return GADGET_OK; } // this noise can be from a noise scan or it can be from the built-in noise if ( number_of_noise_samples_per_acquisition_ == 0 ) { number_of_noise_samples_per_acquisition_ = samples; } if ( noise_dwell_time_us_ < 0 ) { if (noise_dwell_time_us_preset_ > 0.0) { noise_dwell_time_us_ = noise_dwell_time_us_preset_; } else { noise_dwell_time_us_ = m1->getObjectPtr()->sample_time_us; } } //If noise covariance matrix is not allocated if (noise_covariance_matrixf_.get_number_of_elements() != channels*channels) { std::vector<size_t> dims(2, channels); try { noise_covariance_matrixf_.create(&dims); noise_covariance_matrixf_once_.create(&dims); } catch (std::runtime_error& err) { GEXCEPTION(err, "Unable to allocate storage for noise covariance matrix\n" ); return GADGET_FAIL; } Gadgetron::clear(noise_covariance_matrixf_); Gadgetron::clear(noise_covariance_matrixf_once_); number_of_noise_samples_ = 0; } std::complex<float>* cc_ptr = noise_covariance_matrixf_.get_data_ptr(); std::complex<float>* data_ptr = m2->getObjectPtr()->get_data_ptr(); hoNDArray< std::complex<float> > readout(*m2->getObjectPtr()); gemm(noise_covariance_matrixf_once_, readout, true, *m2->getObjectPtr(), false); Gadgetron::add(noise_covariance_matrixf_once_, noise_covariance_matrixf_, noise_covariance_matrixf_); number_of_noise_samples_ += samples; m1->release(); return GADGET_OK; } //We should only reach this code if this data is not noise. if ( perform_noise_adjust_ ) { //Calculate the prewhitener if it has not been done if (!noise_decorrelation_calculated_ && (number_of_noise_samples_ > 0)) { if (number_of_noise_samples_ > 1) { //Scale noise_covariance_matrixf_ *= std::complex<float>(1.0/(float)(number_of_noise_samples_-1)); number_of_noise_samples_ = 1; //Scaling has been done } computeNoisePrewhitener(); acquisition_dwell_time_us_ = m1->getObjectPtr()->sample_time_us; if ((noise_dwell_time_us_ == 0.0f) || (acquisition_dwell_time_us_ == 0.0f)) { noise_bw_scale_factor_ = 1.0f; } else { noise_bw_scale_factor_ = (float)std::sqrt(2.0*acquisition_dwell_time_us_/noise_dwell_time_us_*receiver_noise_bandwidth_); } noise_prewhitener_matrixf_ *= std::complex<float>(noise_bw_scale_factor_,0.0); GDEBUG("Noise dwell time: %f\n", noise_dwell_time_us_); GDEBUG("Acquisition dwell time: %f\n", acquisition_dwell_time_us_); GDEBUG("receiver_noise_bandwidth: %f\n", receiver_noise_bandwidth_); GDEBUG("noise_bw_scale_factor: %f", noise_bw_scale_factor_); } if (noise_decorrelation_calculated_) { //Apply prewhitener if ( noise_prewhitener_matrixf_.get_size(0) == m2->getObjectPtr()->get_size(1) ) { hoNDArray<std::complex<float> > tmp(*m2->getObjectPtr()); gemm(*m2->getObjectPtr(), tmp, noise_prewhitener_matrixf_); } else { if (!pass_nonconformant_data_) { m1->release(); GERROR("Number of channels in noise prewhitener %d is incompatible with incoming data %d\n", noise_prewhitener_matrixf_.get_size(0), m2->getObjectPtr()->get_size(1)); return GADGET_FAIL; } } } } if (this->next()->putq(m1) == -1) { GDEBUG("Error passing on data to next gadget\n"); return GADGET_FAIL; } return GADGET_OK; }
int gpuCSICoilEstimationGadget::process( GadgetContainerMessage<IsmrmrdAcquisitionBucket>* m1) { IsmrmrdAcquisitionBucket* bucket = m1->getObjectPtr(); auto cm1 = new GadgetContainerMessage<cuSenseData>(); auto senseData = cm1->getObjectPtr(); coils = bucket->data_.front().head_->getObjectPtr()->active_channels; GDEBUG("Active channels %i \n",coils); { hoNDArray<std::complex<float>> * ho_data; hoNDArray<float>* ho_traj; std::tie(ho_data,ho_traj) = combine_data(bucket->data_); if (skip_lines_ > 0){ auto cal_dims = *ho_data->get_dimensions(); cal_dims.back() = skip_lines_; auto data_dims = *ho_data->get_dimensions(); data_dims.back() -= skip_lines_; hoNDArray<float_complext> cal_view(cal_dims,(float_complext*) ho_data->get_data_ptr()); senseData->freq_calibration = boost::make_shared<cuNDArray<float_complext>>(cal_view); senseData->freq_calibration->squeeze(); hoNDArray<float_complext> data_view(data_dims,(float_complext*)ho_data->get_data_ptr()+cal_view.get_number_of_elements()); senseData->data = boost::make_shared<cuNDArray<float_complext>>(data_view); } else { senseData->data = boost::make_shared<cuNDArray<float_complext>>(reinterpret_cast<hoNDArray<float_complext>*>(ho_data)); } if (ho_traj->get_size(0) > 2){ //We have dcw auto traj_dcw = separate_traj_and_dcw(ho_traj); senseData->traj = boost::make_shared<cuNDArray<floatd2>>(*std::get<0>(traj_dcw)); senseData->dcw = boost::make_shared<cuNDArray<float>>(*std::get<1>(traj_dcw)); } else { std::vector<size_t> tdims = *ho_traj->get_dimensions(); std::vector<size_t> tmp_dim(tdims.begin()+1,tdims.end()); hoNDArray<floatd2> tmp(tmp_dim,reinterpret_cast<floatd2*>(ho_traj->get_data_ptr())); senseData->traj = boost::make_shared<cuNDArray<floatd2>>(tmp); } delete ho_data; delete ho_traj; } //Remove Initial Spirals boost::shared_ptr< cuNDArray<float_complext> > ref_data; boost::shared_ptr< cuNDArray<floatd2> > ref_traj; boost::shared_ptr<cuNDArray<float> > ref_dcw; if (bucket->ref_.empty()){ ref_data = senseData->data; ref_traj = senseData->traj; ref_dcw = senseData->dcw; } else { hoNDArray<std::complex<float>> * ho_data; hoNDArray<float>* ho_traj; std::tie(ho_data,ho_traj) = combine_data(bucket->ref_); ref_data = boost::make_shared<cuNDArray<float_complext>>(reinterpret_cast<hoNDArray<float_complext>*>(ho_data)); if (ho_traj->get_size(0) > 2){ auto traj_dcw = separate_traj_and_dcw(ho_traj); ref_traj =boost::make_shared<cuNDArray<floatd2>>(*std::get<0>(traj_dcw)); ref_dcw = boost::make_shared<cuNDArray<float>>(*std::get<1>(traj_dcw)); } else { std::vector<size_t> tdims = *ho_traj->get_dimensions(); std::vector<size_t> tmp_dim(tdims.begin()+1,tdims.end()); hoNDArray<floatd2> tmp(tmp_dim,reinterpret_cast<floatd2*>(ho_traj->get_data_ptr())); ref_traj = boost::make_shared<cuNDArray<floatd2>>(tmp); } delete ho_data; delete ho_traj; } senseData->csm = calculate_CSM(ref_data.get(),ref_traj.get(),ref_dcw.get()); if (this->next()->putq(cm1) == GADGET_FAIL){ GERROR("Failed to put message on que\n"); return GADGET_FAIL; } return GADGET_OK; }
int IEVChannelSumGadget::process(GadgetContainerMessage< ISMRMRD::ImageHeader>* m1) { GadgetContainerMessage<hoNDArray< float > > *unfiltered_unwrapped_msg_ptr = AsContainerMessage<hoNDArray<float>>(m1->cont()); GadgetContainerMessage<hoNDArray< float > > *filtered_unwrapped_msg_ptr = AsContainerMessage<hoNDArray<float>>(unfiltered_unwrapped_msg_ptr->cont()); GadgetContainerMessage<ISMRMRD::MetaContainer> *meta; static int c=0; int e; int echo = m1->getObjectPtr()->contrast; float inv_echo_time; int echo_offset=yres*xres*num_ch*echo; if(!filtered_unwrapped_msg_ptr || !unfiltered_unwrapped_msg_ptr) { GERROR("Wrong types received in IEVChannelSumGadget. Filtered and unfiltered phase expected.\n"); return GADGET_FAIL; } meta = AsContainerMessage<ISMRMRD::MetaContainer>(filtered_unwrapped_msg_ptr->cont()); //float* filtered_phase_ptr= filtered_unwrapped_msg_ptr->getObjectPtr()->get_data_ptr(); m1->getObjectPtr()->channels=1; //yes? inv_echo_time=1/echoTimes[echo];//to avoid millions of divisions per slice memcpy(filtered_phase_ptr+echo_offset, filtered_unwrapped_msg_ptr->getObjectPtr()->get_data_ptr(), xres*yres*num_ch*sizeof(float)); for (int i = 0; i < xres*yres*num_ch; i++) freq_ptr[echo_offset+i] = filtered_phase_ptr[echo_offset+i]*inv_echo_time; hdr_ptr[echo]=*(m1->getObjectPtr()); if(meta) { meta->getObjectPtr()->append("StudyInstanceUID", studyInstanceUID.c_str());//may be in xml header, may not be, in that case put it in xml so it can get to dicom char TE[10]; sprintf(TE, "%f", echoTimes[echo]*1000); meta->getObjectPtr()->append("TE", TE); attributes[echo]=*(meta->getObjectPtr()); } unfiltered_unwrapped_msg_ptr->release();//all data have been copied if(echo==(numEchos-1)) { float* weights= new float[xres*yres*num_ch]; float** channel_weights= new float* [num_ch]; float* to_normalize = new float[xres*yres]; int ch; if(iev.value()==int(IEV::YES))//just to allow this to work without IEV/make it very easy to compare { #pragma omp parallel //expanded parallel --- to allow sample to be allocated once { float* sample = new float[numEchos]; int start = omp_get_thread_num()/omp_get_num_threads()*xres*yres*num_ch; int end = (omp_get_thread_num()+1)/omp_get_num_threads()*xres*yres*num_ch; for(int i =start; i <end; i++) { ///////// for(int j = 0; j < numEchos; j++) sample[j]=freq_ptr[i+j*xres*yres*num_ch]; //assuming all(6-10 at at time) pages can be held in memory, this isn't terrible weights[i]=stdev(sample, numEchos); //find standard deviation between echoes ///// } delete[] sample; } #pragma omp parallel for private(ch) for(ch = 0; ch < num_ch; ch++) { float* temp_array; channel_weights[ch]=&weights[ch*xres*yres]; medianFilter(channel_weights[ch], xres, yres); for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]=1/(channel_weights[ch][i]+FLT_MIN); //weight as inverse, } } for (int i = 0; i < xres*yres; i++) to_normalize[i] = 0; for(int ch=0; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { to_normalize[i]+=channel_weights[ch][i]; } for(int ch=0; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]/=to_normalize[i]; //normalize weights } } else { #pragma omp parallel for private(ch) for(int ch=0; ch< num_ch; ch++) { channel_weights[ch]=&weights[ch*xres*yres]; for (int i = 0; i < xres*yres; i++) { channel_weights[ch][i]=1; } } } for(e=0; e<numEchos; e++) { hdr_ptr[e].channels=1; hdr_ptr[e].contrast=e; hdr_ptr[e].data_type = ISMRMRD::ISMRMRD_FLOAT;//GADGET_IMAGE_REAL_FLOAT; hdr_ptr[e].image_type = ISMRMRD::ISMRMRD_IMTYPE_PHASE;//There is no frequency image type hdr_ptr[e].slice= (hdr_ptr[e].image_index) % num_slices;//was hdr_ptr[e].image_index___-1_____) % num_slices before decrementor was added upstream if(output_phase.value()) { // GadgetContainerMessage<ISMRMRD::ImageHeader>* phase_hdr = new GadgetContainerMessage<ISMRMRD::ImageHeader>(hdr_ptr[e]); //*(phase_hdr->getObjectPtr()) =*(hdr_ptr[e]->getObjectPtr()); GadgetContainerMessage<hoNDArray< float > > *comb_phase_msg = new GadgetContainerMessage<hoNDArray< float > >(); phase_hdr->getObjectPtr()->image_series_index=series_id_offset+1; try{comb_phase_msg->getObjectPtr()->create(xres,yres);} catch (std::runtime_error &err){ GEXCEPTION(err,"Unable to create output image\n"); return GADGET_FAIL; } float* output_ptr=comb_phase_msg->getObjectPtr()->get_data_ptr(); phase_hdr->cont(comb_phase_msg); // for (int i = 0; i < xres*yres; i++) { output_ptr[i]=filtered_phase_ptr[e*xres*yres*num_ch+i]*channel_weights[0][i]; //instead of setting to 0 and adding first channel } for(int ch=1; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { output_ptr[i]+=filtered_phase_ptr[e*xres*yres*num_ch+xres*yres*ch+i]*channel_weights[ch][i];; //instead of setting to 0 and adding first channel } // if(meta) { GadgetContainerMessage<ISMRMRD::MetaContainer>* meta = new GadgetContainerMessage<ISMRMRD::MetaContainer>(attributes[e]); comb_phase_msg->cont(meta); } // if (this->next()->putq(phase_hdr) == -1) { m1->release(); GERROR("Unable to put collapsed images on next gadget's queue\n"); return GADGET_FAIL; } } if(output_LFS.value()) { // GadgetContainerMessage<ISMRMRD::ImageHeader>* freq_hdr=new GadgetContainerMessage<ISMRMRD::ImageHeader>(hdr_ptr[e]); //*(freq_hdr->getObjectPtr()) =*(hdr_ptr[e]->getObjectPtr()); GadgetContainerMessage<hoNDArray< float > > *comb_freq_msg = new GadgetContainerMessage<hoNDArray< float > >(); freq_hdr->getObjectPtr()->image_series_index=series_id_offset+output_phase.value()+1; try{comb_freq_msg->getObjectPtr()->create(xres,yres);} catch (std::runtime_error &err){ GEXCEPTION(err,"Unable to create output image\n"); return GADGET_FAIL; } float* output_ptr=comb_freq_msg->getObjectPtr()->get_data_ptr(); freq_hdr->cont(comb_freq_msg); freq_hdr->getObjectPtr()->image_type = 6; // for (int i = 0; i < xres*yres; i++) output_ptr[i]=freq_ptr[e*xres*yres*num_ch+i]*channel_weights[0][i]; //instead of setting to 0 and adding first channel for(int ch=1; ch< num_ch; ch++) for (int i = 0; i < xres*yres; i++) { output_ptr[i]+=freq_ptr[e*xres*yres*num_ch+xres*yres*ch+i]*channel_weights[ch][i]; } // if(meta) { GadgetContainerMessage<ISMRMRD::MetaContainer>* meta = new GadgetContainerMessage<ISMRMRD::MetaContainer>(attributes[e]); meta->getObjectPtr()->set(GADGETRON_DATA_ROLE, GADGETRON_IMAGE_FREQMAP); //*(meta->getObjectPtr())=*(attributes[e]->getObjectPtr()); comb_freq_msg->cont(meta); } // if (this->next()->putq(freq_hdr) == -1) { //m1->release(); GERROR("Unable to put collapsed images on next gadget's queue\n"); return GADGET_FAIL; } } } delete[] to_normalize; delete[] weights; delete[] channel_weights; //if(output.value()==int(OUTPUT::PHASE)) // delete[] unfiltered_phase_ptr; } return GADGET_OK; }
int GadgetInstrumentationStreamController::output_ready(ACE_Message_Block* mb) { GadgetContainerMessage<GadgetMessageIdentifier>* m0 = AsContainerMessage<GadgetMessageIdentifier>(mb); if (!m0) { GERROR("Unable to extract GadgetMessageIdentifier\n"); mb->release(); return GADGET_FAIL; } GadgetContainerMessage<ISMRMRD::ImageHeader>* m_tmp = 0; switch (m0->getObjectPtr()->id) { case (GADGET_MESSAGE_ACQUISITION): if (0 != this->return_data<ISMRMRD::AcquisitionHeader, hoNDArray< std::complex<float> >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ACQUISITON\n"); m0->release(); return GADGET_FAIL; } break; case (GADGET_MESSAGE_ISMRMRD_IMAGE): m_tmp = AsContainerMessage<ISMRMRD::ImageHeader>(m0->cont()); if (!m_tmp) { GERROR("Error converting header of GADGET_MESSAGE_ISMRMRD_IMAG\n"); mb->release(); return GADGET_FAIL; } switch (m_tmp->getObjectPtr()->data_type) { case (ISMRMRD::ISMRMRD_USHORT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< uint16_t >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_SHORT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< int16_t >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_UINT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< uint32_t >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_INT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< int32_t >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_FLOAT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< float >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_DOUBLE): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< double >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_CXFLOAT): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< std::complex<float> >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; case (ISMRMRD::ISMRMRD_CXDOUBLE): if (0 != this->return_data<ISMRMRD::ImageHeader, hoNDArray< std::complex<double> >, ISMRMRD::MetaContainer >(m0->cont()) ) { GERROR("Unable to convert and return GADGET_MESSAGE_ISMRMRD_IMAGE\n"); m0->release(); return GADGET_FAIL; } break; } break; case (GADGET_MESSAGE_RECONDATA): if (this->return_recondata(m0->cont()) == GADGET_FAIL) { GERROR("Unable to convert and return GADGET_MESSAGE_RECONDATA"); m0->release(); return GADGET_FAIL; } break; case (GADGET_MESSAGE_CLOSE): break; default: GERROR("Unsupported message ID (%d) encountered\n", m0->getObjectPtr()->id); mb->release(); return GADGET_FAIL; } mb->release(); return GADGET_OK; }
int DistributeGadget::process_config(ACE_Message_Block* m) { started_nodes_ = 0; node_parameters_ = std::string(m->rd_ptr()); //Grab the original XML conifguration std::string xml = controller_->get_xml_configuration(); GadgetronXML::GadgetStreamConfiguration cfg; GadgetronXML::deserialize(xml.c_str(),cfg); //Delete Gadgets up to this Gadget std::vector<GadgetronXML::Gadget>::iterator it = cfg.gadget.begin(); while ((it->name != std::string(this->module()->name())) && (it != cfg.gadget.end())) it++; it++; cfg.gadget.erase(cfg.gadget.begin(),it); //Delete Gadgets after collector it = cfg.gadget.begin(); while ((it->name != collector.value()) && (it != cfg.gadget.end())) it++; it++; cfg.gadget.erase(it,cfg.gadget.end()); std::stringstream o; GadgetronXML::serialize(cfg,o); node_xml_config_ = o.str(); Gadget* tmp = this; while (tmp->next()) { if (std::string(tmp->module()->name()) == collector.value()) break; tmp = dynamic_cast<Gadget*>(tmp->next()); } collect_gadget_ = tmp; if (!collect_gadget_) { GERROR("Failed to locate collector Gadget with name %s\n", collector.value().c_str()); return GADGET_FAIL; } else { collect_gadget_->set_parameter("pass_through_mode","true"); } // get current node ip addresses ACE_INET_Addr* the_addr_array = NULL; size_t num_of_ip = 0; int rc = ACE::get_ip_interfaces (num_of_ip, the_addr_array); if (rc != 0) { GERROR_STREAM("Retreive local ip addresses failed ... "); num_of_ip = 0; } if (the_addr_array!=NULL ) delete [] the_addr_array; for (size_t ii=0; ii<num_of_ip; ii++) { std::string ip = std::string(the_addr_array[ii].get_host_addr()); local_address_.push_back(ip); GDEBUG_STREAM("--> Local address : " << ip); } return GADGET_OK; }