Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/methods/analyzing/correlation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,11 @@ export default function correlation(
if (!hasKey(data, keyCategory)) {
throw new Error("No keyCategory " + keyCategory)
}
if (
!checkTypeOfKey(
data,
keyCategory,
"string",
1,
nbTestedValues,
verbose
)
) {
throw new Error(`Values in ${keyCategory} must be strings.`)
}
} else {
throw new Error("keyCategory must be a string")
}

const correlations: { [key: string]: string }[] = []
const correlations: { [key: string]: string | number }[] = []

if (
keyX === undefined &&
Expand Down Expand Up @@ -116,8 +104,13 @@ export default function correlation(
.filter((d) => d[keyCategory] === category)
.map((d) => d[corr.keyY])

if (typeof category !== "string") {
throw new Error(`Values of ${keyCategory} must be strings`)
if (
typeof category !== "string" &&
typeof category !== "number"
) {
throw new Error(
`Values of ${keyCategory} must be strings or numbers.`
)
}
corr[keyCategory] = category

Expand All @@ -139,7 +132,7 @@ function computeCorr(
x: SimpleDataValue[],
y: SimpleDataValue[],
corr: {
[key: string]: string
[key: string]: string | number
},
correlationData: SimpleDataItem[],
nbDigits: number
Expand Down
21 changes: 7 additions & 14 deletions src/methods/analyzing/regression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ export default function regression(
if (!hasKey(data, keyCategory)) {
throw new Error("No keyCategory " + keyCategory)
}
if (
!checkTypeOfKey(
data,
keyCategory,
"string",
1,
nbTestedValues,
verbose
)
) {
throw new Error(`Values in ${keyCategory} must be strings.`)
}
} else {
throw new Error("keyCategory must be a string")
}
Expand Down Expand Up @@ -127,8 +115,13 @@ export default function regression(

for (const category of categories) {
for (const lr of linearRegressions) {
if (typeof category !== "string") {
throw new Error(`Values of ${keyCategory} must be strings`)
if (
typeof category !== "string" &&
typeof category !== "number"
) {
throw new Error(
`Values of ${keyCategory} must be strings or numbers.`
)
}
lr[keyCategory] = category
computeRegr(
Expand Down
95 changes: 40 additions & 55 deletions src/methods/analyzing/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ export default function summarize(
nbDigits?: number
): SimpleDataItem[] {
if (keyValue === undefined) {
keyValue = Object.keys(data[0])
verbose && log(`No keyValue provided. Will summarize over all keys.`)
keyValue = Object.keys(data[0]).filter((d) => {
if (checkTypeOfKey(data, d, "number", 1, nbTestedValues)) {
return true
} else {
verbose &&
log(
`At least one value in ${d} is not a number. Excluded from summaries.`
)
return false
}
})
}

// Let's deal with the keyCategory first
Expand Down Expand Up @@ -94,14 +105,29 @@ export default function summarize(
const summariesResults = []

for (const value of keyValues) {
const isNumber = checkTypeOfKey(
data,
value,
"number",
1,
nbTestedValues,
verbose
)
if (
summaries.includes("sum") ||
summaries.includes("mean") ||
summaries.includes("median") ||
summaries.includes("deviation") ||
summaries.includes("weightedMean")
) {
if (
!checkTypeOfKey(
data,
value,
"number",
1,
nbTestedValues,
verbose
)
) {
throw new Error(
`At least one value in ${value} is not a number. To summarize with sum, mean, median, deviation, and weightedMean, all values must be a number.`
)
}
}

for (const summary of summaries) {
let func: (v: SimpleDataItem[]) => number | undefined
if (summary === "count") {
Expand All @@ -111,55 +137,14 @@ export default function summarize(
} else if (summary === "max") {
func = (v) => max(v, (d) => d[value] as number | undefined)
} else if (summary === "sum") {
if (isNumber) {
func = (v) => sum(v, (d) => d[value] as number | undefined)
} else {
verbose &&
log(
"The majority of " +
value +
" values are not numbers. Returning NaN for sum."
)
func = () => NaN
}
func = (v) => sum(v, (d) => d[value] as number | undefined)
} else if (summary === "mean") {
if (isNumber) {
func = (v) => mean(v, (d) => d[value] as number | undefined)
} else {
verbose &&
log(
"The majority of " +
value +
" values are not numbers. Returning NaN for mean."
)
func = () => NaN
}
func = (v) => mean(v, (d) => d[value] as number | undefined)
} else if (summary === "median") {
if (isNumber) {
func = (v) =>
median(v, (d) => d[value] as number | undefined)
} else {
verbose &&
log(
"The majority of " +
value +
" values are not numbers. Returning NaN for median."
)
func = () => NaN
}
func = (v) => median(v, (d) => d[value] as number | undefined)
} else if (summary === "deviation") {
if (isNumber) {
func = (v) =>
deviation(v, (d) => d[value] as number | undefined)
} else {
verbose &&
log(
"The majority of " +
value +
" values are not numbers. Returning NaN for deviation."
)
func = () => NaN
}
func = (v) =>
deviation(v, (d) => d[value] as number | undefined)
} else if (summary === "weightedMean") {
if (weight === undefined) {
throw new Error("Missing argument weight")
Expand Down
2 changes: 1 addition & 1 deletion test/integration/SimpleData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async function main() {
})
.summarize({ overwrite: false })
.summarize({
keyValue: simpleDataMerged.getKeys(),
keyValue: ["salary", "bonus"],
keyCategory: "job",
overwrite: false,
})
Expand Down
28 changes: 14 additions & 14 deletions test/unit/methods/analyzing/summarize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import summarize from "../../../../src/methods/analyzing/summarize.js"
describe("summarize", function () {
it("should summarize without keyCategory", function () {
const data = [
{ key1: "Rubarbe", key2: 2, key3: new Date("2022-02-14") },
{ key1: "Fraise", key2: 22, key3: new Date("2014-02-14") },
{ key1: 1, key2: 2, key3: 55 },
{ key1: 2, key2: 22, key3: 99 },
]
const summarizedData = summarize(
data,
Expand All @@ -21,12 +21,12 @@ describe("summarize", function () {
{
value: "key1",
count: 2,
min: NaN,
max: NaN,
sum: NaN,
mean: NaN,
median: NaN,
deviation: NaN,
min: 1,
max: 2,
sum: 3,
mean: 1.5,
median: 1.5,
deviation: 0.7,
},
{
value: "key2",
Expand All @@ -41,12 +41,12 @@ describe("summarize", function () {
{
value: "key3",
count: 2,
min: NaN,
max: NaN,
sum: NaN,
mean: NaN,
median: NaN,
deviation: NaN,
min: 55,
max: 99,
sum: 154,
mean: 77,
median: 77,
deviation: 31.1,
},
])
})
Expand Down