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

Example Datasets with Python Package #501

Merged
merged 7 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gtsam/gtsam.i
Original file line number Diff line number Diff line change
Expand Up @@ -2800,7 +2800,6 @@ class SfmData {
gtsam::SfmTrack track(size_t idx) const;
};

string findExampleDataFile(string name);
pair<gtsam::NonlinearFactorGraph*, gtsam::Values*> load2D(string filename,
gtsam::noiseModel::Diagonal* model, int maxIndex, bool addNoise, bool smart);
pair<gtsam::NonlinearFactorGraph*, gtsam::Values*> load2D(string filename,
Expand Down
4 changes: 4 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ if (NOT GTSAM_BUILD_PYTHON)
return()
endif()

# Common directory for storing data/datasets stored with the package.
# This will store the data in the Python site package directly.
set(GTSAM_PYTHON_DATASET_DIR "./gtsam/Data")

# Generate setup.py.
file(READ "${PROJECT_SOURCE_DIR}/README.md" README_CONTENTS)
configure_file(${PROJECT_SOURCE_DIR}/python/setup.py.in
Expand Down
2 changes: 2 additions & 0 deletions python/gtsam/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from . import utils
from .gtsam import *
from .utils import findExampleDataFile


def _init():
Expand Down
22 changes: 22 additions & 0 deletions python/gtsam/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import glob
import os.path as osp


def findExampleDataFile(name):
"""
Find the example data file specified by `name`.
"""
# This is okay since gtsam will already be loaded
# before this function is accessed. Thus no effect.
import gtsam

site_package_path = gtsam.__path__[0]
# add the * at the end to glob the entire directory
data_path = osp.join(site_package_path, "Data", "*")

extensions = ("", ".graph", ".txt", ".out", ".xml", ".g2o")

for data_file_path in glob.glob(data_path, recursive=True):
for ext in extensions:
if (name + ext) == osp.basename(data_file_path):
return data_file_path
7 changes: 7 additions & 0 deletions python/setup.py.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
import os
import sys

Expand All @@ -8,6 +9,11 @@ except ImportError:

packages = find_packages(where=".")
print("PACKAGES: ", packages)

data_path = '${GTSAM_SOURCE_DIR}/examples/Data/'
data_files_and_directories = glob.glob(data_path + '**', recursive=True)
data_files = [x for x in data_files_and_directories if not os.path.isdir(x)]

package_data = {
'': [
'./*.so',
Expand Down Expand Up @@ -44,6 +50,7 @@ setup(
],
packages=packages,
package_data=package_data,
data_files=[('${GTSAM_PYTHON_DATASET_DIR}', data_files),],
test_suite="gtsam.tests",
install_requires=["numpy"],
zip_safe=False,
Expand Down