Exemplo n.º 1
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;
  
}
Exemplo n.º 2
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;
}