|
2 | 2 | ABR Analyze
|
3 | 3 | ***********
|
4 | 4 |
|
5 |
| -ABR_Analyze: A repository for saving, processing, and plotting data from an hdf5 database. |
| 5 | +ABR_Analyze: A repository with helper functions wrapping the hdf5 package. Allows for simplified saving, loading, and searching of data. |
6 | 6 |
|
7 | 7 | Installation
|
8 | 8 | ============
|
| 9 | +To install abr_analyze simply run:: |
9 | 10 |
|
10 |
| -The ABR_Analyze repo depends on NumPy, SciPy, Matplotlib, and H5Py, and we recommend that |
11 |
| -you install these libraries before ABR_Analyze. If you're not sure how to do this, |
12 |
| -we recommend using `Anaconda <https://store.continuum.io/cshop/anaconda/>`_. |
13 |
| -Note that installing in a clean environment will require compiling of the |
14 |
| -dependent libraries, and will take a few minutes. |
| 11 | + pip install -e . |
15 | 12 |
|
16 |
| -ABR_Analyze is tested to work on Python 3.4+. |
| 13 | +Features that visualize robotic arms require an install of abr_control:: |
17 | 14 |
|
| 15 | + git clone https://github.com/abr/abr_control.git |
| 16 | + |
| 17 | + cd abr_control |
| 18 | + |
| 19 | + pip install -e . |
18 | 20 |
|
19 | 21 | Setting paths
|
20 | 22 | =============
|
| 23 | +HFF5 databases can be thought of as a compressed file (.h5) that saves dictionaries. Keys, including keys for nested dictionaries, are treated as folders with the corresponding values being the data entries. |
| 24 | + |
| 25 | +By default, abr_analyze will create a ``data/databases`` folder from the run directory to store databases. This directory can alternatively be set on instantation of a ``DataHandler`` with the ``<database_dir>`` parameter. |
| 26 | + |
| 27 | +To set the default value to avoid passing a folder in on init, update ``abr_analyze/paths.txt`` to have the full path to your save directory following the ``<database_dir:>`` entry. |
| 28 | + |
| 29 | +Manually saving and loading |
| 30 | +===== |
| 31 | +1. Instantiate a database:: |
| 32 | + |
| 33 | + from abr_analyze import DataHandler |
| 34 | + |
| 35 | + database = DataHandler('database_name') |
21 | 36 |
|
22 |
| -See abr_analyze/paths.py and adjust your default locations for |
23 |
| -databases, figures, and cached items |
| 37 | +2. Save Results:: |
24 | 38 |
|
25 |
| -A default paths.py is committed, to avoid running into conflicts with |
26 |
| -you personal setup, run:: |
| 39 | + my_results = {'key1': 3, 'key2': [3, 4, 5], 'nested_dict': {'a': 3, 'b': 6}} |
| 40 | + database.save( |
| 41 | + save_location='experiment1', |
| 42 | + data=my_results |
| 43 | + } |
27 | 44 |
|
28 |
| - git update-index --assume-unchanged <path to paths.py> |
| 45 | +3. Load Data:: |
29 | 46 |
|
30 |
| -this will assume the file is unchanged and will not commit it. |
| 47 | + subset_loaded_data = database.load('experiment1', parameters=['nested_dict']) |
| 48 | + # or load all keys |
| 49 | + all_loaded_data = database.load('experiment1') |
31 | 50 |
|
32 |
| -Usage |
| 51 | +Automated Saving with data_logger |
33 | 52 | =====
|
| 53 | +Alternatively, abr_analyze can automatically save and link results to the parameters that generated them, provided a dictionary of parameters, a dictionary of results, and a unique name for the script that generated the results with the data_logger.searchable_save() function:: |
| 54 | + |
| 55 | + from abr_analyze import data_logger |
| 56 | + |
| 57 | + # Instantiate database to save results |
| 58 | + db_name = "searchable_results_example" |
| 59 | + dat = DataHandler(db_name=db_name) |
| 60 | + |
| 61 | + # generate baseline json |
| 62 | + params = { |
| 63 | + "sin_params": { |
| 64 | + "A": 3, |
| 65 | + "shift": 5, |
| 66 | + }, |
| 67 | + "time": [0, 5, 100], |
| 68 | + "exp": 2, |
| 69 | + } |
| 70 | + |
| 71 | + # if loading from json |
| 72 | + # with open(json_fp) as fp: |
| 73 | + # params = json.load(fp) |
| 74 | + |
| 75 | + # example function that generates results |
| 76 | + # Needs to accept params dict as input and return dictionary of results |
| 77 | + def example_results(params): |
| 78 | + t = np.linspace(params["time"][0], params["time"][1], params["time"][2]) |
| 79 | + y = ( |
| 80 | + params["sin_params"]["A"] |
| 81 | + * np.sin(t - params["sin_params"]["shift"]) ** params["exp"] |
| 82 | + ) |
| 83 | + return {"t": t, "y": y} |
| 84 | + |
| 85 | + # unique name for script that generates results |
| 86 | + # should update name if something changes in the script that would affect results |
| 87 | + script_name = "example_script" |
| 88 | + |
| 89 | + # get results |
| 90 | + print("--Getting results for baseline parameters--") |
| 91 | + results = example_results(params) |
| 92 | + |
| 93 | + # save in searchable format |
| 94 | + print("--Saving baseline results--") |
| 95 | + data_logger.searchable_save(dat=dat, results=results, params=params, script_name=script_name) |
| 96 | + |
| 97 | +Running Parameter Variations |
| 98 | +===== |
| 99 | +The data_logger also has a helper function for quickly generating permutations of a parameter set. These can be saved in the same searchable manner to allow for a quick comparison of results:: |
| 100 | + |
| 101 | + # helper function to quickly create some variations of our parameter set |
| 102 | + print("--Generating parameter variations--") |
| 103 | + param_variations = data_logger.gen_parameter_variations( |
| 104 | + params=params, variation_dict={"sin_params/A": [5, 7, 10], "exp": [3, 4]} |
| 105 | + ) |
| 106 | + |
| 107 | + # get results for each variation and save |
| 108 | + print("--Getting results for parameter variations--") |
| 109 | + for hash_id, varied_params in param_variations.items(): |
| 110 | + print(f"\nGetting results for {hash_id}") |
| 111 | + # pretty printing of nested dictionaries |
| 112 | + data_logger.print_nested(varied_params, indent=0, return_val=False) |
| 113 | + |
| 114 | + results = example_results(varied_params) |
| 115 | + print("Saving results") |
| 116 | + data_logger.searchable_save( |
| 117 | + dat=dat, results=results, params=varied_params, script_name=script_name |
| 118 | + ) |
| 119 | + |
| 120 | +Searching and Plotting Results |
| 121 | +===== |
| 122 | +Once some results and their corresponding parameter sets have been saved using data_logger.searchable_save(), the results can be searched for experiments that match a subset of parameters:: |
| 123 | + |
| 124 | + # now load all results that have these parameter values |
| 125 | + const_params = { |
| 126 | + "exp": 3, |
| 127 | + } |
| 128 | + # result keys to load |
| 129 | + result_keys = ["y"] |
| 130 | + |
| 131 | + # Load results that have a set of common parameters |
| 132 | + print(f"Loading results with parameters:\n{const_params}") |
| 133 | + results = data_logger.load_results( |
| 134 | + script_name=script_name, |
| 135 | + const_params=const_params, |
| 136 | + saved_exp_hashes=None, |
| 137 | + result_keys=result_keys, |
| 138 | + dat=dat, |
| 139 | + ignore_keys=None, |
| 140 | + ) |
| 141 | + |
| 142 | + # plot the results |
| 143 | + plt.figure() |
| 144 | + ax = plt.subplot(111) |
| 145 | + for hash_name in results: |
| 146 | + # ignore const and variable params keys |
| 147 | + if "params" in hash_name: |
| 148 | + continue |
| 149 | + # print(dict_nested2str(results[hash_name])) |
| 150 | + ax.plot(results[hash_name]["results"]["y"], label=results[hash_name]["name"]) |
| 151 | + |
| 152 | + # print the values that are constant between all tests |
| 153 | + ax.text( |
| 154 | + 0, |
| 155 | + -5, |
| 156 | + ( |
| 157 | + "Constant Parameters\n" |
| 158 | + + "___________________\n" |
| 159 | + + data_logger.dict_nested2str(results["const_params"]) |
| 160 | + ), |
| 161 | + fontsize=8, |
| 162 | + ) |
| 163 | + plt.subplots_adjust(right=0.6) |
| 164 | + |
| 165 | + plt.legend() |
| 166 | + plt.tight_layout() |
| 167 | + plt.show() |
| 168 | + |
| 169 | +.. image:: docs/searchable_save_plot.png |
34 | 170 |
|
35 |
| -To understand how to use this code, check out the code in the examples folder. |
| 171 | +See the examples folder for examples covering the full range of functionality of the repository, including: plotting grids of figures, coverting figures to gifs, replaying arm trajectories in 3d plot gifs, and many more. |
0 commit comments