def my_experiment_dfgsdgfdaf(a=1):

            assert a==4, 'Only meant to run aaa variant.'
            experiment_id = get_current_experiment_id()

            rec = get_current_experiment_record()

            record_id = get_current_record_id()

            assert record_id.endswith('-'+experiment_id)

            assert experiment_id == 'my_experiment_dfgsdgfdaf.aaa'
            loc = get_current_record_dir()

            _, record_dir = os.path.split(loc)

            assert record_dir == record_id
            assert os.path.isdir(loc)
            assert loc.endswith(record_id)

            with open_in_record_dir('somefile.pkl', 'wb') as f:
                pickle.dump([1, 2, 3], f, protocol=pickle.HIGHEST_PROTOCOL)

            assert os.path.exists(os.path.join(loc, 'somefile.pkl'))

            with open_in_record_dir('somefile.pkl', 'rb') as f:
                assert pickle.load(f) == [1, 2, 3]

            exp = rec.get_experiment()
            assert exp.get_id() == experiment_id
            assert exp.get_args() == rec.get_args() == OrderedDict([('a', 4)])
            assert rec.get_dir() == loc
            assert has_experiment_record(experiment_id)
            assert record_id in experiment_id_to_record_ids(experiment_id)
            return a+2
Example #2
0
        def my_experiment_dfgsdgfdaf(a=1):

            assert a == 4, 'Only meant to run aaa variant.'
            experiment_id = get_current_experiment_id()

            rec = get_current_experiment_record()

            record_id = get_current_record_id()

            assert record_id.endswith('-' + experiment_id)

            assert experiment_id == 'my_experiment_dfgsdgfdaf.aaa'
            loc = get_current_record_dir()

            _, record_dir = os.path.split(loc)

            assert record_dir == record_id
            assert os.path.isdir(loc)
            assert loc.endswith(record_id)

            with open_in_record_dir('somefile.pkl', 'wb') as f:
                pickle.dump([1, 2, 3], f, protocol=pickle.HIGHEST_PROTOCOL)

            assert os.path.exists(os.path.join(loc, 'somefile.pkl'))

            with open_in_record_dir('somefile.pkl', 'rb') as f:
                assert pickle.load(f) == [1, 2, 3]

            exp = rec.get_experiment()
            assert exp.get_id() == experiment_id
            assert exp.get_args() == rec.get_args() == OrderedDict([('a', 4)])
            assert rec.get_dir() == loc
            assert has_experiment_record(experiment_id)
            assert record_id in experiment_id_to_record_ids(experiment_id)
            return a + 2
Example #3
0
def smart_save(obj, relative_path, remove_file_after=False):
    """
    Save an object locally.  How you save it depends on its extension.
    Extensions currently supported:
        pkl: Pickle file.
        That is all.
    :param obj: Object to save
    :param relative_path: Path to save it, relative to "Data" directory.  The following placeholders can be used:
        %T - ISO time
        %R - Current Experiment Record Identifier (includes experiment time and experiment name)
    :param remove_file_after: If you're just running a test, it's good to verify that you can save, but you don't
        actually want to leave a file behind.  If that's the case, set this argument to True.
    """
    if '%T' in relative_path:
        iso_time = datetime.now().isoformat().replace(':',
                                                      '.').replace('-', '.')
        relative_path = relative_path.replace('%T', iso_time)
    if '%R' in relative_path:
        from artemis.experiments.experiment_record import get_current_experiment_id
        relative_path = relative_path.replace('%R',
                                              get_current_experiment_id())
    _, ext = os.path.splitext(relative_path)

    with smart_file(relative_path, make_dir=True) as local_path:
        print('Saved object <%s at %s> to file: "%s"' %
              (obj.__class__.__name__, hex(id(object)), local_path))
        if ext == '.pkl':
            with open(local_path, 'wb') as f:
                pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
        elif ext in _IMAGE_EXTENSIONS:
            _save_image(obj, local_path)
        elif ext == '.pdf':
            obj.savefig(local_path)
        else:
            raise Exception(
                "No method exists yet to save '%s' files.  Add it!" % (ext, ))

    if remove_file_after:
        os.remove(local_path)

    return local_path
Example #4
0
def smart_save(obj, relative_path, remove_file_after = False):
    """
    Save an object locally.  How you save it depends on its extension.
    Extensions currently supported:
        pkl: Pickle file.
        That is all.
    :param obj: Object to save
    :param relative_path: Path to save it, relative to "Data" directory.  The following placeholders can be used:
        %T - ISO time
        %R - Current Experiment Record Identifier (includes experiment time and experiment name)
    :param remove_file_after: If you're just running a test, it's good to verify that you can save, but you don't
        actually want to leave a file behind.  If that's the case, set this argument to True.
    """
    if '%T' in relative_path:
        iso_time = datetime.now().isoformat().replace(':', '.').replace('-', '.')
        relative_path = relative_path.replace('%T', iso_time)
    if '%R' in relative_path:
        from artemis.experiments.experiment_record import get_current_experiment_id
        relative_path = relative_path.replace('%R', get_current_experiment_id())
    _, ext = os.path.splitext(relative_path)

    with smart_file(relative_path, make_dir=True) as local_path:
        print('Saved object <%s at %s> to file: "%s"' % (obj.__class__.__name__, hex(id(object)), local_path))
        if ext=='.pkl':
            with open(local_path, 'wb') as f:
                pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
        elif ext in _IMAGE_EXTENSIONS:
            _save_image(obj, local_path)
        elif ext=='.pdf':
            obj.savefig(local_path)
        else:
            raise Exception("No method exists yet to save '%s' files.  Add it!" % (ext, ))

    if remove_file_after:
        os.remove(local_path)

    return local_path