@sjcrh/proteinpaint-shared 2.145.1-0 → 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.1-0",
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,95 +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 fences
36
- const lowerFence = p25 - 1.5 * IQR //p25 is same as q1
37
- const upperFence = p75 + 1.5 * IQR //p75 is same as q3
38
-
39
- // Identify outliers
40
- const outlierRange = { min: lowerFence, max: upperFence }
41
-
42
- return {
43
- total: n,
44
- min,
45
- max,
46
- p25,
47
- p75,
48
- median,
49
- mean,
50
- variance,
51
- stdDev,
52
- IQR,
53
- outlierRange
54
- }
55
- }
56
-
57
- export function summaryStats(array) {
58
- const stats = getDescriptiveStats(array)
59
-
60
- return {
61
- values: [
62
- { id: 'total', label: 'Total', value: stats.total },
63
- { id: 'min', label: 'Minimum', value: roundValueAuto(stats.min, true) },
64
- { id: 'p25', label: '1st quartile', value: roundValueAuto(stats.p25, true) },
65
- { id: 'median', label: 'Median', value: roundValueAuto(stats.median, true) },
66
- { id: 'mean', label: 'Mean', value: roundValueAuto(stats.mean, true) },
67
- { id: 'p75', label: '3rd quartile', value: roundValueAuto(stats.p75, true) },
68
- { id: 'max', label: 'Maximum', value: roundValueAuto(stats.max, true) },
69
- { id: 'SD', label: 'Standard deviation', value: roundValueAuto(stats.stdDev, true) },
70
- { id: 'variance', label: 'Variance', value: roundValueAuto(stats.variance, true) },
71
- { id: 'IQR', label: 'Inter-quartile range', value: roundValueAuto(stats.IQR, true) }
72
- ]
73
- }
74
- }
75
-
76
- function computePercentile(values, percentile) {
77
- const index = Math.abs((percentile / 100) * values.length - 1)
78
- const value = Number.isInteger(index) ? (values[index] + values[index + 1]) / 2 : values[Math.ceil(index)]
79
- return value
80
- }
81
-
82
- export function getMean(data) {
83
- return data.reduce((sum, value) => sum + value, 0) / data.length
84
- }
85
-
86
- export function getVariance(data) {
87
- const meanValue = getMean(data)
88
- const squaredDifferences = data.map(value => Math.pow(value - meanValue, 2))
89
- return squaredDifferences.reduce((sum, value) => sum + value, 0) / data.length
90
- }
91
-
92
- export function getStdDev(data) {
93
- const variance = getVariance(data)
94
- return Math.sqrt(variance)
95
- }