Beispiel #1
0
double
MultivariateModel
::ComputeLogLikelihood(const Observations &Obs) 
{
  /// It computes the likelihood of the model. For each subject i, it sums its likelihood, namely the distance,
  /// for each time t_ij, between the observation y_ij and the prediction f(t_ij) = ComputeParallelCurve
  
  ScalarType LogLikelihood = 0;

  /// For each subject
  for(size_t i = 0; i < m_NumberOfSubjects; ++i) 
  {
    ScalarType N = Obs.GetNumberOfTimePoints(i);
    
    /// For each timepoint
    for(size_t j = 0; j < N; ++j)
    {
      auto& it = Obs.GetSubjectCognitiveScore(i, j);
      VectorType PC = ComputeParallelCurve(i, j);
      LogLikelihood += (it - PC).squared_magnitude();
    }
  }
  
  LogLikelihood /= -2*m_Noise->GetVariance();
  LogLikelihood -= m_NbTotalOfObservations * log(sqrt(2 * m_Noise->GetVariance() * M_PI));
  
  return LogLikelihood;
}
///
/// \brief LogicInitiator::processObservations  main function which processes new observations
/// to initiation candidates. And compares already existing candidates and observation with use of
/// cascaded logic.
/// \param observations new observations
/// \return new tracks to be initated to tracker
///
InitiatorCandidates LogicInitiator::processObservations(const Observations observations)
{        
    // declare structure for new tracks which should be initiated by tracker
    InitiatorCandidates newTracks;
    if(observations.empty()) return newTracks;

    // declare structure for accepted candidates
    InitiatorCandidates acceptedCandidates;

    // Get delta time
    double timeDiff = observations.front()->createdAt - m_lastObservationTime;
    m_lastObservationTime = observations.front()->createdAt;

    // Get observations which could not be matched by the tracker
    Observations newObs;
    foreach(Observation::Ptr ob, observations)
    {
        if(!ob->matched)
        {
            newObs.push_back(ob);
        }
    }
    
    // Associate new observations with existing candidates for initiation
    foreach(InitiatorCandidate::Ptr candidate, m_candidates)
    {
        // initialize flag if pairing could be found for candidate
        bool pairingFound(false);
        
        // If selected number of scans will be reached tracks are created
        if(candidate->observations.size() == m_numberScans-1)
        {
            predictKalman(candidate->state, timeDiff);
            candidate->extrapolation = linearExtrapolation(*(candidate->observations.rbegin()+1),
                                                           candidate->observations.back(),timeDiff);
            foreach(Observation::Ptr observation,newObs)
            {
                if(compareWithExtrapolation(candidate,observation))
                {
                    if(compareWithCandidate(candidate, observation))
                    {
                        if(updateKalman(candidate->state,observation))
                        {
                            pairingFound = true;
                            candidate->missedObs = 0;
                            observation->matched = true;
                            candidate->observations.push_back(observation);
                            newTracks.push_back(candidate);
                        }
                    }
                }
            }
        }
Beispiel #3
0
    // Gather all Objects and setup ObjectRefs
    void PharmML::setupObjects() {
        std::unordered_set<std::string> allOids;

        TrialDesign *td = this->getTrialDesign();

        if (td) {
            Arms *arms = td->getArms();
            if (arms) {
                for (Arm *arm : arms->getArms()) {
                    this->checkAndAddOid(allOids, arm, arm);
                }
            }
            Observations *observations = td->getObservations();
            if (observations) {
                for (Observation *observation : observations->getObservations()) {
                    this->checkAndAddOid(allOids, observation, observation);
                }
                for (IndividualObservations *observation : observations->getIndividualObservations()) {
                    this->checkAndAddOid(allOids, observation, observation);
                }
            }
            Interventions *interventions = td->getInterventions();
            if (interventions) {
                for (Administration *admin : interventions->getAdministrations()) {
                    this->checkAndAddOid(allOids, admin, admin);
                }
                for (InterventionsCombination *comb : interventions->getInterventionsCombinations()) {
                    this->checkAndAddOid(allOids, comb, comb);
                }
            }
        }

        // Obtain a map from all oids to Objects. Will be used to populate ObjectRefs
        std::unordered_map<std::string, Object *> oidMap;
        for (Object *object : this->allObjects) {
            oidMap[object->getOid()] = object;
        }

        for (Object *object : this->allObjects) {
            object->gatherObjectRefs(oidMap);
        }

        // Populate ObjectReferer ObjectRefs
        if (td) {
            Interventions *interventions = td->getInterventions();
            if (interventions) {
                for (IndividualAdministration *ia : interventions->getIndividualAdministrations()) {
                    ia->gatherObjectRefs(oidMap);
                }
            }
        }
    }
Observations PositionTracker::makeObservations(DataPtr data) {

    Observations obsvs;
    for (int i = 0; i < data->position_size(); i++) {

        Observation obsv(OBSERVATION_DIMENSIONS);
        obsv[0] = data->position(i).position().x();
        obsv[1] = data->position(i).position().y();
        obsv[2] = data->position(i).position().z();

        obsvs.push_back(obsv);
    }
    return obsvs;
}
Beispiel #5
0
vector<double> ObjectiveFunc::get_residuals_vec(const Observations &sim_obs, const Parameters &pars, const vector<string> &obs_names) const
{
	vector<double> residuals_vec;
	residuals_vec.resize(obs_names.size(), 0.0);

	Observations::const_iterator found_obs;
	Observations::const_iterator not_found_obs=(*observations_ptr).end();
	PriorInformation::const_iterator found_prior_info;
	PriorInformation::const_iterator not_found_prior_info = prior_info_ptr->end();

	int i=0;
	for(vector<string>::const_iterator b=obs_names.begin(), e=obs_names.end(); b != e; ++b, ++i)
	{
		found_obs = observations_ptr->find(*b);
		found_prior_info = prior_info_ptr->find(*b);

		if (found_obs != not_found_obs)
		{
			residuals_vec[i] = sim_obs.get_rec(*b) - (*found_obs).second;
		}
		else if (found_prior_info != not_found_prior_info)
		{
			residuals_vec[i] = (*found_prior_info).second.calc_residual(pars);
		}
	}
	return residuals_vec;
}
Beispiel #6
0
void RunStorage::update_run(int run_id, const Parameters &pars, const Observations &obs)
{
	//set run status flage to complete
	std::int8_t r_status = 1;
	check_rec_id(run_id);
	vector<double> par_data(pars.get_data_vec(par_names));
	vector<double> obs_data(obs.get_data_vec(obs_names));
	//write data to buffer at end of file and set buffer flag to 1
	std::int8_t buf_status = 0;
	std::int32_t buf_run_id = run_id;
	int end_of_runs = get_nruns();
	buf_stream.seekp(get_stream_pos(end_of_runs), ios_base::beg);
	buf_stream.write(reinterpret_cast<char*>(&buf_status), sizeof(buf_status));
	buf_stream.write(reinterpret_cast<char*>(&buf_run_id), sizeof(buf_run_id));
	buf_stream.write(reinterpret_cast<char*>(&r_status), sizeof(r_status));
	buf_stream.write(reinterpret_cast<char*>(par_data.data()), par_data.size() * sizeof(double));
	buf_stream.write(reinterpret_cast<char*>(obs_data.data()), obs_data.size() * sizeof(double));
	buf_status = 1;
	buf_stream.seekp(get_stream_pos(end_of_runs), ios_base::beg);
	buf_stream.write(reinterpret_cast<char*>(&buf_status), sizeof(buf_status));
	buf_stream.flush();
	//write data
	buf_stream.seekp(get_stream_pos(run_id), ios_base::beg);
	buf_stream.write(reinterpret_cast<char*>(&r_status), sizeof(r_status));
	//skip over info_txt and info_value fields
	buf_stream.seekp(sizeof(char)*info_txt_length+sizeof(double), ios_base::cur);
	buf_stream.write(reinterpret_cast<char*>(par_data.data()), par_data.size() * sizeof(double));
	buf_stream.write(reinterpret_cast<char*>(obs_data.data()), obs_data.size() * sizeof(double));
	buf_stream.flush();
	//reset flag for buffer at end of file to 0 to signal it is no longer relavent
	buf_status = 0;
	buf_stream.seekp(get_stream_pos(end_of_runs), ios_base::beg);
	buf_stream.write(reinterpret_cast<char*>(&buf_status), sizeof(buf_status));
	buf_stream.flush();
}
Beispiel #7
0
AbstractModel::SufficientStatisticsVector
MultivariateModel
::GetSufficientStatistics(const Realizations &R, const Observations& Obs) 
{
  /// Computation of the suffisient statistics of the model
  /// Basically, it is a vector (named S1 to S9) of vectors
  
  /// S1 <- y_ij * eta_ij    &    S2 <- eta_ij * eta_ij
  VectorType S1(m_NbTotalOfObservations), S2(m_NbTotalOfObservations);
  auto itS1 = S1.begin(), itS2 = S2.begin();
  for(size_t i = 0; i < m_NumberOfSubjects; ++i)
  {
      for(size_t j = 0; j < Obs.GetNumberOfTimePoints(i); ++j)
      {
          VectorType PC = ComputeParallelCurve(i, j);
          *itS1 = dot_product(PC, Obs.GetSubjectCognitiveScore(i, j));
          *itS2 = PC.squared_magnitude();
          ++itS1, ++itS2;
      }
  }
  
  /// S3 <- Ksi_i   &    S4 <- Ksi_i * Ksi_i
  VectorType S3 = R.at("Ksi");
  VectorType S4 = R.at("Ksi") % R.at("Ksi");
  
  /// S5 <- Tau_i   &    S6 <- Tau_i * Tau_i
  VectorType S5 = R.at("Tau");
  VectorType S6 = R.at("Tau") % R.at("Tau");
  
  /// S7 <- G 
  VectorType S7(1, R.at("G", 0));
  
  /// S8 <- beta_k
  VectorType S8((m_ManifoldDimension-1) * m_NbIndependentSources);
  ScalarType * itS8 = S8.memptr();
  for(size_t i = 0; i < S8.size(); ++i)
      itS8[i] = R.at("Beta#" + std::to_string(i), 0);
  
  /// S8 <- delta_k
  VectorType S9(m_ManifoldDimension - 1);
  ScalarType * itS9 = S9.memptr();
  for(size_t i = 1; i < S9.size(); ++i)
      itS9[i] = R.at("Delta#" + std::to_string(i), 0);

  return {S1, S2, S3, S4, S5, S6, S7, S8, S9};
  
}
Beispiel #8
0
int RunStorage::get_run(int run_id, Parameters &pars, Observations &obs, string &info_txt, double &info_value, bool clear_old)
{
	vector<double> par_data;
	vector<double> obs_data;

	int status = get_run(run_id, par_data, obs_data, info_txt, info_value);
	if (clear_old)
	{
	  pars.update(par_names, par_data);
	  obs.update(obs_names, obs_data);
	}
	else
	{
	  pars.update_without_clear(par_names, par_data);
	  obs.update_without_clear(obs_names, obs_data);
	}
	return status;
}
TEST(SFM_DATA_FILTERS, LargestViewTrackCC)
{
  // Init a scene with 5 Views & poses
  SfM_Data sfm_data;
  init_scene(sfm_data, 5);

  // Fill with some tracks
  //
  //- View 0,1,2 are sharing 3 tracks
  // TrackId 0 -> 0-1-2
  // TrackId 1 -> 0-1-2
  // TrackId 2 -> 0-1-2
  //
  // While view 3-4 are sharing 1 track
  // TrackId 3 -> 3-4

  Observations obs;
  obs[0] = Observation( Vec2(10,20), 0);
  obs[1] = Observation( Vec2(30,10), 1);
  obs[2] = Observation( Vec2(30,10), 1);
  sfm_data.structure[0].obs = obs;
  sfm_data.structure[0].X = Vec3::Random();

  sfm_data.structure[1].obs = obs;
  sfm_data.structure[1].X = Vec3::Random();
  sfm_data.structure[2].obs = obs;
  sfm_data.structure[2].X = Vec3::Random();

  obs.clear();
  obs[3] = Observation( Vec2(10,20), 0);
  obs[4] = Observation( Vec2(10,20), 0);
  sfm_data.structure[3].obs = obs;
  sfm_data.structure[3].X = Vec3::Random();

  // Track of the SfM_Data scene contains two connected component
  // One for view (0,1,2)
  // One for the view (3,4)
  // Test that function to keep the largest one is working
  EXPECT_EQ (4, sfm_data.GetLandmarks().size());
  EXPECT_FALSE (IsTracksOneCC(sfm_data));
  KeepLargestViewCCTracks(sfm_data);
  EXPECT_EQ (3, sfm_data.GetLandmarks().size());
}
void Simulator::perturbProjections(Observations& obs, double sigma){

  for (Observations::iterator it = obs.begin(); it != obs.end(); ++it){

    double dx = Simulator::getGaussianSample(0,sigma);
    double dy = Simulator::getGaussianSample(0,sigma);

    ROS_WARN("observation moved by %f %f", dx,dy);

    CvPoint2D32f p = it->second;

    p.x += dx; // zero centered normal
    p.y += dy;

    obs[it->first] = p;

  }

}
Beispiel #11
0
vector<char> Serialization::serialize(const Parameters &pars, const vector<string> &par_names_vec, const Observations &obs, const vector<string> &obs_names_vec)
{

	assert(pars.size() == par_names_vec.size());
	assert(obs.size() == obs_names_vec.size());
	vector<char> serial_data;
	size_t npar = par_names_vec.size();
	size_t nobs = obs_names_vec.size();
	size_t par_buf_sz = npar * sizeof(double);
	size_t obs_buf_sz = nobs * sizeof(double);
	serial_data.resize(par_buf_sz + obs_buf_sz, Parameters::no_data);

	char *buf = &serial_data[0];
	vector<double> par_data = pars.get_data_vec(par_names_vec);
	w_memcpy_s(buf, par_buf_sz, &par_data[0], par_data.size() * sizeof(double));

	vector<double> obs_data = obs.get_data_vec(obs_names_vec);
	w_memcpy_s(buf+par_buf_sz, obs_buf_sz, &obs_data[0], obs_data.size() * sizeof(double));
	return serial_data;
}
Beispiel #12
0
 void SensorArray::getObservations(Observations& observations)
 {
     std::vector<SensorPtr>::iterator sensIter;
     size_t i = 0;
     for (sensIter = sensors.begin(); sensIter != sensors.end(); ++sensIter) 
     {
         AssertMsg(i < observations.size(), "There are more built-in sensors than observations in AgentInitInfo");
         SimEntitySet::const_iterator entIter;
         const SimEntitySet entSet = Kernel::instance().GetSimContext()->getSimulation()->GetEntities((*sensIter)->getTypes());
         for (entIter = entSet.begin(); entIter != entSet.end(); ++entIter) 
         {
             (*sensIter)->process(GetEntity(), (*entIter));
         }
         observations[i] = (*sensIter)->getObservation(GetEntity());
         i++;
     }
 }
Beispiel #13
0
Point3 Triangulate(const Observations& observations, double* error, bool optimize)
{
    const int num_points = static_cast<int>(observations.size());

    Mat A{2 * num_points, 3};
    Vec b{2 * num_points};
    Vec x{3};

    for (int i = 0; i < num_points; i++)
    {
        A.row(2 * i + 0) = observations[i].m_R.row(0) - observations[i].m_point.x() * observations[i].m_R.row(2);
        A.row(2 * i + 1) = observations[i].m_R.row(1) - observations[i].m_point.y() * observations[i].m_R.row(2);

        b(2 * i + 0) = observations[i].m_t(2) * observations[i].m_point.x() - observations[i].m_t(0);
        b(2 * i + 1) = observations[i].m_t(2) * observations[i].m_point.y() - observations[i].m_t(1);
    }

    // Find the least squares result
    x = A.colPivHouseholderQr().solve(b);

    TriangulationResidual functor{observations};

    // Run a non-linear optimization to refine the result
    if (optimize)
    {
        Eigen::NumericalDiff<TriangulationResidual> numDiff{functor};
        Eigen::LevenbergMarquardt<Eigen::NumericalDiff<TriangulationResidual>> lm{numDiff};

        lm.parameters.ftol   = 1.0e-5;
        lm.parameters.xtol   = 1.0e-5;
        lm.parameters.maxfev = 200;

        const auto status = lm.minimize(x);
    }

    if (error != nullptr)
    {
        Vec residuals{2 * num_points};
        functor(x, residuals);
        *error = residuals.squaredNorm();
    }

    return x;
}
 /// Triangulate a given track from a selection of observations
 Vec3 track_sample_triangulation(
   const SfM_Data & sfm_data,
   const Observations & obs,
   const std::set<IndexT> & samples) const
 {
   Triangulation trianObj;
   for (auto& it : samples)
   {
     const IndexT & idx = it;
     Observations::const_iterator itObs = obs.begin();
     std::advance(itObs, idx);
     const View * view = sfm_data.views.at(itObs->first).get();
     const IntrinsicBase * cam = sfm_data.getIntrinsics().at(view->id_intrinsic).get();
     const Pose3 & pose = sfm_data.poses.at(view->id_pose);
     trianObj.add(
       cam->get_projective_equivalent(pose),
       cam->get_ud_pixel(itObs->second.x));
   }
   return trianObj.compute();
 }
Beispiel #15
0
int  RunStorage::get_observations(int run_id, Observations &obs)
{
	std::int8_t r_status;
	vector<char> info_txt_buf;
	info_txt_buf.resize(info_txt_length, '\0');
	double info_value;

	check_rec_id(run_id);

	size_t n_par = par_names.size();
	size_t n_obs = obs_names.size();
	vector<double> obs_data;
	obs_data.resize(n_obs);
	buf_stream.seekg(get_stream_pos(run_id), ios_base::beg);
	buf_stream.read(reinterpret_cast<char*>(&r_status), sizeof(r_status));
	buf_stream.read(reinterpret_cast<char*>(&info_txt_buf[0]), sizeof(char)*info_txt_length);
	buf_stream.read(reinterpret_cast<char*>(&info_value), sizeof(double));
	buf_stream.seekg(n_par*sizeof(double), ios_base::cur);
	buf_stream.read(reinterpret_cast<char*>(obs_data.data()), n_obs*sizeof(double));
	int status = r_status;
	obs.update(obs_names, obs_data);
	return status;
}
Beispiel #16
0
void RunManagerGenie::run()
{
	int nexec = comline_vec.size();
	const vector<string> &par_name_vec = file_stor.get_par_name_vec();
	const vector<string> &obs_name_vec = file_stor.get_obs_name_vec();
	int npar = par_name_vec.size();
	int nobs = obs_name_vec.size();
	int ntpl = tplfile_vec.size();
	int nins = insfile_vec.size();

	stringstream apar;
	stringstream aobs;
	stringstream execnames;
	stringstream tplfle;
	stringstream infle;
	stringstream insfle;
	stringstream outfle;

	 vector<double> par_val;
	 vector<double> obs_val;
	 vector<int> ifail;

	// This is necessary to support restart as some run many already be complete
	vector<int> run_id_vec = get_outstanding_run_ids();
	int nruns = run_id_vec.size();

	 par_val.clear();
	 obs_val.resize(nruns*nobs);
	 ifail.resize(nruns);
	for (int i_run : run_id_vec)
	{
	     Parameters tmp_pars;
	     file_stor.get_parameters(i_run, tmp_pars);
		 vector<double> tmp_vec = tmp_pars.get_data_vec(par_name_vec);
		 par_val.insert(par_val.end(), tmp_vec.begin(), tmp_vec.end());
	}

	vector<string> par_name_vec_lwr = par_name_vec;
	for (auto &ipar : par_name_vec_lwr)
	{
		lower_ip(ipar);
	}
	vector<string> obs_name_vec_lwr = obs_name_vec;
	for (auto &iobs : obs_name_vec_lwr)
	{
		lower_ip(iobs);
	}
	std::copy(par_name_vec_lwr.begin(), par_name_vec_lwr.end(),std::ostream_iterator<std::string>(apar,"\n"));
	std::copy(obs_name_vec_lwr.begin(), obs_name_vec_lwr.end(),std::ostream_iterator<std::string>(aobs,"\n"));
	std::copy(comline_vec.begin(), comline_vec.end(),std::ostream_iterator<std::string>(execnames,"\n"));
	std::copy(tplfile_vec.begin(), tplfile_vec.end(),std::ostream_iterator<std::string>(tplfle,"\n"));
	std::copy(inpfile_vec.begin(), inpfile_vec.end(),std::ostream_iterator<std::string>(infle,"\n"));
	std::copy(insfile_vec.begin(), insfile_vec.end(),std::ostream_iterator<std::string>(insfle,"\n"));
	std::copy(outfile_vec.begin(), outfile_vec.end(),std::ostream_iterator<std::string>(outfle,"\n"));

	#ifdef OS_WIN
	GENIE_INTERFACE(&nruns, &nexec, String2CharPtr(execnames.str()).get_char_ptr(), &npar, &nobs,
	String2CharPtr(apar.str()).get_char_ptr(), String2CharPtr(aobs.str()).get_char_ptr(),
					&par_val[0], &obs_val[0], &ntpl, &nins, String2CharPtr(tplfle.str()).get_char_ptr(),
					String2CharPtr(infle.str()).get_char_ptr(), String2CharPtr(insfle.str()).get_char_ptr(),
					String2CharPtr(outfle.str()).get_char_ptr(),
					String2CharPtr(host).get_char_ptr(), String2CharPtr(id).get_char_ptr(), &ifail[0]);
	#endif
	#ifdef OS_LINUX
	throw(PestError("Error: Genie run manager is not supported under linux"));
	#endif

	total_runs += nruns;
	Parameters pars;
	Observations obs;
	for (int i=0; i<nruns; ++i)
	{
		if (ifail[i] == 0)
		{
			int run_id = run_id_vec[i];
			pars.clear();
			vector<double> i_par_vec(par_val.begin() + i*npar, par_val.begin() + (i + 1)*npar);
			pars.insert(par_name_vec, i_par_vec);
			obs.clear();
			vector<double> i_obs_vec(obs_val.begin() + i*nobs, obs_val.begin() + (i + 1)*nobs);
			obs.insert(obs_name_vec, i_obs_vec);
			file_stor.update_run(run_id, pars, obs);
		}
		else
		{
			file_stor.set_run_nfailed(i, max_n_failure);
		}
	}
}
Beispiel #17
0
void RunManagerAbstract::initialize(const Parameters &model_pars, const Observations &obs, const string &_filename)
{
	file_stor.reset(model_pars.get_keys(), obs.get_keys(), _filename);
}
Beispiel #18
0
Observations
ReadData
::ReadObservations(DataSettings &DS) 
{
  Observations Obs;
  
  std::ifstream IndivID(DS.GetPathToGroup());
  std::ifstream TimePointsFile(DS.GetPathToTimepoints());
  std::ifstream LandmarksFile(DS.GetPathToLandmarks());
  std::ifstream CognitiveScoresFile(DS.GetPathToCognitiveScores());
      
  std::string GroupLine, TimePointsLine, LandmarksLine, CognitiveScoresLine;
  unsigned int CurrentSubjectID = -1;
  
  VectorType TimePoints;
  std::vector<VectorType> Landmarks;
  std::vector<VectorType> CognitiveScores;
  
  while(getline(IndivID, GroupLine))
  {
    if(CurrentSubjectID == -1) CurrentSubjectID = std::stoi(GroupLine);
    
    unsigned int NewSubjectID = std::stoi(GroupLine);
    getline(TimePointsFile, TimePointsLine);
    if(DS.LandmarkPresence())        getline(LandmarksFile, LandmarksLine);
    if(DS.CognitiveScoresPresence()) getline(CognitiveScoresFile, CognitiveScoresLine);
    
    /// New subject
    if(NewSubjectID != CurrentSubjectID)
    {
      IndividualObservations Individual(TimePoints);
      if(DS.LandmarkPresence())        Individual.AddLandmarks(Landmarks);
      if(DS.CognitiveScoresPresence()) Individual.AddCognitiveScores(CognitiveScores);
      Obs.AddIndividualData(Individual);
                  
      CurrentSubjectID = NewSubjectID;
      TimePoints.clear();
      Landmarks.clear();
      CognitiveScores.clear();
    }

    TimePoints.push_back(stod(TimePointsLine));
    if(DS.LandmarkPresence()) 
    {
      VectorType NewObs(DS.GetLandmarksDimension());
      int i = 0;
      std::stringstream LineStream(LandmarksLine);
      std::string cell;
      while(std::getline(LineStream, cell, ','))
      {
        NewObs(i) = std::stod(cell);
        ++i;
      }
      
      Landmarks.push_back(NewObs);
    }
    if(DS.CognitiveScoresPresence())
    {
        
      VectorType NewObs(DS.GetCognitiveScoresDimension());
      int i = 0;
      std::stringstream LineStream(CognitiveScoresLine);
      std::string cell;
      while(std::getline(LineStream, cell, ','))
      {
        NewObs(i) = std::stod(cell);
        ++i;
      }
      CognitiveScores.push_back(NewObs);
    }
  }
  
  IndividualObservations Individual(TimePoints);
  if(DS.LandmarkPresence())        Individual.AddLandmarks(Landmarks);
  if(DS.CognitiveScoresPresence()) Individual.AddCognitiveScores(CognitiveScores);
  Obs.AddIndividualData(Individual);
  
  
  return Obs;
}
Beispiel #19
0
void
MultivariateModel
::Initialize(const Observations& Obs) 
{
  /// The function initialize different attributes of the model
  /// As well as the specific random variables and their realizations used by the model
  // TODO : Pitfall : How to have a smart initialization, that may come out of a .txt / .csv file
  // TODO : instead of using the same initialization or modifiying it in the code
  // TODO : Some cases may fall in between the two cases (default values needed)
  
  
  /// Data-related attributes
  m_ManifoldDimension         = Obs.GetSubjectObservations(0).GetCognitiveScore(0).size();
  m_NumberOfSubjects          = Obs.GetNumberOfSubjects();
  m_IndividualObservationDate = Obs.GetObservations();
  m_SubjectTimePoints         = Obs.GetObservations();
  m_NbTotalOfObservations     = Obs.GetTotalNumberOfObservations();
  m_SumObservations           = Obs.GetTotalSumOfCognitiveScores();
  
  /// Initialize the size of some parameters
  m_Deltas.set_size(m_ManifoldDimension);
  m_Block.set_size(m_ManifoldDimension);
  
  /// Population variables 
  /// (Initialization of the random variable m_Random Variable 
  /// and the associated number of realizations m_RealizationPerRandomVariable)
  m_Noise = std::make_shared<GaussianRandomVariable>(0.0, 0.00001);
  
  m_RandomVariables.AddRandomVariable("G", "Gaussian", {0.12, 0.0001 * 0.0001});
  m_RealizationsPerRandomVariable["G"] = 1;
  
  for(size_t i = 1; i < m_ManifoldDimension; ++i)
  {
    std::string NameDelta = "Delta#" + std::to_string(i);
    m_RandomVariables.AddRandomVariable(NameDelta, "Gaussian", {0, 0.003 * 0.003});
    m_RealizationsPerRandomVariable[NameDelta] = 1;
  }
  
  for(int i = 0; i < m_NbIndependentSources*(m_ManifoldDimension - 1); ++i)
  {
    std::string Name = "Beta#" + std::to_string(i);
    m_RandomVariables.AddRandomVariable(Name, "Gaussian", {0, 0.001*0.001});
    m_RealizationsPerRandomVariable[Name] = 1;
  }
  
  /// Individual realizations 
  /// (Initialization of the random variable m_Random Variable 
  /// and the associated number of realizations m_RealizationPerRandomVariable)
  m_RandomVariables.AddRandomVariable("Ksi", "Gaussian", {0.13, 0.0004});
  m_RealizationsPerRandomVariable["Ksi"] = m_NumberOfSubjects;
  
  m_RandomVariables.AddRandomVariable("Tau", "Gaussian", {75, 0.025});
  m_RealizationsPerRandomVariable["Tau"] = m_NumberOfSubjects;
  
  for(int i = 0; i < m_NbIndependentSources; ++i)
  {
    std::string Name = "S#" + std::to_string(i);
    m_RandomVariables.AddRandomVariable(Name, "Gaussian", {0.0, 1});
    m_RealizationsPerRandomVariable[Name] = m_NumberOfSubjects;
  }
}
Beispiel #20
0
void RunManagerSerial::run()
{
	int ifail;
	int success_runs = 0;
	int prev_sucess_runs = 0;
	const vector<string> &par_name_vec = file_stor.get_par_name_vec();
	const vector<string> &obs_name_vec = file_stor.get_obs_name_vec();
	int npar = par_name_vec.size();
	int nobs = obs_name_vec.size();
	int ntpl = tplfile_vec.size();
	int nins = insfile_vec.size();
	stringstream message;		
	bool isDouble = true;
	bool forceRadix = true;
	//TemplateFiles tpl_files(isDouble, forceRadix, tplfile_vec, inpfile_vec, par_name_vec);
	//InstructionFiles ins_files(insfile_vec, outfile_vec);
	std::vector<double> obs_vec;
	// This is necessary to support restart as some run many already be complete
	vector<int> run_id_vec;
	int nruns = get_outstanding_run_ids().size();
	while (!(run_id_vec = get_outstanding_run_ids()).empty())
	{
		for (int i_run : run_id_vec)
		{			
			//first delete any existing input and output files			
			for (auto &out_file : outfile_vec)
			{
				if((check_exist_out(out_file)) && (remove(out_file.c_str()) != 0))
					throw PestError("model interface error: Cannot delete existing model output file "+out_file);				
			}
			for (auto &in_file : inpfile_vec)
			{
				if ((check_exist_out(in_file)) && (remove(in_file.c_str()) != 0))
					throw PestError("model interface error: Cannot delete existing model input file " + in_file);
			}
			Observations obs;
			vector<double> par_values;
			Parameters pars;
			file_stor.get_parameters(i_run, pars);						
			try {
				std::cout << string(message.str().size(), '\b');
				message.str("");
				message << "(" << success_runs << "/" << nruns << " runs complete)";
				std::cout << message.str();
				OperSys::chdir(run_dir.c_str());
				for (auto &i : par_name_vec)
				{
					par_values.push_back(pars.get_rec(i));
				}
				if (std::any_of(par_values.begin(), par_values.end(), OperSys::double_is_invalid))
				{
					throw PestError("Error running model: invalid parameter value returned");
				}
				wrttpl_(&ntpl, StringvecFortranCharArray(tplfile_vec, 50).get_prt(),
					StringvecFortranCharArray(inpfile_vec, 50).get_prt(),
					&npar, StringvecFortranCharArray(par_name_vec, 50, pest_utils::TO_LOWER).get_prt(),
					&par_values[0], &ifail);
				if (ifail != 0)
				{
					throw PestError("Error processing template file");
				}								
				for (int i = 0, n_exec = comline_vec.size(); i < n_exec; ++i)
				{
					system(comline_vec[i].c_str());
				}
				obs_vec.resize(nobs, RunStorage::no_data);
				readins_(&nins, StringvecFortranCharArray(insfile_vec, 50).get_prt(),
					StringvecFortranCharArray(outfile_vec, 50).get_prt(),
					&nobs, StringvecFortranCharArray(obs_name_vec, 50, pest_utils::TO_LOWER).get_prt(),
					&obs_vec[0], &ifail);
				if (ifail != 0)
				{
					throw PestError("Error processing instruction file");
				}				
				// check parameters and observations for inf and nan
				if (std::any_of(par_values.begin(), par_values.end(), OperSys::double_is_invalid))
				{
					throw PestError("Error running model: invalid parameter value returned");
				}
				if (std::any_of(obs_vec.begin(), obs_vec.end(), OperSys::double_is_invalid))
				{
					throw PestError("Error running model: invalid observation value returned");
				}

				/*
				// IOPP version
				for (int i = 0, n_exec = comline_vec.size(); i < n_exec; ++i)
				{
				system(comline_vec[i].c_str());
				}
				//pest_utils::thread_flag* tf1(false);
				//pest_utils::thread_flag* tf2(false);
				//w_run_commands(tf1,tf2,comline_vec);
				ins_files.read(obs_name_vec, obs);
				*/

				success_runs += 1;
				
				pars.clear();
				pars.insert(par_name_vec, par_values);
				obs.clear();
				obs.insert(obs_name_vec, obs_vec);								
				file_stor.update_run(i_run, pars, obs);
				
			}
			catch (const std::exception& ex)
			{
				file_stor.update_run_failed(i_run);
				cerr << endl;
				cerr << "  " << ex.what() << endl;
				cerr << "  Aborting model run" << endl << endl;
			}
			catch (...)
			{
				file_stor.update_run_failed(i_run);
				cerr << endl;
				cerr << "  Error running model" << endl;
				cerr << "  Aborting model run" << endl << endl;
			}
		}
	}

	total_runs += success_runs;
	std::cout << string(message.str().size(), '\b');
	message.str("");
	message << "(" << success_runs << "/" << nruns << " runs complete";
	if (prev_sucess_runs > 0)
	{
		message << " and " << prev_sucess_runs << " additional run completed previously";
	}
	message << ")";
	std::cout << message.str();
	if (success_runs < nruns)
	{			cout << endl << endl;
		cout << "WARNING: " << nruns - success_runs << " out of " << nruns << " runs failed" << endl << endl;
	}
	std::cout << endl << endl;
}
Beispiel #21
0
Observations
MultivariateModel
::SimulateData(io::DataSettings &DS) 
{
  /// This function simulates observations (Patients and their measurements y_ij at different time points t_ij) 
  /// according to the model, with a given noise level e_ij, such that y_ij = f(t_ij) + e_ij
  /// Their is two option: 
  /// 1) The model is not initialized (neither random variables of number of realizations) and it has to be
  /// This case corresponds to simulated data used to test the model, meaning if it can recover the random variables
  /// used to simulate the data
  /// 2) The model is already initialized, thus it should rely on its current state (random variables, realizations, ...)
  /// This case corresponds to new data, for a dataset augmentation for instance
  
  // TODO :
  /// PITFALL  : As for now, only the first option is implemented
  /// PITFALL2 : Take a closer look at / merge with InitializeFakeRandomVariables 
  
  
  /// Initialize the model
  m_ManifoldDimension = DS.GetCognitiveScoresDimension();
  m_NumberOfSubjects  = DS.GetNumberOfSimulatedSubjects();
  
  m_RealizationsPerRandomVariable["G"] = 1;
  
  for(size_t i = 1; i < m_ManifoldDimension; ++i)
    m_RealizationsPerRandomVariable["Delta#" + std::to_string(i)] = 1;

  for(size_t i = 0; i <  m_NbIndependentSources*(m_ManifoldDimension - 1); ++i)
    m_RealizationsPerRandomVariable["Beta#" + std::to_string(i)] = 1;

  m_RealizationsPerRandomVariable["Ksi"] = m_NumberOfSubjects;
  m_RealizationsPerRandomVariable["Tau"] = m_NumberOfSubjects;

  for(int i = 0; i < m_NbIndependentSources; ++i)
    m_RealizationsPerRandomVariable["S#" + std::to_string(i)] = m_NumberOfSubjects;

  auto R = SimulateRealizations();
  
  /// Update the model
  m_G = exp(R.at("G", 0));
  ComputeDeltas(R);
  ComputeOrthonormalBasis();
  ComputeAMatrix(R);
  ComputeSpaceShifts(R);
  ComputeBlock(R);
  
  /// Simulate the data
  std::random_device RD;
  std::mt19937 RNG(RD());
  std::uniform_int_distribution<int> Uni(DS.GetMinimumNumberOfObservations(), DS.GetMaximumNumberOfObservations());
  UniformRandomVariable NumberOfTimePoints(60, 95);
  GaussianRandomVariable Noise(0, m_Noise->GetVariance());
  
  /// Simulate the data
  Observations Obs;
  for(int i = 0; i < m_NumberOfSubjects; ++i)
  { 
    /// Get a random number of timepoints and sort them
    VectorType T = NumberOfTimePoints.Samples(Uni(RNG));
    T.sort();
    m_SubjectTimePoints.push_back(T);
    
    /// Simulate the data base on the time-points
    IndividualObservations IO(T);   
    std::vector<VectorType> Landmarks;
    for(size_t j = 0; j < T.size(); ++j)
    {
      Landmarks.push_back(ComputeParallelCurve(i, j) + Noise.Samples(m_ManifoldDimension));
    }
    
    IO.AddLandmarks(Landmarks);
    Obs.AddIndividualData(IO);
  }
  
  /// Initialize the observation and model attributes
  Obs.InitializeGlobalAttributes();
  m_IndividualObservationDate = Obs.GetObservations();
  m_SumObservations           = Obs.GetTotalSumOfLandmarks();
  m_NbTotalOfObservations     = Obs.GetTotalNumberOfObservations();
  
  return Obs;
  
}
  /// Robustly try to estimate the best 3D point using a ransac Scheme
  /// Return true for a successful triangulation
  bool robust_triangulation(
    const SfM_Data & sfm_data,
    const Observations & obs,
    Vec3 & X,
    const IndexT min_required_inliers = 3,
    const IndexT min_sample_index = 3) const
  {
    const double dThresholdPixel = 4.0; // TODO: make this parameter customizable

    const IndexT nbIter = obs.size(); // TODO: automatic computation of the number of iterations?

    // - Ransac variables
    Vec3 best_model;
    std::set<IndexT> best_inlier_set;
    double best_error = std::numeric_limits<double>::max();

    // - Ransac loop
    for (IndexT i = 0; i < nbIter; ++i)
    {
      std::vector<size_t> vec_samples;
      robust::UniformSample(min_sample_index, obs.size(), &vec_samples);
      const std::set<IndexT> samples(vec_samples.begin(), vec_samples.end());

      // Hypothesis generation.
      const Vec3 current_model = track_sample_triangulation(sfm_data, obs, samples);

      // Test validity of the hypothesis
      // - chierality (for the samples)
      // - residual error

      // Chierality (Check the point is in front of the sampled cameras)
      bool bChierality = true;
      for (auto& it : samples){
        Observations::const_iterator itObs = obs.begin();
        std::advance(itObs, it);
      	const View * view = sfm_data.views.at(itObs->first).get();
        const IntrinsicBase * cam = sfm_data.getIntrinsics().at(view->id_intrinsic).get();
        const Pose3 & pose = sfm_data.poses.at(view->id_pose);
        const double z = pose.depth(current_model); // TODO: cam->depth(pose(X));
        bChierality &= z > 0;
      }

      if (!bChierality)
        continue;

      std::set<IndexT> inlier_set;
      double current_error = 0.0;
      // Classification as inlier/outlier according pixel residual errors.
      for (Observations::const_iterator itObs = obs.begin();
          itObs != obs.end(); ++itObs)
      {
        const View * view = sfm_data.views.at(itObs->first).get();
        const IntrinsicBase * intrinsic = sfm_data.getIntrinsics().at(view->id_intrinsic).get();
        const Pose3 & pose = sfm_data.poses.at(view->id_pose);
        const Vec2 residual = intrinsic->residual(pose, current_model, itObs->second.x);
        const double residual_d = residual.norm();
        if (residual_d < dThresholdPixel)
        {
          inlier_set.insert(itObs->first);
          current_error += residual_d;
        }
        else
        {
          current_error += dThresholdPixel;
        }
      }
      // Does the hypothesis is the best one we have seen and have sufficient inliers.
      if (current_error < best_error && inlier_set.size() >= min_required_inliers)
      {
        X = best_model = current_model;
        best_inlier_set = inlier_set;
        best_error = current_error;
      }
    }
    return !best_inlier_set.empty();
  }
Beispiel #23
0
int YAMRSlave::run_model(Parameters &pars, Observations &obs)
{
	int success = 1;
	//bool isDouble = true;
	//bool forceRadix = true;
	//TemplateFiles tpl_files(isDouble,forceRadix,tplfile_vec,inpfile_vec,par_name_vec);
	//InstructionFiles ins_files(insfile_vec,outfile_vec,obs_name_vec);
	std::vector<double> obs_vec;
	try 
	{
	//	message.str("");
		int ifail;
		int ntpl = tplfile_vec.size();
		int npar = pars.size();
		vector<string> par_name_vec;
		vector<double> par_values;
		for(auto &i : pars)
		{
			par_name_vec.push_back(i.first);
			par_values.push_back(i.second);
		}
		wrttpl_(&ntpl, StringvecFortranCharArray(tplfile_vec, 50).get_prt(),
			StringvecFortranCharArray(inpfile_vec, 50).get_prt(),
			&npar, StringvecFortranCharArray(par_name_vec, 50, pest_utils::TO_LOWER).get_prt(),
			par_values.data(), &ifail);
		if(ifail != 0)
		{
			throw PestError("Error processing template file:" + tpl_err_msg(ifail));
		}
		//tpl_files.writtpl(par_values);

		// update parameter values
		pars.clear();
		for (int i=0; i<npar; ++i)
		{
			pars[par_name_vec[i]] = par_values[i];
		}

		// run model
		for (auto &i : comline_vec)
		{
			ifail = system(i.c_str());
			if(ifail != 0)
		{
			cerr << "Error executing command line: " << i << endl;
			throw PestError("Error executing command line: " + i);
		}
		}
		// process instructio files
		int nins = insfile_vec.size();
		int nobs = obs_name_vec.size();
		std::vector<double> obs_vec;
		obs_vec.resize(nobs, -9999.00);
		readins_(&nins, StringvecFortranCharArray(insfile_vec, 50).get_prt(),
			StringvecFortranCharArray(outfile_vec, 50).get_prt(),
			&nobs, StringvecFortranCharArray(obs_name_vec, 50, pest_utils::TO_LOWER).get_prt(),
			obs_vec.data(), &ifail);
		if(ifail != 0)
		{
			throw PestError("Error processing template file");
		}
		//obs_vec = ins_files.readins();


		// check parameters and observations for inf and nan
		if (std::any_of(par_values.begin(), par_values.end(), OperSys::double_is_invalid))
		{
			throw PestError("Error running model: invalid parameter value returned");
		}
		if (std::any_of(obs_vec.begin(), obs_vec.end(), OperSys::double_is_invalid))
		{
			throw PestError("Error running model: invalid observation value returned");
		}
		// update observation values
		obs.clear();
		for (int i=0; i<nobs; ++i)
		{
			obs[obs_name_vec[i]] = obs_vec[i];
		}

	}
	catch(const std::exception& ex)
	{
		cerr << endl;
		cerr << "   " << ex.what() << endl;
		cerr << "   Aborting model run" << endl << endl;
		success = 0;
	}
	catch(...)
	{
		cerr << "   Error running model" << endl;
		cerr << "   Aborting model run" << endl;
		success = 0;
	}
	return success;
}
int main(int argc, char** argv)
{
	if(argc == 1)
	{
		printf("Flags:\n");
		printf("REQUIRED\n");
		printf(" -cnn=<trained_cnn_file>        Sets CNN to be used for running\n");
		printf(" -video=<videoName>             Sets video to run CNN over\n");
		printf(" -video_start_time=<int>        The start of the video from the DB in seconds.\n");
		printf(" -video_id=<int>                The unique id for the video.\n");
		printf(" -cnn_config_id=<int>           The unique id for the cnn_config\n");
		printf(" One of the following describing how CNN was trained:\n");
		printf("   -carveDown_both_scaled\n");
		printf("   -carveDown_both_raw\n");
		printf("   -carveDown_vth\n");
		printf("   -carveDown_htv\n");
		printf("   -scaleDown\n");
		printf("   -distortDown\n");
		printf("   -random_crop\n");
		printf("OPTIONAL\n");
		printf(" -device=<device_num>           Device to run CNN (and seamcarving if applicable) on. Default 0. Can't use if running on BOINC\n");
		printf(" -jump=<uint>                   Will run on every \"jump-th\" frame. Default 1.\n");
		printf(" -batchSize=<uint>              How many frames that will be seamcarved before running through cnn. Default 10.\n");
		return 0;
	}

	string cnn = "..";
	string video_path = "..";
	int scaleType = -674;
	unsigned int jump = 1;
	int device = 0;
	unsigned int batchSize = 10;
	int fps = 10;
	int video_start_time = -1;
	int video_id = -1;
	int cnn_config_id = -1;

	//get cmd line args
	for(int i = 1; i < argc; i++)
	{
		string arg(argv[i]);
		if(arg.find("-cnn=") != string::npos)
			cnn = arg.substr(arg.find('=')+1);
		else if(arg.find("-video=") != string::npos)
			video_path = arg.substr(arg.find('=')+1);
		else if(arg.find("-video_id=") != string::npos)
			video_id = stoi(arg.substr(arg.find('=')+1));
		else if(arg.find("-cnn_config_id=") != string::npos)
			cnn_config_id = stoi(arg.substr(arg.find('=')+1));
		else if(arg.find("-video_start_time=") != string::npos)
			video_start_time = stoi(arg.substr(arg.find('=')+1));
		else if(arg == "-carveDown_both_scaled")
			scaleType = CARVE_DOWN_BOTH_SCALED;
		else if(arg == "-carveDown_both_raw")
			scaleType = CARVE_DOWN_BOTH_RAW;
		else if(arg == "-carveDown_vth")
			scaleType = CARVE_DOWN_VTH;
		else if(arg == "-carveDown_htv")
			scaleType = CARVE_DOWN_HTV;
		else if(arg == "-scaleDown")
			scaleType = SCALE_DOWN;
		else if(arg == "-distortDown")
			scaleType = DISTORT_DOWN;
		else if(arg == "random_crop")
			scaleType = RANDOM_CROP;
		else if(arg.find("-device=") != string::npos)
			device = stoi(arg.substr(arg.find('=')+1));
		else if(arg.find("-jump=") != string::npos)
			jump = stoi(arg.substr(arg.find('=')+1));
		else if(arg.find("-batchSize=") != string::npos)
			batchSize = stoi(arg.substr(arg.find('=')+1));
		else
		{
			printf("Unknown arg \"%s\"\n", argv[i]);
			return 0;
		}
	}

	if(cnn == ".." || video_path == ".." || video_id == -1 || video_start_time == -1 || scaleType == -674 || cnn_config_id == -1)
	{
		printf("You must have all the required args.\n");
		return 0;
	}

	#ifdef _BOINC_APP_
	int retval;
	cl_device_id cldevice;
	cl_platform_id platform;
	boinc_init_diagnostics(BOINC_DIAG_MEMORYLEAKCHECKENABLED);
	BOINC_OPTIONS options;
	boinc_options_defaults(options);
	options.multi_thread = true;  // for multiple threads in OpenCL
	options.multi_process = true; // for multiple processes in OpenCL?
	options.normal_thread_priority = true; // so GPUs will run at full speed
	boinc_init_options(&options);
	boinc_init();

	if(!boinc_is_standalone())
	{
		retval = boinc_get_opencl_ids(argc, argv, -1, &cldevice, &platform);
	    if (retval) {
	        fprintf(stderr, 
	            "Error: boinc_get_opencl_ids() failed with error %d\n", 
	            retval
	        );
	        return 1;
	    }
	}

	#endif

	string resolved_cnn = getBoincFilename(cnn);
	string resolved_video_path = getBoincFilename(video_path);


	//set up net
	Net net(resolved_cnn.c_str());
	
	#ifdef _BOINC_APP_
		if(!boinc_is_standalone())
			net.setDevice(cldevice,platform);
		else
			net.setDevice(device);
	#else
		net.setDevice(device);
	#endif
	if(!net.finalize())
	{
		#ifdef _BOINC_APP_
		boinc_finish(-1);
		#endif
		return -1;
	}
	// seamcarve_setDevice(device);

	// int inputHeight = net.getInputHeight();
	int inputWidth = net.getInputWidth();

	int inputSize = inputWidth;
	cv::Size cvSize = cv::Size(inputSize, inputSize);

	#ifdef _BOINC_APP_
	if(readCheckpoint())
	{
		printf("Continuing from checkpoint\n");
	}
	else
	{
		printf("No Checkpoint found. Starting from beginning\n");
	}
	#endif

	//set up video
	VideoCapture video(resolved_video_path.c_str());
	if(!video.isOpened())
	{
		cout << "Could not open video: " << resolved_video_path << endl;
		#ifdef _BOINC_APP_
		boinc_finish(-1);
		#endif
		return -1;
	}

	//get to current frame
	for(int i = 0; i < curFrame; i++)
		video.grab();

	bool cont = true;
	vector<int> calcClasses;
	vector<vector<double> > curConfidences;
	Mat frame;

	int frameWidth = video.get(CV_CAP_PROP_FRAME_WIDTH);
	int frameHeight = video.get(CV_CAP_PROP_FRAME_HEIGHT);
	//used for random crop
	// default_random_engine gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
	// uniform_int_distribution<int> disI(0,frameHeight - inputSize);
	// uniform_int_distribution<int> disJ(0,frameWidth - inputSize);

	//used for seamcarving
	int vseams = frameWidth  - inputSize;
	int hseams = frameHeight - inputSize;
	int numSeams = frameWidth - frameHeight;
	vector<thread> thr(batchSize);
	vector<Seamcarver> carvers(batchSize);

	while(cont)
	{
		#ifdef _BOINC_APP_
		boinc_fraction_done(video.get(CV_CAP_PROP_POS_AVI_RATIO) * 100.0);
		// if(boinc_time_to_checkpoint())
		{
			writeCheckpoint();
			boinc_checkpoint_completed();
		}
		printf("Percent done: %lf\n", video.get(CV_CAP_PROP_POS_AVI_RATIO) * 100.0);
		#else
		printf("Percent done: %lf\n", video.get(CV_CAP_PROP_POS_AVI_RATIO) * 100.0);
		#endif
		//get next batchSize of preprocessed images
		vector<Mat> currentFrames(batchSize);
		vector<int> currentFramenums;
		vector<Mat> rawFrames(batchSize);
		int i;
		for(i = 0; i < batchSize; i++)
		{
			// if(!getNextFrame(video,frame,curFrame,jump))
			if(!getNextFrame(video,rawFrames[i],curFrame,jump))
			{
				cont = false;
				break;
			}
			// Mat tempMat;
			// currentFrames.push_back(tempMat);
			currentFramenums.push_back(curFrame);
			Mat* ptr = &(currentFrames[i]);
			Seamcarver* sptr = &(carvers[i]);
			thr[i] = thread([=] {preprocessFrame(rawFrames[i],ptr,cvSize,inputSize,frameWidth,frameHeight,scaleType,sptr);});
			// if(scaleType == DISTORT_DOWN) // straight scale to size. Distort if necessary
			// {
			// 	resize(frame,tempMat,cvSize);
			// }
			// else if(scaleType == RANDOM_CROP)
			// {
			// 	int si = disI(gen);
			// 	int sj = disJ(gen);
			// 	Mat temp(frame,Range(si,si+inputSize),Range(sj,sj+inputSize));
			// 	tempMat = temp;
			// }
			// else if(scaleType == SCALE_DOWN) // seamcarve to square. Scale to size
			// {
			// 	Mat temp;
			// 	if(numSeams > 0) //width > height. landscape
			// 	{
			// 		//vertical seams, fast
			// 		seamcarve_vf_cpu(numSeams,frame,temp);//bring us to square
			// 		resize(temp, tempMat,cvSize);
			// 	}
			// 	else // height > width. portrait
			// 	{
			// 		//horizontal seams, fast
			// 		seamcarve_hf_cpu(-numSeams, frame, temp);
			// 		resize(temp, tempMat,cvSize);
			// 	}
			// }
			// else if(scaleType == CARVE_DOWN_VTH) // seamcarve in both directions down to size. No normal scaling
			// 	seamcarve_both_vth_cpu(vseams, hseams, frame, tempMat);
			// else if(scaleType == CARVE_DOWN_HTV)
			// 	seamcarve_both_htv_cpu(hseams, vseams, frame, tempMat);
			// else if(scaleType == CARVE_DOWN_BOTH_RAW)
			// 	seamcarve_both_raw_cpu(vseams, hseams, frame, tempMat);
			// else if(scaleType == CARVE_DOWN_BOTH_SCALED)
			// 	seamcarve_both_scaled_cpu(vseams, hseams, frame, tempMat);
			// else
			// {
			// 	printf("Unknown scaleType %d\n", scaleType);
			// 	return 0;
			// }
			// currentFrames.push_back(tempMat);
			// currentFramenums.push_back(curFrame);

		}
		// printf("joining\n");
		for(int j = 0; j < i; j++)
			thr[j].join();
		// printf("done joining\n");
		if( i < batchSize )
			currentFrames.resize(i);

		// for(int i = 0; i < currentFramenums.size(); i++)
		// {
		// 	printf("%d ", currentFramenums[i]);
		// }
		// printf("\n");

		//run them through CNN and store results
		net.setData(currentFrames);
		net.run();
		net.getCalculatedClasses(calcClasses);
		net.getConfidences(curConfidences);

		for(int i = 0; i < currentFramenums.size(); i++)
			doneFrames.push_back(DoneFrame(currentFramenums[i],calcClasses[i], curConfidences[i]));

	}

	//now everything has been run through the cnn and we need to generate Observations
	Observations obs;

	//in theory, doneFrames should be in order by frame number
	for(int i = 0; i < doneFrames.size();)
	{
		int curClassIndex = doneFrames[i].classIndex;
		int startFrame = doneFrames[i].framenum;
		i++;
		while(i < doneFrames.size() && doneFrames[i].classIndex == curClassIndex)
			i++;
		int endFrame = doneFrames[i-1].framenum;

		int curStartTime = video_start_time + startFrame / fps;
		int curEndTime   = video_start_time + endFrame / fps;

		obs.addEvent(net.getNameForIndex(curClassIndex), curStartTime, curEndTime);
	}

	/*
	Fields in database
		id: int(11) autoincrement id for cnn_observations
		event_type: enum(...)
		cnn: cnn_id???
		start_time: time (hh:mm:ss)
		video_id: int(11)
		end_time: time (hh:mm:ss)
	*/

	/* 
	Format output file

	cnn:string //assimilator will turn into cnn_id
	video_name:string //assimilator will turn into video_id
	EVENT
	EVENT
	EVENT

	where EVENT is from Event::toString 
	*/

	//list of calculated events with no smoothing / signal processing / etc.
	ofstream outfile(getBoincFilename("results.txt"));
	outfile << cnn_config_id << endl;
	outfile << video_id << endl;
	outfile << obs.toString();
	outfile.close();

	//same format as checkpoint file
	ofstream rawout(getBoincFilename("raw_results.txt"));
	rawout << curFrame << endl;
	rawout << doneFrames.size() << endl;
	rawout << doneFrames[0].confidences.size() << endl;
	for(size_t i = 0; i < doneFrames.size(); i++)
	{
		rawout << doneFrames[i].framenum << " " << doneFrames[i].classIndex;
		for(int j = 0; j < doneFrames[i].confidences.size(); j++)
			rawout << " " << doneFrames[i].confidences[j];
		rawout << endl;
	}
	rawout.close();


	#ifdef _BOINC_APP_
	boinc_finish(0);
	#endif

	return 0;
}