From b31cc9794dda7076c8778ba58940c0e790d2fd53 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Tue, 9 Jul 2024 19:38:31 -0400 Subject: [PATCH] Fix CN histogram x-axis --- CHANGELOG | 4 ++ docs/notebooks/02_cif_ensemble.ipynb | 5 +-- src/cifkit/figures/histogram.py | 40 +++++++++++-------- src/cifkit/models/cif_ensemble.py | 13 ------ test.ipynb | 9 ++++- tests/core/models/test_cif.py | 2 +- tests/core/models/test_cif_ensemble.py | 13 +----- tests/data/{ => cif}/cif_CN_init/1956508.cif | 3 +- .../{ => cif}/cif_CN_init/ErCo2.68In0.32.cif | 1 - 9 files changed, 41 insertions(+), 49 deletions(-) rename tests/data/{ => cif}/cif_CN_init/1956508.cif (99%) rename tests/data/{ => cif}/cif_CN_init/ErCo2.68In0.32.cif (99%) diff --git a/CHANGELOG b/CHANGELOG index b1d4e8d..36d9a47 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +## [1.0.2] - 2024-07-09 + + + ## [1.0.1] - 2024-07-05 ### Fixed diff --git a/docs/notebooks/02_cif_ensemble.ipynb b/docs/notebooks/02_cif_ensemble.ipynb index f7158f8..ab49958 100644 --- a/docs/notebooks/02_cif_ensemble.ipynb +++ b/docs/notebooks/02_cif_ensemble.ipynb @@ -301,9 +301,6 @@ "# Get file count per supercell atom count\n", "print(\"Supercell size stats:\", ensemble.supercell_size_stats)\n", "\n", - "# Get file count per min distance\n", - "print(\"Min distance stats:\", ensemble.min_distance_stats)\n", - "\n", "# Get file count per CN value by min dist method\n", "print(\"CN value using min dist method stats:\", ensemble.unique_CN_values_by_min_dist_method_stat)\n", "\n", @@ -480,7 +477,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.1.-1" } }, "nbformat": 4, diff --git a/src/cifkit/figures/histogram.py b/src/cifkit/figures/histogram.py index b857354..c00be40 100644 --- a/src/cifkit/figures/histogram.py +++ b/src/cifkit/figures/histogram.py @@ -4,6 +4,7 @@ import os import matplotlib.pyplot as plt +from matplotlib.ticker import MaxNLocator from cifkit.utils import folder, prompt @@ -77,17 +78,6 @@ def plot_histogram(attribute, stats, dir_path, display, output_dir): }, } - if attribute == "min_distance": - histogram = { - "data": stats, - "settings": { - "file_name": "min_distance.png", - "title": "Minimum Distances Distribution", - "xlabel": "Minimum Distance", - "key_data_type": "float", - }, - } - if attribute == "elements": histogram = { "data": stats, @@ -161,7 +151,7 @@ def generate_histogram(data, settings, display, output_dir: str) -> None: data = { float(key): data[key] for key in sorted(data.keys(), key=float) } - elif settings.get("key_data_type") == "int": + if settings.get("key_data_type") == "int": data = {int(key): data[key] for key in sorted(data.keys(), key=int)} keys = list(data.keys()) @@ -173,14 +163,32 @@ def generate_histogram(data, settings, display, output_dir: str) -> None: color=settings.get("color", "blue"), edgecolor=settings.get("edgecolor", "black"), ) - plt.title(settings["title"]) - plt.xlabel(settings["xlabel"]) + # Custom settings for specific histograms + file_name = settings.get("file_name") + if ( + file_name == "CN_by_min_dist_method.png" + or file_name == "CN_by_best_methods.png" + or file_name == "composition_type.png" + ): + # Assuming keys can be cast to integers, sort and find the range for x-ticks + int_keys = sorted(map(int, keys)) + plt.xticks( + range(min(int_keys), max(int_keys) + 1) + ) # Setting x-ticks to every integer within the range + plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True)) + else: + plt.xticks(rotation=settings.get("rotation", 45), ha="right") + + # y-axis settings + plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True)) plt.ylabel("Count") - plt.xticks(rotation=settings.get("rotation", 45), ha="right") + plt.xlabel(settings["xlabel"]) + + plt.title(settings["title"]) plt.grid(True, linestyle="--", linewidth=0.5) plt.tight_layout() - output_file_path = folder.get_file_path(output_dir, settings["file_name"]) + output_file_path = folder.get_file_path(output_dir, file_name) if not os.path.exists(output_dir): os.makedirs(output_dir) diff --git a/src/cifkit/models/cif_ensemble.py b/src/cifkit/models/cif_ensemble.py index f3b89a4..e108202 100644 --- a/src/cifkit/models/cif_ensemble.py +++ b/src/cifkit/models/cif_ensemble.py @@ -189,10 +189,6 @@ def site_mixing_type_stats(self) -> dict[str, int]: def supercell_size_stats(self) -> dict[int, int]: return self._attribute_stats("supercell_points", len) - @property - def min_distance_stats(self) -> dict[float, int]: - return self._attribute_stats("shortest_distance") - @property def unique_CN_values_by_min_dist_method_stat( self, @@ -419,15 +415,6 @@ def generate_supercell_size_histogram( output_dir, ) - def generate_min_distance_histogram(self, display=False, output_dir=None): - plot_histogram( - "min_distance", - self.min_distance_stats, - self.dir_path, - display, - output_dir, - ) - def generate_elements_histogram(self, display=False, output_dir=None): plot_histogram( "elements", diff --git a/test.ipynb b/test.ipynb index 77da06d..8ef8686 100644 --- a/test.ipynb +++ b/test.ipynb @@ -29,7 +29,7 @@ "# of files moved to 'error_invalid_label' folder: 0\n", "# of files moved to 'error_others' folder: 0\n", "\n", - "\u001b[33mInitializing 8 objects...\u001b[0m\n", + "\u001b[33mInitializing 8 Cif objects...\u001b[0m\n", "\u001b[32mFinished initialization!\u001b[0m\n" ] } @@ -46,6 +46,13 @@ "# print(ensemble.CN_unique_values_by_min_dist_method)\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/tests/core/models/test_cif.py b/tests/core/models/test_cif.py index 08b338d..585a98e 100644 --- a/tests/core/models/test_cif.py +++ b/tests/core/models/test_cif.py @@ -596,7 +596,7 @@ def test_init_without_radius_data(): def test_init_atomic_mixing_deficiency_without_atomic_mixing(): - file_path = "tests/data/cif_CN_init/1956508.cif" + file_path = "tests/data/cif/cif_CN_init/1956508.cif" assert Cif(file_path).CN_unique_values_by_best_methods == {11, 14, 15} diff --git a/tests/core/models/test_cif_ensemble.py b/tests/core/models/test_cif_ensemble.py index ab3022c..ba466b1 100644 --- a/tests/core/models/test_cif_ensemble.py +++ b/tests/core/models/test_cif_ensemble.py @@ -405,13 +405,6 @@ def test_supercell_size_stats(cif_ensemble_test): assert result == expected -@pytest.mark.fast -def test_min_distance_stats(cif_ensemble_test): - result = cif_ensemble_test.min_distance_stats - expected = {2.28: 1, 2.29: 1, 2.383: 1, 2.725: 2, 2.72: 1} - assert result == expected - - @pytest.mark.fast def test_unique_elements_stats(cif_ensemble_test): result = cif_ensemble_test.unique_elements_stats @@ -446,8 +439,8 @@ def test_unique_CN_values_by_best_methods_stat(cif_ensemble_test): """ -@pytest.mark.fast -def test_generate_histogram(cif_ensemble_test, tmp_path): +@pytest.mark.now +def test_generate_histogram(cif_ensemble_test): output_dir = "tests/data/cif/ensemble_test/histograms" # List of expected file names @@ -458,7 +451,6 @@ def test_generate_histogram(cif_ensemble_test, tmp_path): "space_group_number.png", "space_group_name.png", "supercell_size.png", - "min_distance.png", "elements.png", "CN_by_min_dist_method.png", "CN_by_best_methods.png", @@ -473,7 +465,6 @@ def test_generate_histogram(cif_ensemble_test, tmp_path): cif_ensemble_test.generate_space_group_number_histogram() cif_ensemble_test.generate_space_group_name_histogram() cif_ensemble_test.generate_supercell_size_histogram() - cif_ensemble_test.generate_min_distance_histogram() cif_ensemble_test.generate_elements_histogram() cif_ensemble_test.generate_CN_by_min_dist_method_histogram() cif_ensemble_test.generate_CN_by_best_methods_histogram() diff --git a/tests/data/cif_CN_init/1956508.cif b/tests/data/cif/cif_CN_init/1956508.cif similarity index 99% rename from tests/data/cif_CN_init/1956508.cif rename to tests/data/cif/cif_CN_init/1956508.cif index a472512..c995196 100644 --- a/tests/data/cif_CN_init/1956508.cif +++ b/tests/data/cif/cif_CN_init/1956508.cif @@ -39,7 +39,7 @@ _chemical_formula_weight 1071.3 # Bibliographic data _publ_section_title -'Nature of magnetic properties in R~3~Co~1.87~In~4~ where R= Ho, Er and Tm' +'Nature of magnetic properties in R~3~Co~1.87~In~4~ where R= Ho, Er and Tm' _journal_coden_ASTM PHTRDP _journal_name_full 'Phase Transitions' _journal_year 2018 @@ -122,4 +122,3 @@ _pd_proc_ls_proof_wR_factor 0.132 _refine_ls_R_I_factor 0.077 # End of data set 1956508 - diff --git a/tests/data/cif_CN_init/ErCo2.68In0.32.cif b/tests/data/cif/cif_CN_init/ErCo2.68In0.32.cif similarity index 99% rename from tests/data/cif_CN_init/ErCo2.68In0.32.cif rename to tests/data/cif/cif_CN_init/ErCo2.68In0.32.cif index 05b4b2b..b48a02b 100644 --- a/tests/data/cif_CN_init/ErCo2.68In0.32.cif +++ b/tests/data/cif/cif_CN_init/ErCo2.68In0.32.cif @@ -154,4 +154,3 @@ _refine_ls_R_factor_gt ? _refine_ls_wR_factor_gt ? # End of data set 1234749 -