geo-deep-learning icon indicating copy to clipboard operation
geo-deep-learning copied to clipboard

[feature request]: While testing, Pycharm's default working directory cannot always access test data

Open remtav opened this issue 2 years ago • 0 comments

Description

(Ideally, we'd solve issue #408 before this issue to remove duplicate references to test data)

For example, if a test is located under tests/utils/, the default working directory becomes this directory, making the test data under tests/data and tests/tiling unreachable: image

This results in a FileNotFoundError when executing tests:

E                   FileNotFoundError: [Errno 2] No such file or directory: 'tests/data/massachusetts_buildings_kaggle.zip'

..\..\..\..\Miniconda3\envs\geo_deep_env\lib\zipfile.py:1249: FileNotFoundError

Currently, for each test, the configuration must be changed manually to point to correct directory (path/to/geo-deep-learning), which is obviously time-consuming.

EDIT: solaris' solution (cleaner)

Under tests/data/__init__.py, define the file's directory as data_dir: https://github.com/CosmiQ/solaris/blob/5315390942e05e919555088361bd3df42d4f5a18/solaris/data/init.py#L10

Then use this variable to get test data, no matter where the test script is located (under tests/data) https://github.com/CosmiQ/solaris/blob/5315390942e05e919555088361bd3df42d4f5a18/tests/test_utils/test_data.py#L12

Other solution

This small function could solve this issue:

def set_test_wd():
    test_dir = Path('tests/')
    if not test_dir.is_dir():
        cwd = Path(os.getcwd())
        new_wdir = cwd
        for index in range(len(cwd.parts)):
            if (new_wdir / test_dir).is_dir():
                break
            new_wdir = new_wdir.parent
            if index == len(cwd.parts) - 1:
                raise NotADirectoryError(f"Couldn't locate test directory")
        os.chdir(new_wdir)

Then, our tests could use this function right before places where relative paths to test data are set:

# tests/foo/foo.py
def test_foo():
    set_test_dir()
    extract_archive(src="tests/data/spacenet.zip")
    data = read_csv("tests/tiling/tiling_segmentation_binary-singleband_ci.csv")

remtav avatar Dec 07 '22 16:12 remtav