<commit_before>{-# LANGUAGE OverloadedStrings, QuasiQuotes, ImplicitParams #-}

module Matlab where

import Data.String.Interpolation
import Data.String
import Data.Monoid
import Data.Default
import System.FilePath
import System.Directory
import System.Process
import System.Exit

data MatlabImp = Matlab | Octave deriving (Eq, Ord, Read, Show)
data MatlabOpts = MatlabOpts { dbnSizes :: [Int], numEpochs :: Int, implementation :: MatlabImp } deriving (Eq, Ord, Read, Show)

instance Default MatlabOpts where
    def = MatlabOpts { dbnSizes = [100], numEpochs = 5, implementation = Octave }

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"))

  procHandle <- case implementation matlabOpts of
                  Octave -> runProcess "octave" (words "run_main.m") (Just outputDirectory) Nothing Nothing Nothing Nothing 
                  Matlab -> runProcess "matlab" (words "-nosplash -nodisplay -r run_main") (Just outputDirectory) Nothing Nothing Nothing Nothing 
  exitCode <- waitForProcess procHandle
  return exitCode

-- zapisuje wszystkie warstwy, łącznie z ostatnią<commit_msg>write stdout from Matlab/Octave run into run_log.txt<commit_after>{-# LANGUAGE OverloadedStrings, QuasiQuotes, ImplicitParams #-}

module Matlab where

import Data.String.Interpolation
import Data.String
import Data.Monoid
import Data.Default
import System.FilePath
import System.Directory
import System.Process
import System.Exit
import System.IO

data MatlabImp = Matlab | Octave deriving (Eq, Ord, Read, Show)
data MatlabOpts = MatlabOpts { dbnSizes :: [Int], numEpochs :: Int, implementation :: MatlabImp } deriving (Eq, Ord, Read, Show)

instance Default MatlabOpts where
    def = MatlabOpts { dbnSizes = [100], numEpochs = 5, implementation = Octave }

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ą
{'fraction_non_alphanumeric': 0.09486166007905138, 'fraction_numerical': 0.001976284584980237, 'mean_word_length': 4.391478029294274, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2677511', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1228, 'n_words': 340}