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

Make parameters importable from a directory having "pybamm" in its name #1919

Merged

Conversation

Saransh-cpp
Copy link
Member

Description

Check for pybamm/input/parameters instead of pybamm while trying to import parameters from the base package.

Fixes #1918

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ flake8
  • All tests pass: $ python run-tests.py --unit
  • The documentation builds: $ cd docs and then $ make clean; make html

You can run all three at once, using $ python run-tests.py --quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov
Copy link

codecov bot commented Jan 27, 2022

Codecov Report

Merging #1919 (eab470b) into develop (1c209f2) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1919   +/-   ##
========================================
  Coverage    99.32%   99.32%           
========================================
  Files          346      346           
  Lines        19002    19002           
========================================
  Hits         18874    18874           
  Misses         128      128           
Impacted Files Coverage Δ
pybamm/util.py 100.00% <100.00%> (ø)

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 1c209f2...eab470b. Read the comment docs.

@priyanshuone6
Copy link
Member

Could you add some tests

@Saransh-cpp Saransh-cpp marked this pull request as draft January 28, 2022 19:40
@Saransh-cpp
Copy link
Member Author

Could you add some tests

For some reason, the tests for the modified line don't work. I tried doing -

        pybamm.set_logging_level("DEBUG")
    
        import subprocess

        # create a new lithium_ion folder in the root PyBaMM directory
        subprocess.run(["pybamm_edit_parameter", "lithium_ion"])
       
        # path for a function in the created directory -> x/y/z/PyBaMM/lithium_ion/negative_electrode/ ....
        test_path = os.path.join(
            os.getcwd(),
            "lithium_ion",
            "negative_electrodes",
            "graphite_Chen2020",
            "graphite_LGM50_electrolyte_exchange_current_density_Chen2020.py",
        )

        # check if the lithium_ion directory is present in PyBaMM -> It is
        print(os.listdir(os.getcwd()))

        # load the function
        func = pybamm.load_function(test_path)
        self.assertEqual(
            func,
lithium_ion.negative_electrodes.graphite_Chen2020.graphite_LGM50_electrolyte_exchange_current_density_Chen2020.graphite_LGM50_electrolyte_exchange_current_density_Chen2020,  # noqa
      )

This shows me -

2022-01-29 17:36:39,496 - [DEBUG] util.load_function(312): Importing function 'graphite_LGM50_electrolyte_exchange_current_density_Chen2020' from file 'C:\Users\Saransh\Saransh_softwares\OpenSource\Python\PyBaMM\lithium_ion\negative_electrodes\graphite_Chen2020\graphite_LGM50_electrolyte_exchange_current_density_Chen2020' via path 'lithium_ion.negative_electrodes.graphite_Chen2020.graphite_LGM50_electrolyte_exchange_current_density_Chen2020'

ModuleNotFoundError: No module named 'lithium_ion'

Importing parameters from anywhere works completely fine outside of the testing suite.

Apparently, importing parameters from anywhere else than pybamm/input/parameters/ is not tested in PyBaMM. I couldn't really figure out why is it not working in tests but is working when used elsewhere.

else:
root_path = filename

# getcwd() returns "C:\\" when in the root drive and "C:\\a\\b\\c" otherwise
if root_path[0] == "\\" or root_path[0] == "/":
root_path = root_path[1:]
Copy link
Member

Choose a reason for hiding this comment

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

is "\\" a single character?

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

@Saransh-cpp Saransh-cpp marked this pull request as ready for review February 1, 2022 06:25
@valentinsulzer
Copy link
Member

Apparently, importing parameters from anywhere else than pybamm/input/parameters/ is not tested in PyBaMM. I couldn't really figure out why is it not working in tests but is working when used elsewhere.

Yeah this is strange, I get this error locally as well

CHANGELOG.md Outdated
@@ -13,7 +13,7 @@

## Bug fixes

- Parameters can now be imported from any given path in `Windows` ([#1900](https://github.com/pybamm-team/PyBaMM/pull/1900))
- Parameters can now be imported from any given path ([#1900](https://github.com/pybamm-team/PyBaMM/pull/1900), [#1919](https://github.com/pybamm-team/PyBaMM/pull/1919))
Copy link
Member

Choose a reason for hiding this comment

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

Could you merge develop and move this to the unreleased section

@valentinsulzer
Copy link
Member

Did you want to add a test for this? Or shall I merge?

@Saransh-cpp
Copy link
Member Author

Did you want to add a test for this? Or shall I merge?

I found the issue with the failing tests -

  • The tests pass when run using python run-tests.py --unit as the .../..../PyBaMM directory is in sys.path
  • The tests fail when test_utils.py is run individually, as now ..../..../PyBaMM/tests/unit is in sys.path and not .../..../PyBaMM

Copy link
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

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

Thanks!

@valentinsulzer valentinsulzer merged commit 3d55351 into pybamm-team:develop Feb 5, 2022
@Saransh-cpp Saransh-cpp deleted the issue-1918-parameter-path branch February 6, 2022 10:42
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.

Having "pybamm" in a directory's name throws import error
3 participants