@sjcrh/proteinpaint-shared 2.111.0 → 2.112.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjcrh/proteinpaint-shared",
3
- "version": "2.111.0",
3
+ "version": "2.112.0",
4
4
  "description": "ProteinPaint code that is shared between server and client-side workspaces",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -2,7 +2,7 @@ import { roundValueAuto } from './roundValue.js'
2
2
 
3
3
  /* This file generates summary statistics on any given array of numbers*/
4
4
 
5
- export default function summaryStats(array) {
5
+ export function summaryStats(array) {
6
6
  //console.log("array:",array)
7
7
  let arr = array
8
8
  if (typeof array[0] == 'string') {
@@ -14,18 +14,12 @@ export default function summaryStats(array) {
14
14
  const sorted_arr = arr.sort((a, b) => a - b)
15
15
  const n = arr.length
16
16
 
17
- //compute mean
18
- function mean(arr) {
19
- if (arr.length === 0) throw new Error('No data provided')
20
- return arr.reduce((a, b) => a + b) / n
21
- }
22
-
23
17
  //compute median
24
18
  const median = computePercentile(sorted_arr, 50)
25
- const mean_arr = mean(sorted_arr)
26
- const squareDiffs = arr.map(x => (x - mean_arr) ** 2).reduce((a, b) => a + b, 0)
19
+ //compute mean
20
+ const mean = getMean(arr)
27
21
  // compute variance
28
- const variance = squareDiffs / (n - 1)
22
+ const variance = getVariance(sorted_arr)
29
23
  // compute standard deviation
30
24
  const stdDev = Math.sqrt(variance)
31
25
 
@@ -45,7 +39,7 @@ export default function summaryStats(array) {
45
39
  { id: 'min', label: 'Minimum', value: roundValueAuto(min, true) },
46
40
  { id: 'p25', label: '1st quartile', value: roundValueAuto(p25, true) },
47
41
  { id: 'median', label: 'Median', value: roundValueAuto(median, true) },
48
- { id: 'mean', label: 'Mean', value: roundValueAuto(mean(arr), true) },
42
+ { id: 'mean', label: 'Mean', value: roundValueAuto(mean, true) },
49
43
  { id: 'p75', label: '3rd quartile', value: roundValueAuto(p75, true) },
50
44
  { id: 'max', label: 'Maximum', value: roundValueAuto(max, true) },
51
45
  { id: 'SD', label: 'Standard deviation', value: roundValueAuto(stdDev, true) },
@@ -60,3 +54,18 @@ function computePercentile(values, percentile) {
60
54
  const value = Number.isInteger(index) ? (values[index] + values[index + 1]) / 2 : values[Math.ceil(index)]
61
55
  return value
62
56
  }
57
+
58
+ export function getMean(data) {
59
+ return data.reduce((sum, value) => sum + value, 0) / data.length
60
+ }
61
+
62
+ export function getVariance(data) {
63
+ const meanValue = getMean(data)
64
+ const squaredDifferences = data.map(value => Math.pow(value - meanValue, 2))
65
+ return squaredDifferences.reduce((sum, value) => sum + value, 0) / data.length
66
+ }
67
+
68
+ export function getStdDev(data) {
69
+ const variance = getVariance(data)
70
+ return Math.sqrt(variance)
71
+ }