Ejemplo n.º 1
0
def test_load_data():
    # create a random db name and make sure it doesn't exist already since we'll be deleting it
    db_exists = True
    while db_exists:
        db_name = "".join(
            random.choice(string.ascii_uppercase + string.digits) for _ in range(8)
        )
        db_loc = "%s.h5" % os.path.abspath(os.path.join(database_dir, db_name))
        if os.path.isfile(db_loc):
            db_exists = True
        else:
            db_exists = False

    # generate our data
    dat = DataHandler(db_name)
    label = "test_data"
    n_data_points = 4
    rgb_track = []
    target_track = []
    for ii in range(n_data_points):
        rgb = gen_images(res=[10, 10], img_scale=1, n_imgs=1)
        rgb_track.append(rgb)
        target = np.random.rand(3)
        target_track.append(target)
        dat.save(
            data={"rgb": rgb, "target": target},
            save_location="%s/data/%04d" % (label, ii),
        )

    imgs, targets = dl_utils.load_data(
        db_name=db_name, label=label, n_imgs=n_data_points
    )

    # check that our data matches what was saved
    assert np.array_equal(imgs, np.array(rgb_track))
    assert np.array_equal(targets, np.array(target_track))

    # remove our generated database
    os.remove(db_loc)
Ejemplo n.º 2
0
import numpy as np

from abr_analyze import DataHandler
from download_examples_db import check_exists as examples_db

examples_db()
save_location = 'data_handling'
recorded_time = np.linspace(0, 1, 100)
recorded_data = np.random.rand(100, 3)
data_dict = {'trajectory': recorded_data, 'time': recorded_time}

# instantiate a database with your desired name
dat = DataHandler(db_name='abr_analyze_examples')

# save our data
dat.save(data=data_dict, save_location=save_location, overwrite=True)

# load our data
# we can specify what parameters to load
data = dat.load(parameters=['trajectory', 'time'], save_location=save_location)
trajectory = data['trajectory']
time = data['time']

# we can rename our save_location as well
new_save_location = 'data_handling_rename'
dat.rename(old_save_location=save_location,
           new_save_location=new_save_location,
           delete_old=True)

# if we don't know the parameters, or want to load all of them and want to
# avoid writing out the entire list we can get all the keys at the save