-
Notifications
You must be signed in to change notification settings - Fork 1
/
expeStats.js
113 lines (90 loc) · 3.46 KB
/
expeStats.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
// import { experiments } from './experimentConfig'
const config = require('./experimentConfig')
const mongoose = require('mongoose')
const configApp = require('./config')
const mongoDatabaseURI = configApp.mongoDatabaseURI
const fs = require('fs-extra')
const winston = require('winston')
const execSync = require('child_process').execSync
// const connectDb = async () => {
// await mongoose.connect(mongoDatabaseURI, { useNewUrlParser: true, useFindAndModify: false })
// mongoose.connection.on('error', (err) => console.log(err))
// }
// get whitelist scene for MatchExtractsWithReference experiment
const scenes = config.experiments.MatchExtractsWithReference.availableScenes.whitelist
// File logger configuration
const fileLogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'logs/expeStats.log' }),
new winston.transports.File({ filename: 'logs/expeStats.error.log', level: 'error' }),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
format: winston.format.json()
})
],
exitOnError: false
})
const setup = async (logToFile = false) => {
// await connectDb()
if (logToFile) fileLogger.info({ log: 'Start extraction of data from mongo for `MatchExtractsExperiments`.', date: new Date() })
execSync('python utils/extract_experiment.py', { encoding: 'utf-8' })
if (logToFile) fileLogger.info({ log: 'Mongo extraction done', date: new Date() })
execSync('python utils/extract_stats_freq_and_min_all.py --file results/experiments_results.json --output results/match_extracts_stats.csv', { encoding: 'utf-8' })
if (logToFile) fileLogger.info({ log: 'Stats computation done, need to create probability for each scene', date: new Date() })
// read extracted stats in order to compute probabilities
let statsPath = 'results/match_extracts_stats.csv'
let buffer = fs.readFileSync(statsPath)
let lines = buffer.toString().split('\n')
let stats = {}
let nUsers = 0
for (let l of lines) {
if (l.length > 0) {
// extract data from csv file
let data = l.split(';')
// data[0] contains scene name
// data[1] contains number of users who do this scene
let u = Number(data[1])
stats[String(data[0])] = u
nUsers += u
}
}
// start computing probabilities
let probabilities = {}
let probsArr = []
let nUnknownScenes = 0
// based on white list
for (let s of scenes) {
if (s in stats) {
probabilities[s] = stats[s] / nUsers
probsArr.push(probabilities[s])
}
else {
nUnknownScenes += 1
}
}
// normalize probabilities
let currentMax = Math.max(...probsArr)
for (let s of scenes) {
// if new scene
if (!(s in stats)) {
// multiply prob criteria based on number of unknown scene
// => increase chance for user to pass this scene
probabilities[s] = (1 + (1 - (nUnknownScenes / scenes.length))) * currentMax
probsArr.push(probabilities[s])
}
}
// get sum of current probs
let sum = probsArr.reduce((a, b) => a + b, 0)
for (let s of scenes) {
probabilities[s] /= sum
}
if (logToFile) fileLogger.info({ log: 'New probabilities extracted:' + JSON.stringify(probabilities, null, 3), date: new Date() })
fs.writeFile('results/match_extracts_probs.json', JSON.stringify(probabilities, null, 3))
}
// Execute setup command
setup()
module.exports = { setup, expeStatsServiceLogger: fileLogger }