File: logFunOpt.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (85 lines) | stat: -rw-r--r-- 3,606 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# logging function for tuning and feature selection
# has 2 stages:
# 1: After point to eval is selected
# 2: after evaluation

logFunDefault = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x.string, y, remove.nas, stage, prev.stage, prefixes) {
  if (stage == 1L) {
    start.time = Sys.time()
    messagef("[%s] %i: %s", prefixes[stage], dob, x.string)
    return(list(start.time = start.time))
  } else if (stage == 2L) {
    end.time = Sys.time()
    diff.time = difftime(time1 = end.time, time2 = prev.stage$start.time, units = "mins")
    messagef("[%s] %i: %s; time: %.1f min",
      prefixes[stage], dob, perfsToString(y), diff.time[[1]])
    return(NULL)
  }
}

logFunMemory = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x.string, y, remove.nas, stage, prev.stage, prefixes) {
  if (stage == 1L) {
    gc()
    gc()
    gc()
    start.time = Sys.time()
    messagef("[%s] %i: %s", prefixes[stage], dob, x.string)
    return(list(start.time = start.time))
  } else if (stage == 2L) {
    # get cur mem and max mem and sum ncells and vcells.
    # we had a diff here with gc(reset=TRUE). but this is bad the user cant avoid this, it might even be
    # not allowed on CRAN
    mem = colSums(gc()[, c(2L, 6L)])
    end.time = Sys.time()
    diff.time = difftime(time1 = end.time, time2 = prev.stage$start.time, units = "mins")
    messagef("[%s] %i: %s; time: %.1f min; memory: %.0fMb use, %.0fMb max",
      prefixes[stage], dob, perfsToString(y), diff.time[[1]], mem[1L], mem[2L])
    return(NULL)
  }
}

logFunTune = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x, y, remove.nas, stage, prev.stage) {
  x.string = paramValueToString(par.set, x, show.missing.values = !remove.nas)
  # shorten tuning logging a bit. we remove the sel.learner prefix from params
  if (inherits(learner, "ModelMultiplexer")) {
    x.string = stri_replace_all(x.string, "", regex = stri_paste(x$selected.learner, "\\."))
  }

  logFunDefault(learner, task, resampling, measures, par.set, control, opt.path, dob,
    x.string, y, remove.nas, stage, prev.stage, prefixes = c("Tune-x", "Tune-y")
  )
}

logFunTuneMemory = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x, y, remove.nas, stage, prev.stage) {
  x.string = paramValueToString(par.set, x, show.missing.values = !remove.nas)
  # shorten tuning logging a bit. we remove the sel.learner prefix from params
  if (inherits(learner, "ModelMultiplexer")) {
    x.string = stri_replace_all(x.string, "", regex = stri_paste(x$selected.learner, "\\."))
  }

  logFunMemory(learner, task, resampling, measures, par.set, control, opt.path, dob,
    x.string, y, remove.nas, stage, prev.stage, prefixes = c("Tune-x", "Tune-y")
  )
}

logFunFeatSel = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x, y, remove.nas, stage, prev.stage) {
  x.string = sprintf("%s (%i bits)", clipString(collapse(x, ""), 80L), sum(x))

  logFunDefault(learner, task, resampling, measures, par.set, control, opt.path, dob,
    x.string, y, remove.nas, stage, prev.stage, prefixes = c("FeatSel-x", "FeatSel-y")
  )
}

logFunFeatSelMemory = function(learner, task, resampling, measures, par.set, control, opt.path, dob,
  x, y, remove.nas, stage, prev.stage) {
  x.string = sprintf("%s (%i bits)", clipString(collapse(x, ""), 80L), sum(x))

  logFunMemory(learner, task, resampling, measures, par.set, control, opt.path, dob,
    x.string, y, remove.nas, stage, prev.stage, prefixes = c("FeatSel-x", "FeatSel-y")
  )
}