From 6b2e55c442ae5faa990e1db2d89bec4061188cd8 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 29 Sep 2023 15:54:19 -0700 Subject: [PATCH 1/3] chore: add guard clause to filter functions --- src/include/meta_properties/pseudopotential.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/include/meta_properties/pseudopotential.js b/src/include/meta_properties/pseudopotential.js index 62457e4..e83e757 100644 --- a/src/include/meta_properties/pseudopotential.js +++ b/src/include/meta_properties/pseudopotential.js @@ -95,16 +95,19 @@ export class Pseudopotential extends Property { return Pseudopotential.filterUnique(this.filterByAppName(array, appName)); } - static filterRawDataByPath(rawData, pseudoPath = "") { + static filterRawDataByPath(pseudos, pseudoPath = "") { + if (!pseudoPath) return pseudos; const regexp = new RegExp(pseudoPath); - return rawData.filter((el) => el.path.match(regexp)); + return pseudos.filter((el) => el.path.match(regexp)); } static filterByAppName(pseudos, appName) { + if (!appName) return pseudos; return pseudos.filter((pseudo) => pseudo.apps.includes(appName)); } static filterByElements(pseudos, elements) { + if (!elements || elements.length === 0) return pseudos; return pseudos.filter((pseudo) => elements.includes(pseudo.element)); } From 4e1a5351a59f0f703c28bf523b2cd2abea597cb8 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Tue, 3 Oct 2023 11:37:51 -0700 Subject: [PATCH 2/3] update: add guard clause to filter by exchange-correlation --- src/include/meta_properties/pseudopotential.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/include/meta_properties/pseudopotential.js b/src/include/meta_properties/pseudopotential.js index e83e757..6928312 100644 --- a/src/include/meta_properties/pseudopotential.js +++ b/src/include/meta_properties/pseudopotential.js @@ -65,18 +65,26 @@ export class Pseudopotential extends Property { } /** - * @summary Exclusive filter of raw data by all fields of the passed object - * @param {Array} rawData + * @summary Exclusive filter of raw pseudopotential array by approximation and functional + * @param {Pseudopotential[]} pseudos * @param {Object} exchangeCorrelation * @param {String} exchangeCorrelation.approximation * @param {String} exchangeCorrelation.functional */ - static filterRawDataByExchangeCorrelation(rawData, exchangeCorrelation) { - const { functional } = exchangeCorrelation; + static filterRawDataByExchangeCorrelation(pseudos, exchangeCorrelation) { + const { approximation, functional } = exchangeCorrelation; + if (!functional && !approximation) return pseudos; + + if (!functional) { + return pseudos.filter( + (item) => item.exchangeCorrelation?.approximation === approximation, + ); + } + const isCompatibleWithOther = Object.keys(this.compatibleExchangeCorrelation).includes( functional, ); - return rawData.filter((item) => { + return pseudos.filter((item) => { return isCompatibleWithOther ? this.compatibleExchangeCorrelation[functional].includes( item.exchangeCorrelation?.functional, From 8cd197f42029db6ba3c9e76db9ca4699ad2b30c5 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Tue, 3 Oct 2023 11:38:13 -0700 Subject: [PATCH 3/3] test: updated filter by exchange-correlation --- tests/pseudopotential.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/pseudopotential.test.js b/tests/pseudopotential.test.js index 5379014..613f668 100644 --- a/tests/pseudopotential.test.js +++ b/tests/pseudopotential.test.js @@ -154,6 +154,23 @@ describe("Pseudopotentials", () => { expect(sortedPseudos).to.have.length(3); // there are 3 PBE pseudos above expect(sortedPseudos.map((p) => p.exchangeCorrelation.functional)).to.include("pbe"); }); + it("can be filtered by exchange-correlation approximation only", () => { + const exchangeCorrelation = { approximation: "lda", functional: "" }; + const sortedPseudos = Pseudopotential.filterRawDataByExchangeCorrelation( + pseudos, + exchangeCorrelation, + ); + expect(sortedPseudos).to.have.length(1); // there is 1 LDA pseudo above + expect(sortedPseudos.map((p) => p.exchangeCorrelation.functional)).to.include("pz"); + }); + it("should return original array if approximation and functional are falsy", () => { + const exchangeCorrelation = { approximation: "", functional: "" }; + const sortedPseudos = Pseudopotential.filterRawDataByExchangeCorrelation( + pseudos, + exchangeCorrelation, + ); + expect(sortedPseudos).to.have.length(PSEUDO_CONFIGS.length); // there is 1 LDA pseudo above + }); it("can be filtered by pseudopotential type", () => { const filtered = Pseudopotential.filterByType(pseudos, "paw"); const filteredWithObject = Pseudopotential.applyPseudoFilters(pseudos, { type: "paw" });