Skip to content

Commit b745db0

Browse files
committed
merge main and resove conflicts
2 parents ace0e50 + 10f5879 commit b745db0

23 files changed

+1450
-132
lines changed

.github/workflows/cicd.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ concurrency:
88

99
jobs:
1010
run-py-linter:
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-24.04
1212
strategy:
1313
matrix:
1414
python-version: [3.10.13]
@@ -33,7 +33,7 @@ jobs:
3333

3434
run-py-tests:
3535
needs: run-py-linter
36-
runs-on: ubuntu-20.04
36+
runs-on: ubuntu-24.04
3737
strategy:
3838
matrix:
3939
python-version:

dist/js/ArrayWithIds.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { RoundingOptions, ValueWithId } from "./ValueWithId";
2+
export declare class ArrayWithIds<T> {
3+
values: T[];
4+
ids: number[];
5+
constructor(values?: T[], ids?: number[]);
6+
static fromValues<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, values: U[]): C;
7+
static fromObjects<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, objects: {
8+
id: number;
9+
value: U;
10+
}[]): C;
11+
toJSON(): object[];
12+
toValueWithIdArray(): ValueWithId<T>[];
13+
getElementValueById(id: number): T | undefined;
14+
getElementValueByIndex(index: number): T | undefined;
15+
getElementIdByIndex(index: number): number | undefined;
16+
getElementIdByValue(value: T): number | undefined;
17+
filterByValues(valuesToKeep: T | T[]): void;
18+
filterByIndices(indices: number | number[]): void;
19+
filterByIds(ids: number | number[], invert?: boolean): void;
20+
equals(other: ArrayWithIds<T>): boolean;
21+
mapArrayInPlace(func: (value: T) => T): void;
22+
addItem(value: T, id?: number): void;
23+
removeItem(index: number, id?: number): void;
24+
}
25+
export declare class RoundedArrayWithIds<T> extends ArrayWithIds<T> {
26+
readonly roundingOptions: RoundingOptions;
27+
constructor(values?: T[], ids?: number[], options?: RoundingOptions);
28+
toJSON(): object[];
29+
}

dist/js/ArrayWithIds.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.RoundedArrayWithIds = exports.ArrayWithIds = void 0;
4+
const ValueWithId_1 = require("./ValueWithId");
5+
class ArrayWithIds {
6+
constructor(values = [], ids = []) {
7+
if (values.length !== ids.length) {
8+
throw new Error("Values and IDs must have the same length");
9+
}
10+
this.values = [...values];
11+
this.ids = [...ids];
12+
}
13+
static fromValues(values) {
14+
const ids = values.map((_, i) => i);
15+
return new this(values, ids);
16+
}
17+
static fromObjects(objects) {
18+
const values = objects.map((obj) => obj.value);
19+
const ids = objects.map((obj) => obj.id);
20+
return new this(values, ids);
21+
}
22+
toJSON() {
23+
return this.values.map((value, index) => ({
24+
id: this.ids[index],
25+
value: value !== null &&
26+
typeof value === "object" &&
27+
"toJSON" in value &&
28+
typeof value.toJSON === "function"
29+
? value.toJSON()
30+
: value,
31+
}));
32+
}
33+
toValueWithIdArray() {
34+
return this.values.map((value, index) => ValueWithId_1.ValueWithId.fromValueAndId(value, this.ids[index]));
35+
}
36+
getElementValueById(id) {
37+
const index = this.ids.indexOf(id);
38+
return index !== -1 ? this.values[index] : undefined;
39+
}
40+
getElementValueByIndex(index) {
41+
return this.values[index];
42+
}
43+
getElementIdByIndex(index) {
44+
return this.ids[index] || undefined;
45+
}
46+
getElementIdByValue(value) {
47+
const index = this.values.findIndex((v) => Array.isArray(v) && Array.isArray(value)
48+
? v.length === value.length && v.every((val, idx) => val === value[idx])
49+
: v === value);
50+
return index !== -1 ? this.ids[index] : undefined;
51+
}
52+
filterByValues(valuesToKeep) {
53+
const toHash = (v) => (Array.isArray(v) ? JSON.stringify(v) : String(v));
54+
const keepSet = new Set(Array.isArray(valuesToKeep) ? valuesToKeep.map(toHash) : [toHash(valuesToKeep)]);
55+
const filtered = this.values
56+
.map((value, i) => [value, this.ids[i]])
57+
.filter(([value]) => keepSet.has(toHash(value)));
58+
this.values = filtered.map(([v]) => v);
59+
this.ids = filtered.map(([_, id]) => id);
60+
}
61+
filterByIndices(indices) {
62+
const keepSet = new Set(Array.isArray(indices) ? indices : [indices]);
63+
this.values = this.values.filter((_, i) => keepSet.has(i));
64+
this.ids = this.ids.filter((_, i) => keepSet.has(i));
65+
}
66+
filterByIds(ids, invert = false) {
67+
const idSet = new Set(Array.isArray(ids) ? ids : [ids]);
68+
const keep = invert
69+
? this.ids.map((id, i) => (idSet.has(id) ? -1 : i)).filter((i) => i >= 0)
70+
: this.ids.map((id, i) => (idSet.has(id) ? i : -1)).filter((i) => i >= 0);
71+
this.values = keep.map((i) => this.values[i]);
72+
this.ids = keep.map((i) => this.ids[i]);
73+
}
74+
equals(other) {
75+
if (!(other instanceof ArrayWithIds))
76+
return false;
77+
if (this.values.length !== other.values.length)
78+
return false;
79+
if (this.ids.length !== other.ids.length)
80+
return false;
81+
return (this.values.every((v, i) => {
82+
const ov = other.values[i];
83+
return Array.isArray(v) && Array.isArray(ov)
84+
? v.length === ov.length && v.every((val, idx) => val === ov[idx])
85+
: v === ov;
86+
}) && this.ids.every((id, i) => id === other.ids[i]));
87+
}
88+
mapArrayInPlace(func) {
89+
this.values = this.values.map(func);
90+
}
91+
addItem(value, id) {
92+
const newId = id !== null && id !== void 0 ? id : Math.max(-1, ...this.ids) + 1;
93+
this.values.push(value);
94+
this.ids.push(newId);
95+
}
96+
removeItem(index, id) {
97+
if (id !== undefined) {
98+
index = this.ids.indexOf(id);
99+
if (index === -1)
100+
throw new Error("ID not found");
101+
}
102+
if (index < 0 || index >= this.values.length) {
103+
throw new Error("Index out of range");
104+
}
105+
this.values.splice(index, 1);
106+
this.ids.splice(index, 1);
107+
}
108+
}
109+
exports.ArrayWithIds = ArrayWithIds;
110+
class RoundedArrayWithIds extends ArrayWithIds {
111+
constructor(values = [], ids = [], options = ValueWithId_1.defaultRoundingOptions) {
112+
super(values, ids);
113+
this.roundingOptions = options;
114+
}
115+
toJSON() {
116+
return this.values.map((value, index) => new ValueWithId_1.RoundedValueWithId(this.ids[index], value, this.roundingOptions).toJSON());
117+
}
118+
}
119+
exports.RoundedArrayWithIds = RoundedArrayWithIds;

dist/js/ValueWithId.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ObjectWithIdAndValueSchema } from "@mat3ra/esse/dist/js/types";
2+
import { RoundingMethodEnum } from "./math";
3+
interface ValueWithIdSchema<T> {
4+
id: ObjectWithIdAndValueSchema["id"];
5+
value: T | null;
6+
}
7+
export declare class ValueWithId<T> {
8+
id: number;
9+
value: T | null;
10+
static defaultConfig: {
11+
id: number;
12+
value: null;
13+
};
14+
static fromValueAndId<U, C extends ValueWithId<U>>(this: new (args: {
15+
id: number;
16+
value: U;
17+
}) => C, value: U, id?: number): C;
18+
constructor({ id, value }?: ValueWithIdSchema<T>);
19+
/**
20+
* Converts the instance to a plain JavaScript object.
21+
*/
22+
toJSON(): object;
23+
/**
24+
* Checks if this instance is equal to another ValueWithId.
25+
*/
26+
equals<U>(other: ValueWithId<U>): boolean;
27+
}
28+
export interface RoundingOptions {
29+
precision: number;
30+
roundingMethod: RoundingMethodEnum;
31+
}
32+
export declare const defaultRoundingOptions: RoundingOptions;
33+
export declare class RoundedValueWithId<T> extends ValueWithId<T> {
34+
readonly precision: number;
35+
readonly roundingMethod: RoundingMethodEnum;
36+
constructor(id: number, value: T, options?: RoundingOptions);
37+
toJSON(): object;
38+
}
39+
export {};

dist/js/ValueWithId.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.RoundedValueWithId = exports.defaultRoundingOptions = exports.ValueWithId = void 0;
4+
const math_1 = require("./math");
5+
class ValueWithId {
6+
static fromValueAndId(value, id = 0) {
7+
return new this({ id, value });
8+
}
9+
constructor({ id, value } = ValueWithId.defaultConfig) {
10+
this.id = id;
11+
this.value = value;
12+
}
13+
/**
14+
* Converts the instance to a plain JavaScript object.
15+
*/
16+
toJSON() {
17+
if (this.value !== null &&
18+
typeof this.value === "object" &&
19+
"toJSON" in this.value &&
20+
typeof this.value.toJSON === "function") {
21+
return { id: this.id, value: this.value.toJSON() };
22+
}
23+
return { id: this.id, value: this.value };
24+
}
25+
/**
26+
* Checks if this instance is equal to another ValueWithId.
27+
*/
28+
equals(other) {
29+
if (!(other instanceof ValueWithId))
30+
return false;
31+
// because U may differ from T, we cast to unknown when comparing
32+
const v1 = this.value;
33+
const v2 = other.value;
34+
if (Array.isArray(v1) && Array.isArray(v2)) {
35+
if (v1.length !== v2.length)
36+
return false;
37+
for (let i = 0; i < v1.length; i++) {
38+
if (v1[i] !== v2[i])
39+
return false;
40+
}
41+
return this.id === other.id;
42+
}
43+
return this.id === other.id && v1 === v2;
44+
}
45+
}
46+
exports.ValueWithId = ValueWithId;
47+
ValueWithId.defaultConfig = {
48+
id: 0,
49+
value: null,
50+
};
51+
exports.defaultRoundingOptions = {
52+
precision: 9,
53+
roundingMethod: math_1.RoundingMethodEnum.HalfAwayFromZero,
54+
};
55+
class RoundedValueWithId extends ValueWithId {
56+
constructor(id, value, options = exports.defaultRoundingOptions) {
57+
super({ id, value });
58+
this.precision = options.precision;
59+
this.roundingMethod = options.roundingMethod;
60+
}
61+
toJSON() {
62+
return {
63+
id: this.id,
64+
value: math_1.math.roundArrayOrNumber(this.value, this.precision, this.roundingMethod),
65+
};
66+
}
67+
}
68+
exports.RoundedValueWithId = RoundedValueWithId;

dist/js/constants.d.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
export namespace coefficients {
2-
let EV_TO_RY: number;
3-
let BOHR_TO_ANGSTROM: number;
4-
let ANGSTROM_TO_BOHR: number;
5-
let EV_A_TO_RY_BOHR: number;
6-
}
7-
export namespace tolerance {
8-
let length: number;
9-
let lengthAngstrom: number;
10-
let pointsDistance: number;
11-
}
12-
export namespace units {
13-
let bohr: string;
14-
let angstrom: string;
15-
let degree: string;
16-
let radian: string;
17-
let alat: string;
18-
}
19-
export namespace ATOMIC_COORD_UNITS {
20-
let crystal: string;
21-
let cartesian: string;
22-
}
23-
export const HASH_TOLERANCE: 3;
24-
declare namespace _default {
25-
export { coefficients };
26-
export { tolerance };
27-
export { units };
28-
export { ATOMIC_COORD_UNITS };
29-
}
1+
export declare const coefficients: {
2+
EV_TO_RY: number;
3+
BOHR_TO_ANGSTROM: number;
4+
ANGSTROM_TO_BOHR: number;
5+
EV_A_TO_RY_BOHR: number;
6+
};
7+
export declare const tolerance: {
8+
length: number;
9+
lengthAngstrom: number;
10+
pointsDistance: number;
11+
};
12+
export declare const units: {
13+
bohr: string;
14+
angstrom: string;
15+
degree: string;
16+
radian: string;
17+
alat: string;
18+
};
19+
/**
20+
* @summary Coordinates units for a material's basis.
21+
*/
22+
export declare const ATOMIC_COORD_UNITS: {
23+
crystal: string;
24+
cartesian: string;
25+
};
26+
export declare const HASH_TOLERANCE = 3;
27+
declare const _default: {
28+
coefficients: {
29+
EV_TO_RY: number;
30+
BOHR_TO_ANGSTROM: number;
31+
ANGSTROM_TO_BOHR: number;
32+
EV_A_TO_RY_BOHR: number;
33+
};
34+
tolerance: {
35+
length: number;
36+
lengthAngstrom: number;
37+
pointsDistance: number;
38+
};
39+
units: {
40+
bohr: string;
41+
angstrom: string;
42+
degree: string;
43+
radian: string;
44+
alat: string;
45+
};
46+
ATOMIC_COORD_UNITS: {
47+
crystal: string;
48+
cartesian: string;
49+
};
50+
};
3051
export default _default;

dist/js/index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
import { ArrayWithIds, RoundedArrayWithIds } from "./ArrayWithIds";
12
import * as context from "./context";
23
import * as entity from "./entity";
34
import * as utils from "./utils";
4-
export declare const Code: {
5+
import { RoundedValueWithId, ValueWithId } from "./ValueWithId";
6+
import { RoundedVector3D, Vector3D } from "./vector";
7+
export { ArrayWithIds, ValueWithId, RoundedValueWithId, RoundedArrayWithIds, RoundedVector3D, Vector3D, };
8+
export { entity, context, utils };
9+
declare const Code: {
10+
ArrayWithIds: typeof ArrayWithIds;
11+
ValueWithId: typeof ValueWithId;
12+
RoundedArrayWithIds: typeof RoundedArrayWithIds;
13+
RoundedValueWithId: typeof RoundedValueWithId;
14+
RoundedVector3D: typeof RoundedVector3D;
15+
Vector3D: typeof Vector3D;
516
entity: typeof entity;
617
context: typeof context;
718
utils: typeof utils;
819
};
20+
export type CodeType = typeof Code;
21+
export default Code;

0 commit comments

Comments
 (0)