prepAndRun :: MatlabOpts -> FilePath -> FilePath -> IO ExitCode
prepAndRun matlabOpts outputDirectory' inputDataFile' = do
  let ?dbnSizes = dbnSizes matlabOpts
      ?numEpochs = numEpochs matlabOpts

  outputDirectory <- canonicalizePath outputDirectory'
  inputDataFile <- canonicalizePath inputDataFile'

  createDirectoryIfMissing True outputDirectory

  writeFile (outputDirectory </> "nnsave_to_file_full.m") nnsave_to_file_full
  writeFile (outputDirectory </> "nnsave_to_file.m") nnsave_to_file
  writeFile (outputDirectory </> "run_main.m") (run_main inputDataFile)
  writeFile (outputDirectory </> "run_trainer.m") (run_trainer (outputDirectory </> "dbn.txt"))
  writeFile (outputDirectory </> "run_trainer_ll.m") (run_trainer_ll (outputDirectory </> "dbn-ll.txt"))

  let logFile = outputDirectory </> "run_log.txt"
  tailHandle <- runProcess "tail" (words "--retry --follow=name" ++ [logFile]) (Just outputDirectory) Nothing Nothing Nothing Nothing

  exitCode <- withFile logFile WriteMode (\ log'han -> do
                         procHandle <- case implementation matlabOpts of
                           Octave -> runProcess "octave" (words "run_main.m") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing 
                           Matlab -> runProcess "matlab" (words "-nosplash -nodisplay -r run_main") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing
                         waitForProcess procHandle)
  print =<< readFile logFile
  terminateProcess tailHandle
  return exitCode

-- zapisuje wszystkie warstwy, łącznie z ostatnią
nn
prepAndRun :: MatlabOpts -> FilePath -> FilePath -> IO ExitCode
prepAndRun matlabOpts outputDirectory' inputDataFile' = do
  let ?dbnSizes = dbnSizes matlabOpts
      ?numEpochs = numEpochs matlabOpts

  outputDirectory <- canonicalizePath outputDirectory'
  inputDataFile <- canonicalizePath inputDataFile'

  createDirectoryIfMissing True outputDirectory

  writeFile (outputDirectory </> "nnsave_to_file_full.m") nnsave_to_file_full
  writeFile (outputDirectory </> "nnsave_to_file.m") nnsave_to_file
  writeFile (outputDirectory </> "run_main.m") (run_main inputDataFile)
  writeFile (outputDirectory </> "run_trainer.m") (run_trainer (outputDirectory </> "dbn.txt"))
  writeFile (outputDirectory </> "run_trainer_ll.m") (run_trainer_ll (outputDirectory </> "dbn-ll.txt"))

  let logFile = outputDirectory </> "run_log.txt"
  tailHandle <- runProcess "tail" (words "--retry --follow=name" ++ [logFile]) (Just outputDirectory) Nothing Nothing Nothing Nothing

  exitCode <- withFile logFile WriteMode (\ log'han -> do
                         procHandle <- case implementation matlabOpts of
                           Octave -> runProcess "octave" (words "run_main.m") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing 
                           Matlab -> runProcess "matlab" (words "-nosplash -nodisplay -r run_main") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing
                         waitForProcess procHandle)
  print =<< readFile logFile
  terminateProcess tailHandle
  return exitCode

-- zapisuje wszystkie warstwy, łącznie z ostatnią
prepAndRun matlabOpts outputDirectory' inputDataFile' = do
  let ?dbnSizes = dbnSizes matlabOpts
      ?numEpochs = numEpochs matlabOpts

  outputDirectory <- canonicalizePath outputDirectory'
  inputDataFile <- canonicalizePath inputDataFile'

  createDirectoryIfMissing True outputDirectory

  writeFile (outputDirectory </> "nnsave_to_file_full.m") nnsave_to_file_full
  writeFile (outputDirectory </> "nnsave_to_file.m") nnsave_to_file
  writeFile (outputDirectory </> "run_main.m") (run_main inputDataFile)
  writeFile (outputDirectory </> "run_trainer.m") (run_trainer (outputDirectory </> "dbn.txt"))
  writeFile (outputDirectory </> "run_trainer_ll.m") (run_trainer_ll (outputDirectory </> "dbn-ll.txt"))

  let logFile = outputDirectory </> "run_log.txt"
  tailHandle <- runProcess "tail" (words "--retry --follow=name" ++ [logFile]) (Just outputDirectory) Nothing Nothing Nothing Nothing

  exitCode <- withFile logFile WriteMode (\ log'han -> do
                         procHandle <- case implementation matlabOpts of
                           Octave -> runProcess "octave" (words "run_main.m") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing 
                           Matlab -> runProcess "matlab" (words "-nosplash -nodisplay -r run_main") (Just outputDirectory) Nothing Nothing (Just log'han) Nothing
                         waitForProcess procHandle)
  print =<< readFile logFile
  terminateProcess tailHandle
  return exitCode

-- zapisuje wszystkie warstwy, łącznie z ostatnią