-
Notifications
You must be signed in to change notification settings - Fork 22
Addign editor to fix atomic positions in x,y,z and to add constraints #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cpignedoli
wants to merge
6
commits into
main
Choose a base branch
from
feature/fixatoms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
921e4cd
draft of GUI
cpignedoli 0b94534
Added posisbility to fix atoms in x,y,z. Updated tables accordingly
cpignedoli 1601737
added constraints to the summary
cpignedoli 940476b
bug fix
cpignedoli 69ad8f1
added distance constraint
cpignedoli 5bb82db
do not remember what I changed
cpignedoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| from __future__ import annotations | ||
| from pydoc import html | ||
|
|
||
| from aiida_quantumespresso.tools import data | ||
| from matplotlib import legend | ||
| import traitlets as tl | ||
| from ase.formula import Formula | ||
|
|
||
|
|
@@ -74,30 +77,154 @@ def _get_structure_info(self): | |
| </div> | ||
| """ | ||
|
|
||
| # def _get_atom_table_data(self): | ||
| # """Build table data; if 'fixed_atoms' is present in structure attributes, | ||
| # add a 'Free x,y,z' column showing '✓' for free (1) and 'x' for fixed (0).""" | ||
| # # Try to get fixed_atoms from AiiDA StructureData attributes | ||
| # fixed_atoms = self.structure.base.attributes.all.get('fixed_atoms', None) | ||
|
|
||
| # ase_atoms = self.structure.get_ase() | ||
|
|
||
| # # Header | ||
| # data = [["Atom index", "Chemical symbol", "Tag", "x (Å)", "y (Å)", "z (Å)"]] | ||
| # if fixed_atoms is not None: | ||
| # data[0].append("Free x,y,z") | ||
|
|
||
| # positions = ase_atoms.positions | ||
| # chemical_symbols = ase_atoms.get_chemical_symbols() | ||
| # tags = ase_atoms.get_tags() | ||
|
|
||
| # def fmt_free(mask): | ||
| # """mask: tuple/list of three 0/1; 0=free -> 'X', 1=fixed -> ' '.""" | ||
| # try: | ||
| # x, y, z = mask | ||
| # except Exception: | ||
| # x = y = z = 0 | ||
| # # If your UI collapses spaces, replace ' ' with '·' or '\u00A0' (NBSP). | ||
| # return f"({'x' if x == 0 else '✓'} {'x' if y == 0 else '✓'} {'x' if z == 0 else '✓'})" | ||
|
|
||
| # for idx, (symbol, tag, pos) in enumerate(zip(chemical_symbols, tags, positions), start=1): | ||
| # formatted_position = [f"{coord:.2f}" for coord in pos] | ||
| # row = [idx, symbol, tag, *formatted_position] | ||
|
|
||
| # if fixed_atoms is not None: | ||
| # mask = fixed_atoms[idx - 1] if idx - 1 < len(fixed_atoms) else (0, 0, 0) | ||
| # row.append(fmt_free(mask)) | ||
|
|
||
| # data.append(row) | ||
|
|
||
| # return data | ||
| def _get_atom_table_data(self): | ||
| structure = self.structure.get_ase() | ||
| data = [ | ||
| [ | ||
| "Atom index", | ||
| "Chemical symbol", | ||
| "Tag", | ||
| "x (Å)", | ||
| "y (Å)", | ||
| "z (Å)", | ||
| ] | ||
| ] | ||
| positions = structure.positions | ||
| chemical_symbols = structure.get_chemical_symbols() | ||
| tags = structure.get_tags() | ||
|
|
||
| for index, (symbol, tag, position) in enumerate( | ||
| zip(chemical_symbols, tags, positions), start=1 | ||
| ): | ||
| formatted_position = [f"{coord:.2f}" for coord in position] | ||
| data.append([index, symbol, tag, *formatted_position]) | ||
| """Build table data. | ||
|
|
||
| - If 'fixed_atoms' is present in AiiDA StructureData attributes (or ASE arrays), | ||
| add a 'Free x,y,z' column showing '✓' for free (1) and 'x' for fixed (0). | ||
| - If constraints exist (CONSTRAINTS block), add a 'Constr.' column with | ||
| star labels (*1, *2, …) for atoms involved in each constraint. | ||
| """ | ||
| # --- source data --- | ||
| attrs_all = getattr(getattr(getattr(self.structure, "base", None), "attributes", None), "all", {}) or {} | ||
| fixed_atoms = attrs_all.get("fixed_atoms", None) | ||
|
|
||
| ase_atoms = self.structure.get_ase() | ||
|
|
||
| # Try also to read fixed mask from ASE arrays if attributes missing | ||
| if fixed_atoms is None and "fixed_atoms" in getattr(ase_atoms, "arrays", {}): | ||
| fixed_atoms = ase_atoms.arrays["fixed_atoms"].tolist() | ||
|
|
||
| # constraints: prefer attributes, else ASE info | ||
| constraints_info = attrs_all.get("CONSTRAINTS") | ||
| if constraints_info is None: | ||
| constraints_info = ase_atoms.info.get("CONSTRAINTS", None) if hasattr(ase_atoms, "info") else None | ||
| # normalize constraints_info | ||
| if not isinstance(constraints_info, dict): | ||
| constraints_info = {"number": 0, "tolerance": "1e-6", "list": []} | ||
| constraints_list = constraints_info.get("list", []) or [] | ||
|
|
||
| # Build star marks per atom and a legend (legend not returned here) | ||
| def build_marks(n_atoms, entries): | ||
| marks = {i: [] for i in range(n_atoms)} | ||
| legend = [] | ||
| for k, entry in enumerate(entries, start=1): | ||
| legend.append(f"*{k} {entry}") | ||
| parts = str(entry).strip().split() | ||
| if len(parts) >= 4 and parts[0] == "distance": | ||
| try: | ||
| i1 = int(parts[1]) - 1 | ||
| i2 = int(parts[2]) - 1 | ||
| except Exception: | ||
| continue | ||
| label = f"*{k}" | ||
| if 0 <= i1 < n_atoms: | ||
| marks[i1].append(label) | ||
| if 0 <= i2 < n_atoms: | ||
| marks[i2].append(label) | ||
| return marks, legend | ||
|
|
||
| marks_map, legend = build_marks(len(ase_atoms), constraints_list) | ||
|
|
||
| # --- header --- | ||
| data = [["Atom index", "Chemical symbol", "Tag", "x (Å)", "y (Å)", "z (Å)"]] | ||
| if fixed_atoms is not None: | ||
| data[0].append("Free x,y,z") | ||
| # add constraints column if any constraints exist | ||
| if constraints_list: | ||
| data[0].append("Constr.") | ||
|
|
||
| positions = ase_atoms.positions | ||
| chemical_symbols = ase_atoms.get_chemical_symbols() | ||
| tags = ase_atoms.get_tags() | ||
|
|
||
| def fmt_free(mask): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think here, you can put a condition if mask is not an instance , mask = (0,0,0) and then you do return f"({' '.join('✓' if val else 'x' for val in mask)})" |
||
| """mask: 3-tuple/list of 0/1; 1=free → '✓', 0=fixed → 'x'.""" | ||
| try: | ||
| x, y, z = mask | ||
| except Exception: | ||
| x = y = z = 1 # default free if malformed | ||
| return f"({'✓' if int(x)==1 else 'x'} {'✓' if int(y)==1 else 'x'} {'✓' if int(z)==1 else 'x'})" | ||
|
|
||
| for idx, (symbol, tag, pos) in enumerate(zip(chemical_symbols, tags, positions), start=1): | ||
| formatted_position = [f"{coord:.2f}" for coord in pos] | ||
| row = [idx, symbol, tag, *formatted_position] | ||
|
|
||
| if fixed_atoms is not None: | ||
| # robustly fetch mask; default to all free if missing | ||
| mask = (1, 1, 1) | ||
| try: | ||
| if 0 <= (idx - 1) < len(fixed_atoms): | ||
| mask = fixed_atoms[idx - 1] | ||
| except Exception: | ||
| pass | ||
| row.append(fmt_free(mask)) | ||
|
|
||
| if constraints_list: | ||
| stars = " ".join(marks_map.get(idx - 1, [])) | ||
| row.append(stars) | ||
|
|
||
| data.append(row) | ||
| # temporary quick fix to show teh legend of constraints | ||
| if legend: | ||
| ncols = len(data[0]) | ||
| legend_text = "; ".join(legend) | ||
|
|
||
| injected = ( | ||
| # close the last normal row | ||
| f'</td></tr>' | ||
| # add a non-interactive full-width legend row | ||
| f'<tr class="constraints-legend-row" ' | ||
| f'style="pointer-events:none; user-select:none;">' | ||
| f'<td colspan="{ncols}" style="text-align:right;">{html.escape(legend_text)}</td>' | ||
| f'</tr>' | ||
| # open a dummy row/cell so the renderer’s trailing </td></tr> stays balanced | ||
| f'<tr><td>' | ||
| ) | ||
| data.append([injected]) | ||
|
|
||
|
|
||
|
|
||
| return data | ||
|
|
||
|
|
||
| def get_model_state(self): | ||
| return { | ||
| "selected_view": self.selected_view, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.