Skip to content
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

Clean up asciimaps #436

Open
youngmit opened this issue Oct 7, 2021 · 13 comments · May be fixed by #1767
Open

Clean up asciimaps #436

youngmit opened this issue Oct 7, 2021 · 13 comments · May be fixed by #1767
Assignees
Labels
cleanup Code/comment cleanup: Low Priority good first issue Good for newcomers

Comments

@youngmit
Copy link
Contributor

youngmit commented Oct 7, 2021

The functionality in utils/asciimaps.py has been pretty brittle, and difficult to extend and debug, and frankly pretty error prone to use. We've been talking for a while about perhaps taking another stab at the problem, so I am making this ticket to get it out there as a thing to do. I think a lot of the complexity stems from modelling the problem with a bunch of classes, which have pretty abstract state that needs to be ushered through a handful of methods that are subclassed in pretty nuanced and difficult to understand ways. I think the problem of mapping from a structured text grid to a dictionary of i,j indices -> specifier would better be suited to straight-up functions (e.g., fullHexFlatsUpToAscii(d) and its inverse fullHexFlatsUpToDict(text). We can still provide a sort of pseudo-factory, which returns the appropriate functions given a provided geom and symmetry condition. If whoever tackles this feels like the use of some private classes would ease implementation of these functions, they are more than welcome to do so, or even try using the classes that we already have to get a fist-cut implementation of the functional interface, but I suspect that the class approach is more complicated than it needs to be.

In summary:

  • replace the classes in asciimaps.py with functions for going to/from ascii<->dict
  • refactor client code to use the functions. life should be way simpler!
@youngmit
Copy link
Contributor Author

youngmit commented Oct 7, 2021

@davis8dd think you'd be interested?

@ntouran
Copy link
Member

ntouran commented Oct 8, 2021

While pondering this problem, it's always wise to click around on this unbelievable hexagon page. They even have an implementation guide in many languages, including python!

That said, the basic requirements of this kind of system would be to convert various 2-D geometrical grids to and from a compact ASCII-based representation losslessly, including:

  • Cartesian geometry
  • Hexagon geometry (both flats up and points up)
  • RZ geometry (may be basically identical to Cartesian) (bonus points, not required)

The mapping is preferred to be whitespace delimited but multi-character. We sometimes back things like (5,4) as well as F as well as GT into indices.

Note that this messes with brains a bit because while matrix nomenclature usually have an 'origin' at the top left, spatial coordinates usually have the origin in the bottom left. So we have to decide in advance which one we want. In other words, we have to pick a specific mapping style for each and make a diagram describing it.

Example:

"""
A B
C D
"""

might lead to

{ 
    (0,0): "C",
    (0,1): "D",
    (1,0): "A", 
    (1,1): "B"
}

or 4 or more other possible mappings. Thus, we should explicitly specify these in advance for each geometry option with a graphical example and then once we all agree on what the mappings should look like, then we can implement. That's effectively what we have done in test_asciimaps.py to a degree but it could be better.

@scottyak
Copy link
Contributor

Question -- I'm considering using dataclasses as an implementation detail (since __post_init__ is nice), but it is only available from 3.7 onwards. I suspect that's okay, since the latest release of ARMI appears to be tested against 3.7, 3.8, and 3.9 but not 3.6: https://github.com/terrapower/armi/runs/4267083946?check_suite_focus=true

However, since setup.py says >=3.6, I thought it would be good to check first. (there's a typo there too -- "s/requres/requires")

armi/setup.py

Line 49 in b94744f

python_requres=">=3.6",

So can I assume the user has python >=3.7 ?

@jakehader
Copy link
Member

I think it's a fair assumption that most users have Python 3.7+ internally at TerraPower, but I cannot speak for all external users. Right now the installation docs do state Python 3.6+ even though the setup.py is broken.

@scottyak
Copy link
Contributor

It seems, then, that either release v0.2 should be tested on python 3.6 (since it seems like it currently isn't), or that setup.py and the release doc should be updated to say it requires >=3.7 ?

I think dropping python 3.6 support might not be a terrible idea -- if someone is using python 3.6, they could depend on the previous release. Since release v0.2 was only 5 days ago, it seems unlikely that anyone is relying on it yet. This could be a good opportunity to drop python 3.6 support -- otherwise you might have to wait till the next major release to do so :(

The reason why I'm hoping that python 3.6 support can be dropped is that, besides dataclasses, python 3.7 also brings types and generics, which would make refactorings and cleanups so much easier. Perhaps some subclasses of abc.ABC can also be simplified.

@jakehader
Copy link
Member

@john-science - maybe we can hot patch the v0.2 release to enforce 3.7+? I don't see a reason why we need to support 3.6 at this time.

@john-science
Copy link
Member

@jakehader @scottyak My preference is to only support Python 3.7-3.9, as those are really the only versions that we test for seriously. And while I'm generally a fan of supporting a wide breadth of versions for an importable library... I don't know, I think for a scientific modeling framework I'm more in favor of constraining the versions to be better tested.

Let me ask around, and see if anyone depends on 3.6.

@john-science
Copy link
Member

@jakehader @scottyak It looks like no one is using Python 3.6 anyway. Nick is hesitant to drop support for 3.10, so I think saying we support Python 3.7+ is a pretty safe bet for everyone.

@jakehader
Copy link
Member

@scottyak, the 3.6 requirement has been removed in #491.

@john-science
Copy link
Member

john-science commented Oct 10, 2023

@sdow1ll Is taking this ticket!

Thanks Silba!

(I just assigned it to myself so no one else would grab it.)

@john-science john-science self-assigned this Oct 10, 2023
@davis8dd
Copy link
Contributor

Sorry for leaving this one assigned for so long!

@sdow1ll
Copy link

sdow1ll commented Oct 25, 2023

Hi @john-science I have a working Jupyter Notebook of the asciimaps.py file that basically has the converted functions from the asciimap class, but one thing that I got stuck on was that I noticed at the end of the asciimaps.py file, there is a code block of the following:

def asciiMapFromGeomAndSym(geomType: str, symmetry: str):
    """Get a ascii map class from a geometry type."""
    from armi.reactor import geometry
    
    MAP_FROM_GEOM = {
        (geometry.HEX, geometry.THIRD_CORE): AsciiMapHexThird,
        (geometry.HEX, geometry.FULL_CORE): AsciiMapHexFullTipsUp,
        (geometry.CARTESIAN, None): AsciiMapCartesian,
        (geometry.CARTESIAN, geometry.FULL_CORE): AsciiMapCartesian,
    }
return MAP_FROM_GEOM[(geomType, symmetry)]

Is this something that I should try to edit as well? When looking at this, I notice that the MAP_FROM_GEOM dictionary relies on the original classes. Are the functions I wrote basically an additional feature to this file, and that I should not touch the following code block above?

@john-science
Copy link
Member

The only two references to that function are here:

latticeCls = asciimaps.asciiMapFromGeomAndDomain(self.geom, symmetry.domain)

aMap = asciimaps.asciiMapFromGeomAndDomain(

So, refactoring those two use-cases will be necessary to do your refactoring of the asciimaps utils.

(Sorry for the long delay.)

@tlimato tlimato linked a pull request Jul 7, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code/comment cleanup: Low Priority good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants