Skip to content

Commit

Permalink
Fix CN histogram x-axis
Browse files Browse the repository at this point in the history
  • Loading branch information
bobleesj committed Jul 9, 2024
1 parent 45f3c61 commit b31cc97
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.0.2] - 2024-07-09



## [1.0.1] - 2024-07-05

### Fixed
Expand Down
5 changes: 1 addition & 4 deletions docs/notebooks/02_cif_ensemble.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -480,7 +477,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.1.-1"
}
},
"nbformat": 4,
Expand Down
40 changes: 24 additions & 16 deletions src/cifkit/figures/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from cifkit.utils import folder, prompt


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand All @@ -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)

Expand Down
13 changes: 0 additions & 13 deletions src/cifkit/models/cif_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
Expand All @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/models/test_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down
13 changes: 2 additions & 11 deletions tests/core/models/test_cif_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,4 +122,3 @@ _pd_proc_ls_proof_wR_factor 0.132
_refine_ls_R_I_factor 0.077

# End of data set 1956508

Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,3 @@ _refine_ls_R_factor_gt ?
_refine_ls_wR_factor_gt ?

# End of data set 1234749

0 comments on commit b31cc97

Please sign in to comment.