-
Notifications
You must be signed in to change notification settings - Fork 887
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
Lobster io improvements #3627
Lobster io improvements #3627
Conversation
Hi @JaGeo , I made this changes as per our prep to update our schema in atomate2. |
Thank you! @naik-aakash , could you add tests for from_dict as well? And, doesn't monty already implement as_dict. Why do you need to implement this yourself? |
Just to add @naik-aakash and I will discuss this and then update the PR. |
Hello, If I may, here are some possibly useful comments. I would avoid having the filename in the init, but only have the data itself and have a from_file method that reads in. class Charge(MSONable):
def __init__(self, atomlist, types, mulliken, loewdin):
self.atomlist = atomlist
self.types= types
self.mulliken= mulliken
self.loewdin= loewdin
@classmethod
def from_file(cls, filename: str = "CHARGE.lobster"):
with zopen(filename, mode="rt") as file:
data = file.read().split("\n")[3:-3]
if len(data) == 0:
raise OSError("CHARGES file contains no data.")
atomlist: list[str] = []
types: list[str] = []
mulliken: list[float] = []
loewdin: list[float] = []
for line in data:
sp = line.split()
atomlist.append(sp[1] + sp[0])
types.append(sp[1])
mulliken.append(float(sp[2]))
loewdin.append(float(sp[3]))
return cls(atomlist=atomlist, types=types, mulliken=mulliken, loewdin=loewdin)
@property
def num_atoms(self)
return len(self.atomlist) Such an organization of the object is usually cleaner than init with filenames (which are actually not exactly compatible with MSONable, which is probably why @naik-aakash needed to reimplement as_dict). For standard MSONable to work, the init method should only set the attributes and do nothing else no "post-processing" of the arguments passed to init ideally. Then indeed you don't need to implement as_dict and from_dict in principle. Also I would avoid having capitalized attributes (herabove mulliken instead of Mulliken), otherwise you may think it's a class. Same applies to the other objects of course. I don't know if it's possible or easy to change for you on the packages/scripts using these but I would say it would improve the lobster io if it is possible. Best |
Thanks, @davidwaroquiers ! |
Some first thoughts from my side. It would be good to keep the breaking changes minimal as these classes are used by other people. Thus, I am not sure if implementing a from_file method at this point is a good idea. Vasp output io implements the classes similarily. And I think I took them as an example when I initially started with this development, e.g., pymatgen/pymatgen/io/vasp/outputs.py Line 143 in 098b2a9
In any case, if we implement an as_dict method, the from_dict one needs to work. And, we can rename the property names but I would suggest to add a deprecation warning then. I agree that I did not follow naming conventions very well when I initially implemented these classes. Might take some time to correct all of these issues. |
My answer sounds a bit too direct. Sorry for this. I |
No offense taken @JaGeo ;) Best, |
Thank you @davidwaroquiers , for your suggestions. They were certainly insightful and would be nice to keep in mind when implementing new stuff here on. |
Hi @JaGeo , I have adapted Charge class now, without introducing breaking change am able to get as_dict and from_dict working out of the box without having to define it myself. If you think this is fine, will add a similar approach to other classes as well. Note: Will add deprecation warning for renamed attributes , I just want to know if you think this approach is fine. 😃 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with the changes. We will need these modifications to make the integration of the LOBSTER data into the MP API and emmet builders possible.
@janosh : in case you have time to review it. (Technically, I can merge now but will of course not do it until one of the maintainers is fine with it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, i had the same thought as @davidwaroquiers and was planning to make similar comments but they've already been discussed so feel free to merge |
@janosh , great thanks. Is there any io class that you would recommend to model other io classes after? Might be helpful for future implementations. I think we have very different strategies depending on the io module at the moment. |
good question! maybe the recently added FHI-aims IO modules? e.g. pymatgen/pymatgen/io/aims/outputs.py Lines 30 to 50 in 0f474f6
the Lines 323 to 353 in 0f474f6
Line 48 in 0f474f6
|
Todo
Make MSONable