Commit d3daf57d authored by Knut Behrends's avatar Knut Behrends
Browse files

perform nearest-neighbor-search to match hexvals -> colornames

parent 17ee1472
#!/usr/bin/env node
const data = require("../data/macrotstrat-lithclasses-all.json")
const tinycolor = require("tinycolor2")
const KNN = require("ml-knn")
let train_dataset = Object.entries(tinycolor.names)
.map((c, k) => ({
color: c[0],
hex: c[1],
rgb: Object.values(tinycolor(c[1]).toRgb()).slice(0, 3),
}))
.map((c) => [c.rgb].flat())
let train_labels = Object.entries(tinycolor.names)
.map((c, k) => ({
color: c[0],
hex: c[1],
rgb: Object.values(tinycolor(c[1]).toRgb()).slice(0, 3),
}))
.map((c) => `#${c.hex} ${c.color}`)
knn = new KNN(train_dataset, train_labels, { k: 2 })
let test_dataset = data.success.data.map((d) => {
let v = Object.values(tinycolor(d.color).toRgb()).slice(0, 3)
return v
})
let closest_colors = knn.predict(test_dataset)
// console.log(
// closest_colors.map((v, k) => ({
// original: data[k].color,
// predicted: v,
// }))
// )
data.success.data.forEach((v, k) => {
v.color_predicted = closest_colors[k]
v.color_hex = closest_colors[k].split(" ")[0]
v.color_name = closest_colors[k].split(" ")[1]
})
console.log(JSON.stringify(data))
//console.log(closest_colors)
......@@ -43,6 +43,19 @@
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
},
"ml-distance-euclidean": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ml-distance-euclidean/-/ml-distance-euclidean-2.0.0.tgz",
"integrity": "sha512-yC9/2o8QF0A3m/0IXqCTXCzz2pNEzvmcE/9HFKOZGnTjatvBbsn4lWYJkxENkA4Ug2fnYl7PXQxnPi21sgMy/Q=="
},
"ml-knn": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/ml-knn/-/ml-knn-3.0.0.tgz",
"integrity": "sha512-w7Jig4vW09OYMurlIRRJj8yL2O/835QJpxup+yW7QVYXSjixmanPtPQL+weiTYbaB/wWX8JilB+Wllk4mxvP5w==",
"requires": {
"ml-distance-euclidean": "^2.0.0"
}
},
"moment": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz",
......@@ -52,6 +65,11 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"tinycolor2": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/tinycolor2/-/tinycolor2-1.4.1.tgz",
"integrity": "sha1-9PrTM0R7wLB9TcjpIJ2POaisd+g="
}
}
}
......@@ -13,7 +13,9 @@
"faker": "^4.1",
"getopts": "^2.2.5",
"lodash": "^4.17.19",
"moment": "^2.27"
"ml-knn": "^3.0.0",
"moment": "^2.27",
"tinycolor2": "^1.4.1"
},
"devDependencies": {}
}
......@@ -13,8 +13,11 @@ const axios = require("axios")
const axutil = require("./axios-util.js")
const querystring = require("querystring")
const getopts = require("getopts")
const tinycolor = require("tinycolor2")
const Util = require("./classes/fake-util.js")
const macrostrat_data = require("../data/macrotstrat-lithclasses-all.json")
//["lith_id","name","type","group","class","color","fill","t_units"]
const litho_data = require("../data/macrotstrat-lithclasses-extended.json")
.success.data
const LithologyGeology = require("./classes/fake-lithology.js")
const util = new Util()
......@@ -27,8 +30,7 @@ const options = getopts(process.argv.slice(2), {
expedition: 10, //SAMP
},
})
//["lith_id","name","type","group","class","color","fill","t_units"]
const litho_data = macrostrat_data.success.data
//let core_url = "/api/v1/form?name=core&per-page=1&sort=-id"
//const core_info = options.infile ? JSON.parse(options.infile) : {}
const ax = axios.create(config.endpoint)
......@@ -234,12 +236,21 @@ Promise.all(queries_map.values())
let sec_len = sp.section_length
? sp.section_length
: util.round(util.frac_above(0.5), 2)
let color = lithounits_per_split[s]?.color || "red"
color = tinycolor(color).toName()
? `${color} (${tinycolor(color).toName()})`
: color
let color_name =
lithounits_per_split[s]?.color_name || "red"
let color_hex =
lithounits_per_split[s]?.color_hex || "#FF0000"
let rock_name = lithounits_per_split[s]?.name || "weird"
let fake_lithology = new LithologyGeology(
Object.assign({}, geology_lithology_columns, {
section_split_id: sp.id,
top_depth: sp.top_depth,
bottom_depth: sp.bottom_depth,
color: lithounits_per_split[s]?.color || "red",
color: color_name,
litho_unit: s,
rock_class:
lithounits_per_split[s]?.class || "artificial",
......@@ -250,7 +261,8 @@ Promise.all(queries_map.values())
util.frac_below(0.1) * sec_len * 100 + 1,
0
),
description: `Spl${sp.id
composition: rock_name,
description: `${color} (${color_hex}) Spl${sp.id
.toString()
.padStart(4, "0")}, U${s
.toString()
......@@ -259,7 +271,7 @@ Promise.all(queries_map.values())
}, secLen ${util.round(sp.section_length, 1)}m`,
}),
sp, // current
sp // previous
sp // previous - not used
).fake()
fake_lithology_data.push(Object.assign({}, fake_lithology))
i++
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment