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

plot bands with only one kpoint #3798

Merged
merged 10 commits into from
Apr 12, 2020
Merged

Conversation

unkcpz
Copy link
Member

@unkcpz unkcpz commented Feb 24, 2020

Fix #2462 : Support plotting BandsData instances with a single kpoint.

@ltalirz ltalirz self-requested a review February 24, 2020 15:05
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @unkcpz for looking into this issue.

Would it be possible to add a test for this to make sure this issue does not come back?

E.g. here:

def test_bandsexport(self):

aiida/orm/nodes/data/array/bands.py Outdated Show resolved Hide resolved
aiida/orm/nodes/data/array/bands.py Outdated Show resolved Hide resolved
@unkcpz
Copy link
Member Author

unkcpz commented Feb 25, 2020

Sure I will add test. Now, it serves as an attempt to address the problem, I'll perfect solution for mpl plot and gnuplot both.
Thanks for review @ltalirz .

@unkcpz unkcpz changed the title WIP: plot energy levels when only 1 kpoints [WIP]: plot bands with only one kpoint Feb 27, 2020
@unkcpz unkcpz requested a review from ltalirz February 27, 2020 04:30
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @unkcpz !

I've had a quick look through and have some comments

@@ -825,7 +825,10 @@ def _prepare_mpl_singlefile(self, *args, **kwargs):

s_header = matplotlib_header_template.substitute()
s_import = matplotlib_import_data_inline_template.substitute(all_data_json=json.dumps(all_data, indent=2))
s_body = matplotlib_body_template.substitute()
if len(all_data['paths']) == 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seem to me that _prepare_mpl_singlefile could simply be reusing _prepare_mpl_withjson in order to avoid code duplication. Is this correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, these two can be combined. I'll try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, however, each of _prepare_mpl_* function is similar but slightly different. I can't think of a better way to combine them at the moment. Using a lone template string and leave all optional string as arguments seems like not a good idea.

@@ -1074,6 +985,115 @@ def show_mpl(self, **kwargs):
"""
exec(*self._exportcontent(fileformat='mpl_singlefile', main_file_name='', **kwargs)) # pylint: disable=exec-used

def _prepare_gnuplot(self,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you moved _prepare_gnuplot down in the file?
That makes it harder to spot the changes you made in review.

Copy link
Member Author

@unkcpz unkcpz Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just move all prepare_mpl_* functions together. _prepare_gnuplot was inserted in the middle of these _mpl* functions. I'll move it back for your review, if necessary I'll move mpl functions together at the end.

tests/cmdline/commands/test_data.py Outdated Show resolved Hide resolved
@ltalirz
Copy link
Member

ltalirz commented Apr 11, 2020

@unkcpz Just merged the other PR, I guess now it makes sense to continue work on this.

@codecov
Copy link

codecov bot commented Apr 11, 2020

Codecov Report

Merging #3798 into develop will increase coverage by 0.02%.
The diff coverage is 96.22%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3798      +/-   ##
===========================================
+ Coverage    78.18%   78.20%   +0.02%     
===========================================
  Files          460      460              
  Lines        33971    33982      +11     
===========================================
+ Hits         26559    26575      +16     
+ Misses        7412     7407       -5     
Flag Coverage Δ
#django 70.23% <96.22%> (+0.02%) ⬆️
#sqlalchemy 71.07% <96.22%> (+0.02%) ⬆️
Impacted Files Coverage Δ
aiida/orm/nodes/data/array/bands.py 76.21% <96.22%> (+1.40%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff9e8a2...68621fc. Read the comment docs.

@unkcpz unkcpz changed the title [WIP]: plot bands with only one kpoint plot bands with only one kpoint Apr 12, 2020
@unkcpz
Copy link
Member Author

unkcpz commented Apr 12, 2020

@ltalirz Seems working now:smile: After your review, I suggest to move all _prepare_mpl_* functions together for clarity.

Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @unkcpz !

Looks good to me, just a few minor comments & requests.

Good to merge afterwards; also feel free to move the functions to the appropriate place now.

@@ -343,6 +343,31 @@ def test_bandsexport(self):
self.assertEqual(res.exit_code, 0, 'The command did not finish correctly')
self.assertIn(b'[1.0, 3.0]', res.stdout_bytes, 'The string [1.0, 3.0] was not found in the bands' 'export')

def test_bandsexport_single_kp(self):
"""
test for issue #2462: plot band on single kpoint case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test for issue #2462: plot band on single kpoint case
Plot band for single k-point (issue #2462).

self.cli_runner.invoke(cmd_bands.bands_export, options, catch_exceptions=False)
with open('bands.gnu', 'r') as gnu_file:
res = gnu_file.read()
self.assertIn('vectors nohead', res, 'The string vectors nohead was not found in the gnuplot script')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertIn('vectors nohead', res, 'The string vectors nohead was not found in the gnuplot script')
self.assertIn('vectors nohead', res, 'The string "vectors nohead" was not found in the gnuplot script')

@@ -885,7 +891,10 @@ def _prepare_gnuplot(self,
"""
import os

dat_filename = os.path.splitext(main_file_name)[0] + '_data.dat'
if main_file_name is not None:
Copy link
Member

@ltalirz ltalirz Apr 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, the default seems to be '' rather than None

I suggest to change the default of the function argument to None, and then:

if not main_file_name:  # also catches '', just to be safe
    main_file_name = 'band.dat'  # not sure what a reasonable default is
dat_filename = os.path.splitext(main_file_name)[0] + '_data.dat'

Or even shorter, if you like:

main_file_name = main_file_name or 'band.dat' 
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default filename is not important here, in fact it is not used. Default filename is only used when export gnuplot without specify the extra_file with -o, if that happened it will throw a command line critical information. Here the file_name is only to make sure os.path.splitext is working and throw the right Critical information to user.

aiida/orm/nodes/data/array/bands.py Outdated Show resolved Hide resolved
@unkcpz unkcpz force-pushed the plot_energy_level branch from f01760f to 68621fc Compare April 12, 2020 18:01
@unkcpz
Copy link
Member Author

unkcpz commented Apr 12, 2020

Thanks for review! @ltalirz

@unkcpz unkcpz requested a review from ltalirz April 12, 2020 18:14
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, let's go!

@ltalirz ltalirz merged commit c74048b into aiidateam:develop Apr 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

verdi data bands export broken
2 participants