@sjcrh/proteinpaint-shared 2.145.2 → 2.146.2

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.145.2",
3
+ "version": "2.146.2",
4
4
  "description": "ProteinPaint code that is shared between server and client-side workspaces",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,7 +1,8 @@
1
1
  // compute the percentile value from an array of values
2
+ // sorted parameter allows to skip sorting if array is pre-sorted
2
3
  // source: https://www.dummies.com/article/academics-the-arts/math/statistics/how-to-calculate-percentiles-in-statistics-169783/
3
- export default function computePercentile(values, percentile) {
4
- values.sort((a, b) => a - b)
4
+ export default function computePercentile(values, percentile, sorted = false) {
5
+ if (!sorted) values.sort((a, b) => a - b)
5
6
  const index = Math.abs((percentile / 100) * values.length - 1)
6
7
  const value = Number.isInteger(index) ? (values[index] + values[index + 1]) / 2 : values[Math.ceil(index)]
7
8
  return value
package/src/index.js CHANGED
@@ -3,7 +3,6 @@ export * from './bulk.js'
3
3
  export * from './clustering.js'
4
4
  export * from './common.js'
5
5
  export * from './compute.percentile.js'
6
- export * from './descriptive.stats.js'
7
6
  export * from './fetch-helpers.js'
8
7
  export * from './fileSize.js'
9
8
  export * from './filter.js'
@@ -1,107 +0,0 @@
1
- import { roundValueAuto } from './roundValue.js'
2
-
3
- /* This file generates summary statistics on any given array of numbers*/
4
-
5
- export function getDescriptiveStats(array) {
6
- //console.log("array:",array)
7
- let arr = array
8
- if (typeof array[0] == 'string') {
9
- // somehow the values can be string but not numbers
10
- // must cast to numbers to properly compute values
11
- arr = array.map(Number)
12
- }
13
- //compute total
14
- const sorted_arr = arr.sort((a, b) => a - b)
15
- const n = arr.length
16
-
17
- //compute median
18
- const median = computePercentile(sorted_arr, 50)
19
- //compute mean
20
- const mean = getMean(arr)
21
- // compute variance
22
- const variance = getVariance(sorted_arr)
23
- // compute standard deviation
24
- const stdDev = Math.sqrt(variance)
25
-
26
- //compute percentile ranges
27
- const p25 = computePercentile(sorted_arr, 25)
28
- const p75 = computePercentile(sorted_arr, 75)
29
-
30
- //compute IQR
31
- const IQR = p75 - p25
32
- const min = sorted_arr[0]
33
- const max = sorted_arr[sorted_arr.length - 1]
34
-
35
- // Calculate outlier boundaries
36
- const outlierMin = p25 - 1.5 * IQR //p25 is same as q1
37
- const outlierMax = p75 + 1.5 * IQR //p75 is same as q3
38
-
39
- return {
40
- total: n,
41
- min,
42
- max,
43
- p25,
44
- p75,
45
- median,
46
- mean,
47
- variance,
48
- stdDev,
49
- IQR,
50
- outlierMin,
51
- outlierMax
52
- }
53
- }
54
-
55
- export function summaryStats(array, showOutlierRange = false) {
56
- const stats = getDescriptiveStats(array)
57
- return summaryStatsFromStats(stats, showOutlierRange)
58
- }
59
-
60
- export function summaryStatsFromStats(stats, showOutlierRange = false) {
61
- const result = {
62
- values: [
63
- { id: 'total', label: 'Total', value: stats.total },
64
- { id: 'min', label: 'Minimum', value: roundValueAuto(stats.min, true) },
65
- { id: 'p25', label: '1st quartile', value: roundValueAuto(stats.p25, true) },
66
- { id: 'median', label: 'Median', value: roundValueAuto(stats.median, true) },
67
- { id: 'mean', label: 'Mean', value: roundValueAuto(stats.mean, true) },
68
- { id: 'p75', label: '3rd quartile', value: roundValueAuto(stats.p75, true) },
69
- { id: 'max', label: 'Maximum', value: roundValueAuto(stats.max, true) },
70
- { id: 'SD', label: 'Standard deviation', value: roundValueAuto(stats.stdDev, true) },
71
- { id: 'variance', label: 'Variance', value: roundValueAuto(stats.variance, true) },
72
- { id: 'IQR', label: 'Inter-quartile range', value: roundValueAuto(stats.IQR, true) }
73
- ]
74
- }
75
- if (showOutlierRange) {
76
- result.values.push(
77
- { id: 'outlierMin', label: 'Outlier min', value: roundValueAuto(stats.outlierMin, true) },
78
- { id: 'outlierMax', label: 'Outlier max', value: roundValueAuto(stats.outlierMax, true) }
79
- )
80
- }
81
- return result
82
- }
83
-
84
- function computePercentile(values, percentile) {
85
- const index = Math.abs((percentile / 100) * values.length - 1)
86
- const value = Number.isInteger(index) ? (values[index] + values[index + 1]) / 2 : values[Math.ceil(index)]
87
- return value
88
- }
89
-
90
- export function getMean(data) {
91
- return data.reduce((sum, value) => sum + value, 0) / data.length
92
- }
93
-
94
- export function getVariance(data) {
95
- const meanValue = getMean(data)
96
- const squaredDifferences = data.map(value => Math.pow(value - meanValue, 2))
97
- //Using n−1 compensates for the fact that we're basing variance on a sample mean,
98
- // which tends to underestimate true variability. The correction is especially important with small sample sizes,
99
- // where dividing by n would significantly distort the variance estimate.
100
- // For more details see https://en.wikipedia.org/wiki/Bessel%27s_correction
101
- return squaredDifferences.reduce((sum, value) => sum + value, 0) / (data.length - 1)
102
- }
103
-
104
- export function getStdDev(data) {
105
- const variance = getVariance(data)
106
- return Math.sqrt(variance)
107
- }