void SigmoidNetwork::ForwardPassOutputs() { int i, iout; // Output unit activations for (iout = 0; iout < NOutputs; iout++) { vectorIterator<float> itout(*(outputs[iout])); float sum = 0.0; for (i = NInputs+1, itout.init(); i < NInputs+1+NHidden+1 && ! itout; i++, ++itout) { sum += itout()*activations[i]; } sums[NHidden + iout] = sum; float act = 0.0; switch (outputType) { case Linear: {act = sum; break;} case Sigmoid: {act = Logistic(sum); break;} case Gaussian: {act = exp(-(sum * sum)); break;} } activations[iout+NInputs+1+NHidden+1] = act; } }
void SigmoidNetwork::AdjustBiases() { // assuming that each hidden unit outputs 0.5, adjust the output // biases to match the values in the targets array. // The activation going into the output unit is // b * 1 + (W * 0.5), where W is the hidden-to-output weight vector // and 0.5 is a corresponding vector of 0.5's. b is the "bias" weight // that connects to activations[NInputs + 1 + NHidden]. // Let T be the target. For sigmoid units, the desired value for b is // ln(T/(1-T)) - (W * 0.5) = b // For gaussian units, the desired value for b is // +/- sqrt(- ln T) - (W * 0.5) // We have ambiguity (unless T = 1). We will always choose the low // side (the - sign). // When the target is 0 or 1, we adjust it to 0.01 or 0.99. if (outputType == Linear) return; for (int iout = 0; iout < NOutputs; iout++) { float tar = targets[iout]; if (tar < 0.01) tar = 0.01; else if (tar > 0.99) tar = 0.99; if (outputType == Sigmoid) tar = log(tar / (1.0 - tar)); else if (outputType == Gaussian) tar = -sqrt(-log(tar)); vectorIterator<float> itout(*(outputs[iout])); float sum = 0.0; int i; for (i = NInputs + 1, itout.init(); i < NInputs + 1 + NHidden && ! itout; i++, ++itout) { sum += itout() * 0.5; } float bias = tar - sum; itout() = bias; } }
List polyData2R(vtkSmartPointer<vtkPolyData> polydata) { try { //vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); //poltOutput(); int np = polydata->GetNumberOfPoints(); vtkPoints* points=polydata->GetPoints(); NumericMatrix vb(3,np); double point[3]; for (int i=0; i< np;i++) { polydata->GetPoints()->GetPoint(i,point); for (int j=0; j<3; j++) vb(j,i) = point[j]; } //points->Delete(); int h; vtkIdType npts=3,*pts; int nit = polydata->GetNumberOfPolys(); std::vector<int> it; vtkSmartPointer<vtkCellArray> oCellArr= vtkSmartPointer<vtkCellArray>::New(); polydata->GetPolys()->InitTraversal(); while(polydata->GetPolys()->GetNextCell(npts,pts)) { //for (int i=0; i< nit;i++) { // h = oCellArr->GetNextCell(npts,pts); //if(h == 0){ // break; //} if(npts == 3){ it.push_back(pts[0]+1); it.push_back(pts[1]+1); it.push_back(pts[2]+1); } } int ll = it.size()/3; IntegerMatrix itout(3,ll,it.begin()); List out = List::create(Named("vb")=vb, Named("it")=itout ); if (it.size() > 0) out.attr("class") = "mesh3d"; return out; } catch(...) { ::Rf_error("PolyData to R-Data conversion failed"); return List::create(Named("error")=1); } }
void operator()() { std::ostreambuf_iterator<char> itout(m_tgtcn); std::set<int> existingchan; //Keep track of the nb of unique channels used by the tracks size_t nbwritten = 0; //Reserve Header itout = std::fill_n( itout, SMDL_Header::size(), 0 ); nbwritten += SMDL_Header::size(); //Reserve Song chunk if( m_version == eDSEVersion::V402 ) { itout = std::fill_n( itout, SongChunk_v402::Size, 0 ); nbwritten += SongChunk_v402::Size; } else if( m_version == eDSEVersion::V415 ) { itout = std::fill_n( itout, (SongChunk_v415::SizeNoPadd + SongChunk_v415::LenMaxPadding), 0 ); nbwritten += (SongChunk_v415::SizeNoPadd + SongChunk_v415::LenMaxPadding); } else throw std::runtime_error( "SMDL_Writer::operator()(): Invalid DSE version supplied!!" ); if( utils::LibWide().isLogOn() ) { clog << "-------------------------\n" << "Writing SMDL\n" << "-------------------------\n" << m_src.printinfo() <<"\n" ; } //Write tracks for( uint8_t i = 0; i < m_src.getNbTracks(); ++i ) { if( m_src[i].empty() ) continue; //ignore empty tracks const DSE::MusicTrack & atrk = m_src[i]; TrkPreamble preamble; preamble.trkid = i; preamble.chanid = atrk.GetMidiChannel(); preamble.unk1 = 0; preamble.unk2 = 0; uint32_t lenmod = WriteTrkChunk( itout, preamble, atrk.begin(), atrk.end(), atrk.size() ); nbwritten += lenmod; //Write padding lenmod = (lenmod % 4); uint32_t lenpad = lenmod; if( lenmod != 0 ) std::fill_n( itout, lenpad, static_cast<uint8_t>(eTrkEventCodes::EndOfTrack) ); existingchan.insert( preamble.chanid ); } //Write end chunk DSE::ChunkHeader eoc; eoc.label = static_cast<uint32_t>(eDSEChunks::eoc); eoc.datlen = 0; eoc.param1 = EocParam1Default; eoc.param2 = EocParam2Default; itout = eoc.WriteToContainer( itout ); //Go back to write the header and song chunk! size_t flen = static_cast<size_t>(m_tgtcn.tellp()); m_tgtcn.seekp(0); itout = std::ostreambuf_iterator<char>(m_tgtcn); WriteHeader( itout, existingchan, flen ); }
mitk::Image::Pointer PartialVolumeAnalysisClusteringCalculator::CaculateAngularErrorImage( mitk::Image::Pointer comp1, mitk::Image::Pointer comp2, mitk::Image::Pointer probImg) const { // cast input images to itk typedef itk::Image<float, 3> ImageType; typedef mitk::ImageToItk<ImageType> CastType; CastType::Pointer caster = CastType::New(); caster->SetInput(comp1); caster->Update(); ImageType::Pointer comp1Image = caster->GetOutput(); caster = CastType::New(); caster->SetInput(comp2); caster->Update(); ImageType::Pointer comp2Image = caster->GetOutput(); caster = CastType::New(); caster->SetInput(probImg); caster->Update(); ImageType::Pointer probImage = caster->GetOutput(); // figure out maximum probability for fiber class float maxProb = 0; itk::ImageRegionConstIterator<ImageType> itprob(probImage, probImage->GetLargestPossibleRegion()); itprob.GoToBegin(); while( !itprob.IsAtEnd() ) { maxProb = itprob.Get() > maxProb ? itprob.Get() : maxProb; ++itprob; } // generate a list sample of angles at positions // where the fiber-prob is higher than .2*maxprob typedef float MeasurementType; const unsigned int MeasurementVectorLength = 2; typedef itk::Vector< MeasurementType , MeasurementVectorLength > MeasurementVectorType; typedef itk::Statistics::ListSample< MeasurementVectorType > ListSampleType; ListSampleType::Pointer listSample = ListSampleType::New(); listSample->SetMeasurementVectorSize( MeasurementVectorLength ); itk::ImageRegionIterator<ImageType> it1(comp1Image, comp1Image->GetLargestPossibleRegion()); itk::ImageRegionIterator<ImageType> it2(comp2Image, comp2Image->GetLargestPossibleRegion()); it1.GoToBegin(); it2.GoToBegin(); itprob.GoToBegin(); while( !itprob.IsAtEnd() ) { if(itprob.Get() > 0.2 * maxProb) { MeasurementVectorType mv; mv[0] = ( MeasurementType ) it1.Get(); mv[1] = ( MeasurementType ) it2.Get(); listSample->PushBack(mv); } ++it1; ++it2; ++itprob; } // generate a histogram from the list sample typedef float HistogramMeasurementType; typedef itk::Statistics::Histogram< HistogramMeasurementType, itk::Statistics::DenseFrequencyContainer2 > HistogramType; typedef itk::Statistics::SampleToHistogramFilter< ListSampleType, HistogramType > GeneratorType; GeneratorType::Pointer generator = GeneratorType::New(); GeneratorType::HistogramType::SizeType size(2); size.Fill(30); generator->SetHistogramSize( size ); generator->SetInput( listSample ); generator->SetMarginalScale( 10.0 ); generator->Update(); // look for frequency mode in the histogram GeneratorType::HistogramType::ConstPointer histogram = generator->GetOutput(); GeneratorType::HistogramType::ConstIterator iter = histogram->Begin(); float maxFreq = 0; MeasurementVectorType maxValue; maxValue.Fill(0); while ( iter != histogram->End() ) { if(iter.GetFrequency() > maxFreq) { maxFreq = iter.GetFrequency(); maxValue[0] = iter.GetMeasurementVector()[0]; maxValue[1] = iter.GetMeasurementVector()[1]; } ++iter; } // generate return image that contains the angular // error of the voxels to the histogram max measurement ImageType::Pointer returnImage = ImageType::New(); returnImage->SetSpacing( comp1Image->GetSpacing() ); // Set the image spacing returnImage->SetOrigin( comp1Image->GetOrigin() ); // Set the image origin returnImage->SetDirection( comp1Image->GetDirection() ); // Set the image direction returnImage->SetRegions( comp1Image->GetLargestPossibleRegion() ); returnImage->Allocate(); itk::ImageRegionConstIterator<ImageType> cit1(comp1Image, comp1Image->GetLargestPossibleRegion()); itk::ImageRegionConstIterator<ImageType> cit2(comp2Image, comp2Image->GetLargestPossibleRegion()); itk::ImageRegionIterator<ImageType> itout(returnImage, returnImage->GetLargestPossibleRegion()); cit1.GoToBegin(); cit2.GoToBegin(); itout.GoToBegin(); vnl_vector<float> v(3); v[0] = cos( maxValue[0] ) * sin( maxValue[1] ); v[1] = sin( maxValue[0] ) * sin( maxValue[1] ); v[2] = cos( maxValue[1] ); // MITK_INFO << "max vector: " << v; while( !cit1.IsAtEnd() ) { vnl_vector<float> v1(3); v1[0] = cos( cit1.Get() ) * sin( cit2.Get() ); v1[1] = sin( cit1.Get() ) * sin( cit2.Get() ); v1[2] = cos( cit2.Get() ); itout.Set(fabs(angle(v,v1))); // MITK_INFO << "ang_error " << v1 << ": " << fabs(angle(v,v1)); ++cit1; ++cit2; ++itout; } mitk::Image::Pointer retval = mitk::Image::New(); retval->InitializeByItk(returnImage.GetPointer()); retval->SetVolume(returnImage->GetBufferPointer()); return retval; }
CodAddMsgDialog::CodAddMsgDialog(CodObjCanvas * from, CodObjCanvas * to, CodMsgSupport * i, ColDiagramView * v, bool fo) : QDialog(0, "add msg dialog", TRUE), in(i), view(v), forward(fo) { setCaption(TR("Add message dialog")); QVBoxLayout * vbox = new QVBoxLayout(this); QHBoxLayout * hbox; vbox->setMargin(5); hbox = new QHBoxLayout(vbox); hbox->setMargin(10); QLabel * label1 = new QLabel(TR("Add message to %1", to->get_full_name()), this); label1->setAlignment(AlignCenter); hbox->addWidget(label1); QGrid * grid = new QGrid(2, this); vbox->addWidget(grid); new QLabel(TR("rank : "), grid); cbrank = new QComboBox(FALSE, grid); ColMsgList all_in; ColMsgList all_out; from->get_all_in_all_out(all_in, all_out); ColMsg * m; QStringList new_ones; QListIterator<ColMsg> itout(all_out); for (; (m = itout.current()) != 0; ++itout) { QString s = m->next_hierarchical_rank(); if ((s.find('.') != - 1) && (ColMsg::find(s, all_out) == 0)) { cbrank->insertItem(QString::number(m->get_rank() + 1) + " : " + s); new_ones.append(s); } } QListIterator<ColMsg> itin(all_in); for (; (m = itin.current()) != 0; ++itin) { QString s = m->get_hierarchical_rank() + ".1"; if ((ColMsg::find(s, all_out) == 0) && (new_ones.findIndex(s) == -1)) { cbrank->insertItem(QString::number(m->get_rank() + 1) + " : " + s); new_ones.append(s); } } // add a the rank for a new toplevel link cbrank->insertItem(QString::number(ColMsg::last_rank(view->get_msgs()) + 1) + " : " + QString::number(view->get_msgs().count() + 1)); // the default new one follow the last input or output or view's last msg cbrank->setCurrentItem((cbrank->count() == 1) ? 0 : cbrank->count() - 2); QSizePolicy sp = cbrank->sizePolicy(); sp.setHorData(QSizePolicy::Expanding); cbrank->setSizePolicy(sp); new QLabel("", grid); new QLabel("", grid); // the operations SmallPushButton * b = new SmallPushButton(TR("message :"), grid); connect(b, SIGNAL(clicked()), this, SLOT(menu_op())); edoper = new QComboBox(TRUE, grid); edoper->setAutoCompletion(completion()); // gets operations cl = to->get_class(); if (cl != 0) { cl->get_opers(opers, list); edoper->insertStringList(list); if (!cl->is_writable()) cl = 0; } edoper->setSizePolicy(sp); new QLabel("", grid); new QLabel("", grid); // ok & cancel hbox = new QHBoxLayout(vbox); hbox->setMargin(5); QPushButton * ok = new QPushButton(TR("&OK"), this); QPushButton * cancel = new QPushButton(TR("&Cancel"), this); QSize bs(cancel->sizeHint()); ok->setDefault(TRUE); ok->setFixedSize(bs); cancel->setFixedSize(bs); hbox->addWidget(ok); hbox->addWidget(cancel); connect(ok, SIGNAL(clicked()), this, SLOT(accept())); connect(cancel, SIGNAL(clicked()), this, SLOT(reject())); }