/
dataset.ts
682 lines (605 loc) · 23 KB
/
dataset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/**
* @packageDocumentation
* @module main
*/
import { Arrow, CustomShape, Cylinder, Ellipsoid, Sphere } from './structure/shapes';
import { ShapeParameters } from './structure/shapes';
/** A dataset containing all the data to be displayed. */
export interface Dataset {
/** metadata for this dataset */
meta: Metadata;
/**
* List of structures in the dataset.
*
* The structures can either follow the `Structure` interface, in which
* case they will be loaded as-defined; or contain any kind of data as a
* {@link UserStructure}, in which case the {@link ViewersGrid.loadStructure}
* callback should be set to translate from whatever is stored to a
* {@link Structure}.
*/
structures: Structure[] | UserStructure[];
/**
* Dictionary containing shape information about the dataset.
* Each shape is associated with a name that will also be used in the GUI,
* and the definition of the type and parameters of each shape is given by
* in the {@link ShapeParameters} dictionary.
*/
shapes?: { [name: string]: ShapeParameters };
/**
* List of properties for the structures (`target == "structure"`), or
* atom-centered environments in the structures (`target == "atom"`).
*
* For structure properties, the `values` array of the property should have
* the same size as the structure list in `Dataset.structures`.
*
* For atomic properties, the `values` array of the property should have
* the same size as the environments list in `Dataset.environments`.
*/
properties: { [name: string]: Property };
/**
* List of atom-centered environments in the dataset.
*
* Currently, the code assumes that every atom is associated with an
* environment. This may change in the future.
*/
environments?: Environment[];
/** Settings for visualization of this dataset */
settings?: Partial<Settings>;
/** Parameters of multidimensional properties */
parameters?: { [name: string]: Parameter };
}
/**
* Type definition for settings that can be saved with a dataset. THey should be
* a simple object with string keys, scalar values, array values or nested
* Settings objects.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Settings
extends Record<
string,
string | string[] | number | number[] | boolean | boolean[] | Settings | Settings[]
> {}
/** Various metadata associated with a dataset */
export interface Metadata {
/** dataset name */
name: string;
/** authors of the dataset */
authors?: string[];
/** academic references related to this dataset */
references?: string[];
/** description of the dataset */
description?: string;
}
/** A single atomic structure */
export interface Structure {
/** Number of atoms in the structure */
size: number;
/** Names of all atoms in the structure */
names: string[];
/**
* x position (cartesian coordinate) of all atoms in the structure
*
* This array should have the same size as {@link Structure.names}, and contain
* values expressed in Angströms.
*/
x: number[];
/**
* y position (cartesian coordinate) of all atoms in the structure
*
* This array should have the same size as {@link Structure.names}, and contain
* values expressed in Angströms.
*/
y: number[];
/**
* z position (cartesian coordinate) of all atoms in the structure
*
* This array should have the same size as {@link Structure.names}, and contain
* values expressed in Angströms.
*/
z: number[];
/**
* Unit cell of the system, given as `[ax ay az bx by bz cx cy cz]`, where
* **a**, **b**, and **c** are the unit cell vectors. All values should be
* expressed in Angströms.
*/
cell?: number[];
/**
* possible shapes to display, multiple groups of shapes with different
* names are allowed
*/
shapes?: {
/**
* dictionary containing shape data
*/
[name: string]: ShapeParameters;
};
}
/**
* User-defined data to allow dynamic loading of the structures.
*
* The main use-case of this is making the initial loading time of chemiscope
* faster by loading structure on-demand, from files, a database or even a
* javascript program.
*/
export interface UserStructure {
/** Number of atoms in the structure */
size: number;
/**
* User-defined data which can be turned into a {@link Structure}.
*
* {@link ViewersGrid.loadStructure} must be set to be able to load this
* data.
*/
data: unknown;
}
/** Possible types of properties: full structure property, or atomic property */
export type Target = 'structure' | 'atom';
/**
* A single property in a dataset.
*
* Properties can be physical properties (energy, number of atoms, density,
* *etc.*); values associated with the structure (such as SOAP vectors),
* projected onto a lower dimensionality sub-space (through PCA or any other
* algorithm); or any other value associated with every structure/environment in
* the dataset.
*/
export interface Property {
/** is this property associated with a full structure or a single atom? */
target: Target;
/**
* values of the property
*
* string values should represent classification results (category "A", "B"
* or "C"); and numeric values should be use for everything else.
*
* 2D properties are also supported, with the goal of displaying one curve
* for each atom/structure. In this case, the first dimension of the 2D
* array corresponds to number of structures/atoms and the second dimension
* corresponds to the values for each structure/atom.
*/
values: string[] | number[] | number[][];
/** user-facing description of the property */
description?: string;
/** unit of the property values */
units?: string;
/** parameter name associated to the multidimensional property */
parameters?: string[];
}
/**
* An atom-centered environment.
*
* Currently, only spherical (i.e. cutoff-based) environments are supported
*/
export interface Environment {
/** Index of the related structure in {@link Dataset.structures} */
structure: number;
/** Index of the central atom in the structure, 0-based */
center: number;
/** Spherical cutoff radius, expressed in Angströms */
cutoff: number;
}
/**
* Parameters are used to describe multidimensional properties.
*
* The first dimension of a multidimensional property always corresponds to
* structure/atoms in the dataset, and additional dimensions should be described
* with a `Parameter`.
*/
export interface Parameter {
/** name of the parameter */
name?: string;
/** units of the elements in the property array */
units?: string;
/** values of the parameter */
values: number[];
}
/** Arbitrary javascript object, to be validated */
export type JsObject = Record<string, unknown>;
/** @hidden
* Check that the given object, potentially comming from javascript, has all
* required properties to be a dataset.
*/
export function validateDataset(o: JsObject): void {
if (typeof o !== 'object') {
throw Error('the dataset must be a JavaScript object');
}
if (!('meta' in o)) {
throw Error('missing "meta" key in the dataset');
} else if (!(typeof o.meta === 'object' && o.meta !== null)) {
throw Error('"meta" must be an object in the dataset');
}
checkMetadata(o.meta as JsObject);
if (!('structures' in o)) {
throw Error('missing "structures" key in the dataset');
} else if (!Array.isArray(o.structures)) {
throw Error('"structures" must be an array in the dataset');
}
const [structureCount, atomsCount] = checkStructures(o.structures as JsObject[]);
let envCount = atomsCount;
if ('environments' in o) {
if (!Array.isArray(o.environments)) {
throw Error('"environments" must be an array in the dataset');
}
envCount = o.environments.length;
checkEnvironments(
o.environments as JsObject[],
o.structures as (Structure | UserStructure)[]
);
}
if ('shapes' in o) {
const check_shape = checkShapes(
o.shapes as Record<string, JsObject>,
structureCount,
envCount
);
if (check_shape !== '') {
throw Error('Error checking shape definitions: ' + check_shape);
}
const check_assign = assignShapes(
o.shapes as { [name: string]: ShapeParameters },
o.structures as Structure[]
);
if (check_assign !== '') {
throw Error('Error assigning shapes to structures: ' + check_assign);
}
}
if (!('properties' in o)) {
throw Error('missing "properties" key in then dataset');
} else if (!(typeof o.properties === 'object' && o.properties !== null)) {
throw Error('"properties" must be an object in the dataset');
}
if ('parameters' in o) {
if (!(typeof o.parameters === 'object' && o.parameters !== null)) {
throw Error('"parameters" must be an object in the dataset');
}
}
checkProperties(
o.properties as Record<string, JsObject>,
structureCount,
envCount,
o.parameters as Record<string, JsObject> | undefined
);
}
function checkMetadata(o: JsObject) {
if (!('name' in o)) {
throw Error('missing "meta.name" key in the dataset');
} else if (typeof o.name !== 'string') {
throw Error('"meta.name" must be a string in the dataset');
}
if ('description' in o && typeof o.description !== 'string') {
throw Error('"meta.description" should be a string in the dataset');
}
if ('authors' in o) {
if (!Array.isArray(o.authors)) {
throw Error('"meta.authors" must be an array in the dataset');
}
for (const a of o.authors) {
if (typeof a !== 'string') {
throw Error('"meta.authors" must be an array of strings in the dataset');
}
}
}
if ('references' in o) {
if (!Array.isArray(o.references)) {
throw Error('"meta.references" must be an array in the dataset');
}
for (const a of o.references) {
if (typeof a !== 'string') {
throw Error('"meta.references" must be an array of strings in the dataset');
}
}
}
}
function checkStructures(o: JsObject[]): [number, number] {
let atomsCount = 0;
for (let i = 0; i < o.length; i++) {
const structure = o[i];
if (
!(
'size' in structure &&
typeof structure.size === 'number' &&
isPositiveInteger(structure.size)
)
) {
throw Error(`missing 'size' for structure ${i}`);
}
atomsCount += structure.size;
if ('data' in structure) {
// user-specified structure, nothing to do
} else {
const message = checkStructure(structure);
if (message !== '') {
throw Error(`error in structure ${i}: ${message}`);
}
}
}
return [o.length, atomsCount];
}
function checkShapes(
shapes: Record<string, JsObject>,
structureCount: number,
envCount: number
): string {
// checks only that the shape and naming of shape options is correct.
// validity of the actual options is assessed in assignShapes
if (typeof shapes !== 'object' || shapes === null) {
return "'shapes' must be an object";
}
for (const [key, o_shape] of Object.entries(shapes as object)) {
const shape = o_shape as ShapeParameters;
if (!('kind' in shape)) {
return `missing "kind" in shape ${key}`;
}
if (typeof shape.kind !== 'string') {
return `shapes 'kind' must be a string for shape ${key}`;
}
if (!('parameters' in shape)) {
return `missing "parameters" in shape ${key}`;
}
const parameters = shape.parameters;
if ('structure' in parameters) {
const s_parameters = parameters.structure;
if (!Array.isArray(s_parameters)) {
return `'structure' parameters should be an array in shape ${key}`;
}
if (s_parameters.length !== structureCount) {
return `'structure' parameters in shape ${key} contain ${s_parameters.length} entries, but there are ${structureCount} structures.`;
}
}
if ('atom' in parameters) {
const a_parameters = parameters.atom;
if (!Array.isArray(a_parameters)) {
return `'atom' parameters should be an array in shape ${key}`;
}
if (a_parameters.length !== envCount) {
return `'atom' parameters in shape ${key} contain ${a_parameters.length} entries, but there are ${envCount} environments.`;
}
}
}
return '';
}
function validateShape(kind: string, parameters: Record<string, unknown>): string {
if (kind === 'sphere') {
return Sphere.validateParameters(parameters);
} else if (kind === 'ellipsoid') {
return Ellipsoid.validateParameters(parameters);
} else if (kind === 'arrow') {
return Arrow.validateParameters(parameters);
} else if (kind === 'cylinder') {
return Cylinder.validateParameters(parameters);
} else if (kind === 'custom') {
return CustomShape.validateParameters(parameters);
}
return '';
}
function assignShapes(
shapes: { [name: string]: ShapeParameters },
structures: Structure[]
): string {
// creates shapes associated with actual structures by picking slices of the full
// arrays. it also tests the shape validity, and for that it builds (but does not store)
// the fully expanded parameters for each shape
let atomsCount = 0;
for (let i_structure = 0; i_structure < structures.length; i_structure++) {
const structure = structures[i_structure];
structure.shapes = {};
for (const [name, shape] of Object.entries(shapes)) {
const parameters = {
global: shape.parameters.global,
structure: shape.parameters.structure,
atom: shape.parameters.atom,
};
let full_parameters = shape.parameters.global;
if (parameters.structure) {
parameters.structure = [parameters.structure[i_structure]];
full_parameters = { ...full_parameters, ...parameters.structure[0] };
}
if (parameters.atom) {
parameters.atom = parameters.atom.slice(atomsCount, atomsCount + structure.size);
for (const atom of parameters.atom) {
const atom_parameters = { ...full_parameters, ...atom };
const check = validateShape(shape.kind, atom_parameters);
if (check !== '') {
return `Validation error for an atom in shape ${name}: ${check}`;
}
}
} else {
const check = validateShape(shape.kind, full_parameters);
if (check !== '') {
return `Validation error for a structure in shape ${name}: ${check}`;
}
}
structure.shapes[name] = {
kind: shape.kind,
parameters: parameters,
};
}
atomsCount += structure.size;
}
return ''; // success!
}
/**
* Check that the given object is a structure. Return a string describing the
* issue with `s` if any, or the empty string if `s` looks like a valid
* structure.
*/
export function checkStructure(s: JsObject): string {
if (typeof s !== 'object' || s === null) {
throw Error('the structure must be a JavaScript object');
}
if (!('size' in s && typeof s.size === 'number' && isPositiveInteger(s.size))) {
return 'missing "size"';
}
for (const key of ['names', 'x', 'y', 'z']) {
if (!(key in s)) {
return `missing "${key}"`;
}
const array = s[key];
if (!Array.isArray(array)) {
return `"${key}" must be an array`;
}
if (s.size > 0 && array.length !== s.size) {
return `wrong size for "${key}", expected ${s.size}, got ${array.length}`;
}
}
if ('cell' in s) {
if (!(Array.isArray(s.cell) && s.cell.length === 9)) {
return '"cell" must be an array of size 9';
}
}
return '';
}
function checkProperties(
properties: Record<string, JsObject>,
structureCount: number,
envCount: number,
parameters?: Record<string, JsObject> | undefined
) {
for (const key in properties) {
const property = properties[key];
if (!('target' in property && typeof property.target === 'string')) {
Error(`'properties['${key}'].target' should be a string`);
}
if (property.target !== 'atom' && property.target !== 'structure') {
throw Error(`'properties['${key}'].target', should be 'atom' | 'structure'`);
}
if (!('values' in property && Array.isArray(property.values))) {
throw Error(`'properties["${key}"].values' should be an array`);
}
// check size if possible
let expected = 0;
if (property.target === 'atom') {
expected = envCount;
} else if (property.target === 'structure') {
expected = structureCount;
}
if (expected > 0 && property.values.length !== expected) {
throw Error(
`wrong size for 'properties['${key}'].values': expected ${expected}, got ${property.values.length}`
);
}
const initial = typeof property.values[0];
if (
initial !== 'string' &&
initial !== 'number' &&
!isMultidimensional(property.values as number[][])
) {
throw Error(
`'properties['${key}'].values' should contain string or number or an array of numbers`
);
}
for (const value of property.values) {
if (typeof value !== initial) {
throw Error(`'properties['${key}'].values' should be of a single type`);
}
}
// few checks on multidimensional properties
if (isMultidimensional(property.values as number[][])) {
// check if parameters exists
if (!parameters) {
throw Error(
`'parameters' should be provided for multidimensional properties '${key}'`
);
}
// check if parameter keyword exists and has the right format
const propertyParameters = property.parameters as string[];
if (!isArrayString(propertyParameters)) {
throw Error(`'properties['${key}'].parameters' should be an array of strings`);
}
// check if the length of parameters is 1 TODO: remove when support for multiple parameters is ready
if (propertyParameters.length !== 1) {
throw Error(`'properties['${key}'].parameters' should contain a single parameter`);
}
// check if parameters of the property exists in the parameters
//for (const value of propertyParameters) {
for (const value of propertyParameters) {
if (!(value in parameters)) {
throw Error(
`parameter '${value}' of 'properties['${key}']' does not appear in the list provided parameters`
);
}
}
// check if the length of the first array matches the length of the parameters
const initialValues = property.values[0] as number[];
for (const value of propertyParameters) {
const parameterValues = parameters[value].values as number[];
if (initialValues.length !== parameterValues.length) {
throw Error(
`'properties['${key}'].values' and 'parameters['${value}'].values' should have the same length`
);
}
}
// check if all the multidimensial array elements have the same length
if (!isConsistent2DArray(property.values as number[][])) {
throw Error(
`'properties['${key}].values' should contain arrays of the same length`
);
}
}
// check that units & description are valid
if ('description' in property && typeof property.description !== 'string') {
throw Error(`'properties['${key}'].description' should contain a string`);
}
if ('units' in property && typeof property.units !== 'string') {
throw Error(`'properties['${key}'].units' should contain a string`);
}
}
}
function checkEnvironments(o: JsObject[], structures: (Structure | UserStructure)[]) {
for (let i = 0; i < o.length; i++) {
const env = o[i];
if (!('structure' in env && typeof env.structure === 'number')) {
throw Error(`missing 'structure' for environment ${i}`);
}
if (!isPositiveInteger(env.structure) || env.structure >= structures.length) {
throw Error(
`out of bounds 'structure' for environment ${i}: index is \
${env.structure}, we have ${structures.length} structures`
);
}
if (!('center' in env && typeof env.center === 'number')) {
throw Error(`missing 'center' for environment ${i}`);
}
const size = structures[env.structure].size;
if (!isPositiveInteger(env.center) || env.center >= size) {
throw Error(
`out of bounds 'center' for environment ${i}: index is \
${env.center}, we have ${size} atoms in structure ${env.structure}`
);
}
if (!('cutoff' in env && typeof env.cutoff === 'number')) {
throw Error(`missing 'cutoff' for environment ${i}`);
}
}
}
function isPositiveInteger(number: number): boolean {
return Number.isInteger(number) && number >= 0;
}
function isMultidimensional(array: number[][]): boolean {
// check if an array is 2D
let result = true;
for (const value of array) {
result = Array.isArray(value) && result;
}
return result;
}
function isConsistent2DArray(array: number[][]): boolean {
// check if the elements of 2D array have the same length
const initial = array[0];
let result = true;
for (const value of array) {
result = value.length === initial.length && result;
}
return result;
}
function isArrayString(array: unknown): boolean {
if (Array.isArray(array)) {
let result = true;
for (const value of array) {
result = typeof value === 'string' && result;
}
return result;
} else {
return false;
}
}