|
| 1 | +.. _Parsers Example: |
| 2 | + |
| 3 | +:tocdepth: 2 |
| 4 | + |
| 5 | +Parsers Example |
| 6 | +############### |
| 7 | + |
| 8 | +This example will demonstrate how diffpy.utils lets us easily process and serialize files. |
| 9 | +Using the parsers module, we can load file data into simple and easy-to-work-with Python objects. |
| 10 | + |
| 11 | +1) To begin, unzip :download:`parserdata<./exampledata/parserdata.zip>` and take a look at ``data.txt``. |
| 12 | + Our goal will be to extract and serialize the data table as well as the parameters listed in the header of this file. |
| 13 | + |
| 14 | +2) To get the data table, we will use the ``loadData`` function. The default behavior of this |
| 15 | + function is to find and extract a data table from a file.:: |
| 16 | + |
| 17 | + from diffpy.utils.parsers import loadData |
| 18 | + data_table = loadData('<PATH to data.txt>') |
| 19 | + |
| 20 | + While this will work with most datasets, on our ``data.txt`` file, we got a ``ValueError``. The reason for this is |
| 21 | + due to the comments ``$ Phase Transition Near This Temperature Range`` and ``--> Note Significant Jump in Rw <--`` |
| 22 | + embedded within the dataset. To fix this, try using the ``comments`` parameter. :: |
| 23 | + |
| 24 | + data_table = loadData('<PATH to data.txt>', comments=['$', '-->']) |
| 25 | + |
| 26 | + This parameter tells ``loadData`` that any lines beginning with ``$`` and ``-->`` are just comments and |
| 27 | + more entries in our data table may follow. |
| 28 | + |
| 29 | + Here are a few other parameters to test out: |
| 30 | + |
| 31 | + * ``delimiter=','``: Look for a comma-separated data table. Useful for csv file types. |
| 32 | + However, since ``data.txt`` is whitespace separated, running :: |
| 33 | + |
| 34 | + loadData('<PATH to data.txt>', comments=['$', '-->'], delimiter=',') |
| 35 | + |
| 36 | + returns an empty list. |
| 37 | + * ``minrows=50``: Only look for data tables with at least 50 rows. Since our data table has much less than that many |
| 38 | + rows, running :: |
| 39 | + |
| 40 | + loadData('<PATH to data.txt>', comments=['$', '-->'], minrows=50) |
| 41 | + |
| 42 | + returns an empty list. |
| 43 | + * ``usecols=[0, 3]``: Only return the 0th and 3rd columns (zero-indexed) of the data table. For ``data.txt``, this |
| 44 | + corresponds to the temperature and rw columns. :: |
| 45 | + |
| 46 | + loadData('<PATH to data.txt>', comments=['$', '-->'], usecols=[0, 3]) |
| 47 | + |
| 48 | +3) Next, to get the header information, we can again use ``loadData``, |
| 49 | + but this time with the ``headers`` parameter enabled. :: |
| 50 | + |
| 51 | + hdata = loadData('<PATH to data.txt>', comments=['$', '-->'], headers=True) |
| 52 | + |
| 53 | +4) Rather than working with separate ``data_table`` and ``hdata`` objects, it may be easier to combine them into a single |
| 54 | +dictionary. We can do so using the ``serialize_data`` function. :: |
| 55 | + |
| 56 | + from diffpy.utils.parsers import serialize_data |
| 57 | + file_data = serialize_data('<PATH to data.txt', hdata, data_table) |
| 58 | + # File data is a dictionary with a single key |
| 59 | + # The key is the file name (in our case, 'data.txt') |
| 60 | + # The entry is a dictionary containing data from hdata and data_table |
| 61 | + data_dict = file_data['data.txt'] |
| 62 | + |
| 63 | + This dictionary ``data_dict`` contains all entries in ``hdata`` and an additional entry named |
| 64 | + ``data table`` containing ``data_table``. :: |
| 65 | + |
| 66 | + here_is_the_data_table = data_dict['data table'] |
| 67 | + |
| 68 | + There is also an option to name columns in the data table and save those columns as entries instead. :: |
| 69 | + |
| 70 | + data_table_column_names = ['temperature', 'scale', 'stretch', 'rw'] # names of the columns in data.txt |
| 71 | + file_data = serialize_data('<PATH to data.txt>', hdata, data_table, dt_colnames=data_table_column_names) |
| 72 | + data_dict = file_data['data.txt'] |
| 73 | + |
| 74 | + Now we can extract specific data table columns from the dictionary. :: |
| 75 | + |
| 76 | + data_table_temperature_column = data_dict['temperature'] |
| 77 | + data_table_rw_column = data_dict['rw'] |
| 78 | + |
| 79 | +5) When we are done working with the data, we can store it on disc for later use. This can also be done using the |
| 80 | + ``serialize_data`` function with an additional ``serial_file`` parameter.:: |
| 81 | + |
| 82 | + parsed_file_data = serialize_data('<PATH to data.txt>', hdata, data_table, serial_file='<PATH to serialfile.json>') |
| 83 | + |
| 84 | + The returned value, ``parsed_file_data``, is the dictionary we just added to ``serialfile.json``. |
| 85 | + To extract the data from the serial file, we use ``deserialize_data''. :: |
| 86 | + |
| 87 | + from diffpy.utils.parsers import deserialize_data |
| 88 | + parsed_file_data = deserialize_data('<PATH to serialdata.json>') |
| 89 | + |
| 90 | +6) Finally, ``serialize_data`` allows us to store data from multiple text file in a single serial file. For one last bit |
| 91 | + of practice, we will extract and add the data from ``moredata.txt`` into the same ``serialdata.json`` file.:: |
| 92 | + |
| 93 | + data_table = loadData('<PATH to moredata.txt>') |
| 94 | + hdata = loadData('<PATH to moredata.txt>', headers=True) |
| 95 | + serialize_data('<PATH to moredata.txt>', hdata, data_table, serial_file='<PATH to serialdata.json>') |
| 96 | + |
| 97 | + The serial file ``serialfile.json`` should now contain two entries: ``data.txt`` and ``moredata.txt``. |
| 98 | + The data from each file can be accessed using :: |
| 99 | + |
| 100 | + serial_data = deserialize_data('<PATH to serialdata.json>') |
| 101 | + data_txt_data = serial_data['data.txt'] # Access data.txt data |
| 102 | + moredata_txt_data = serial_data['moredata.txt'] # Access moredata.txt data |
| 103 | + |
| 104 | +For more information, check out the :ref:`documentation<Parsers Documentation>` of the ``parsers`` module. |
0 commit comments