data-model.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Column } from 'mol-data/db'
  8. import { Tensor } from 'mol-math/linear-algebra'
  9. export interface File {
  10. readonly name?: string,
  11. readonly blocks: ReadonlyArray<Block>
  12. }
  13. export function File(blocks: ArrayLike<Block>, name?: string): File {
  14. return { name, blocks: blocks as any };
  15. }
  16. export interface Frame {
  17. readonly header: string,
  18. // Category names stored separately so that the ordering can be preserved.
  19. readonly categoryNames: ReadonlyArray<string>,
  20. readonly categories: Categories
  21. }
  22. export interface Block extends Frame {
  23. readonly saveFrames: Frame[]
  24. }
  25. export function Block(categoryNames: string[], categories: Categories, header: string, saveFrames: Frame[] = []): Block {
  26. return { categoryNames, header, categories, saveFrames };
  27. }
  28. export function SafeFrame(categoryNames: string[], categories: Categories, header: string): Frame {
  29. return { categoryNames, header, categories };
  30. }
  31. export type Categories = { readonly [name: string]: Category }
  32. export interface Category {
  33. readonly rowCount: number,
  34. readonly name: string,
  35. readonly fieldNames: ReadonlyArray<string>,
  36. getField(name: string): Field | undefined
  37. }
  38. export function Category(name: string, rowCount: number, fieldNames: string[], fields: { [name: string]: Field }): Category {
  39. return { rowCount, name, fieldNames: [...fieldNames], getField(name) { return fields[name]; } };
  40. }
  41. export namespace Category {
  42. export function empty(name: string): Category {
  43. return { rowCount: 0, name, fieldNames: [], getField(name: string) { return void 0; } };
  44. };
  45. }
  46. /**
  47. * Implementation note:
  48. * Always implement without using "this." in any of the interface functions.
  49. * This is to ensure that the functions can invoked without having to "bind" them.
  50. */
  51. export interface Field {
  52. readonly '@array': ArrayLike<any> | undefined
  53. readonly isDefined: boolean,
  54. readonly rowCount: number,
  55. str(row: number): string,
  56. int(row: number): number,
  57. float(row: number): number,
  58. valueKind(row: number): Column.ValueKind,
  59. areValuesEqual(rowA: number, rowB: number): boolean,
  60. toStringArray(params?: Column.ToArrayParams<string>): ReadonlyArray<string>,
  61. toIntArray(params?: Column.ToArrayParams<number>): ReadonlyArray<number>,
  62. toFloatArray(params?: Column.ToArrayParams<number>): ReadonlyArray<number>
  63. }
  64. export function getTensor(category: Category, field: string, space: Tensor.Space, row: number): Tensor.Data {
  65. const ret = space.create();
  66. if (space.rank === 1) {
  67. const rows = space.dimensions[0];
  68. for (let i = 0; i < rows; i++) {
  69. const f = category.getField(`${field}[${i + 1}]`);
  70. space.set(ret, i, !!f ? f.float(row) : 0.0);
  71. }
  72. } else if (space.rank === 2) {
  73. const rows = space.dimensions[0], cols = space.dimensions[1];
  74. for (let i = 0; i < rows; i++) {
  75. for (let j = 0; j < cols; j++) {
  76. const f = category.getField(`${field}[${i + 1}][${j + 1}]`);
  77. space.set(ret, i, j, !!f ? f.float(row) : 0.0);
  78. }
  79. }
  80. } else if (space.rank === 3) {
  81. const d0 = space.dimensions[0], d1 = space.dimensions[1], d2 = space.dimensions[2];
  82. for (let i = 0; i < d0; i++) {
  83. for (let j = 0; j < d1; j++) {
  84. for (let k = 0; k < d2; k++) {
  85. const f = category.getField(`${field}[${i + 1}][${j + 1}][${k + 1}]`);
  86. space.set(ret, i, j, k, !!f ? f.float(row) : 0.0);
  87. }
  88. }
  89. }
  90. } else throw new Error('Tensors with rank > 3 or rank 0 are currently not supported.');
  91. return ret;
  92. }