|
1 | 1 | from pathlib import Path
|
2 | 2 | from concurrent.futures import ThreadPoolExecutor
|
3 | 3 | import asyncio
|
4 |
| -from typing import Dict, Union |
| 4 | +from servicex.utils import ServiceXException |
| 5 | +from typing import Dict, Optional, Union |
5 | 6 |
|
6 |
| -_conversion_pool = ThreadPoolExecutor(2) |
| 7 | +from awkward.array.chunked import ChunkedArray |
7 | 8 |
|
| 9 | +_conversion_pool = ThreadPoolExecutor(4) |
8 | 10 |
|
9 |
| -async def _convert_root_to_pandas(file: Path): |
| 11 | + |
| 12 | +class DataConverterAdaptor: |
| 13 | + '''Methods to convert from one type of data to the other. |
10 | 14 | '''
|
11 |
| - Convert the contents of a ROOT file to pandas. |
| 15 | + def __init__(self, default_file_type: str): |
| 16 | + '''Create a data converter adaptor. By default it will do the |
| 17 | + conversation as requested. |
12 | 18 |
|
13 |
| - Arguments: |
| 19 | + Args: |
| 20 | + default_file_type (str): The default file type (`parquet` or `root`) |
| 21 | + ''' |
| 22 | + self._default_file_type = default_file_type |
14 | 23 |
|
15 |
| - file A `Path` to the file containing the pandas data |
| 24 | + async def convert_to_pandas(self, file: Path, file_type: Optional[str] = None): |
| 25 | + '''Convert to a pandas dataframe from data stored in a file of a particular file_type |
16 | 26 |
|
17 |
| - Returns: |
| 27 | + Args: |
| 28 | + file (Path): Path to the file |
| 29 | + file_type (str): What the file contains (root, parquet, etc) |
| 30 | + ''' |
| 31 | + file_type = file_type if file_type is not None else self._default_file_type |
| 32 | + if file_type == 'root': |
| 33 | + return await self._convert_root_to_pandas(file) |
| 34 | + elif file_type == 'parquet': |
| 35 | + return await self._convert_parquet_to_pandas(file) |
| 36 | + else: |
| 37 | + raise ServiceXException(f'Conversion from {file_type} into an pandas DF is not ' |
| 38 | + 'yet supported') |
18 | 39 |
|
19 |
| - DataFrame A pandas dataframe |
| 40 | + async def convert_to_awkward(self, file: Path, file_type: Optional[str] = None): |
| 41 | + '''Convert to an awkward data array from data stored in a file of a particular file_type |
20 | 42 |
|
21 |
| - Note: |
| 43 | + Args: |
| 44 | + file (Path): Path to the file |
| 45 | + file_type (str): What the file contains (root, parquet, etc) |
| 46 | + ''' |
| 47 | + file_type = file_type if file_type is not None else self._default_file_type |
| 48 | + if file_type == 'root': |
| 49 | + return await self._convert_root_to_awkward(file) |
| 50 | + elif file_type == 'parquet': |
| 51 | + return await self._convert_parquet_to_awkward(file) |
| 52 | + else: |
| 53 | + raise ServiceXException(f'Conversion from {file_type} into an awkward array is not ' |
| 54 | + 'yet supported') |
22 | 55 |
|
23 |
| - - Work is done on a second thread. |
24 |
| - - Pandas is only imported if this is called. |
| 56 | + async def _convert_root_to_pandas(self, file: Path): |
| 57 | + ''' |
| 58 | + Convert the contents of a ROOT file to pandas. |
25 | 59 |
|
26 |
| - ''' |
27 |
| - from pandas import DataFrame |
| 60 | + Arguments: |
28 | 61 |
|
29 |
| - def do_the_work(file: Path) -> DataFrame: |
30 |
| - import uproot |
| 62 | + file A `Path` to the file containing the pandas data |
31 | 63 |
|
32 |
| - f_in = uproot.open(file) |
33 |
| - try: |
34 |
| - r = f_in[f_in.keys()[0]] |
35 |
| - return r.pandas.df() # type: ignore |
36 |
| - finally: |
37 |
| - f_in._context.source.close() |
| 64 | + Returns: |
38 | 65 |
|
39 |
| - return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
| 66 | + DataFrame A pandas dataframe |
40 | 67 |
|
| 68 | + Note: |
41 | 69 |
|
42 |
| -async def _convert_root_to_awkward(file: Path): |
43 |
| - ''' |
44 |
| - Convert the contents of a ROOT file to an awkward dictionary. |
| 70 | + - Work is done on a second thread. |
| 71 | + - Pandas is only imported if this is called. |
45 | 72 |
|
46 |
| - Arguments: |
| 73 | + ''' |
| 74 | + from pandas import DataFrame |
47 | 75 |
|
48 |
| - file A `Path` to the file containing the pandas data |
| 76 | + def do_the_work(file: Path) -> DataFrame: |
| 77 | + import uproot |
49 | 78 |
|
50 |
| - Returns: |
| 79 | + f_in = uproot.open(file) |
| 80 | + try: |
| 81 | + r = f_in[f_in.keys()[0]] |
| 82 | + return r.pandas.df() # type: ignore |
| 83 | + finally: |
| 84 | + f_in._context.source.close() |
51 | 85 |
|
52 |
| - DataFrame A pandas dataframe |
| 86 | + return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
53 | 87 |
|
54 |
| - Note: |
| 88 | + async def _convert_parquet_to_pandas(self, file: Path): |
| 89 | + ''' |
| 90 | + Convert the contents of a parquet file to pandas. |
55 | 91 |
|
56 |
| - - Work is done on a second thread. |
57 |
| - - Pandas is only imported if this is called. |
| 92 | + Arguments: |
58 | 93 |
|
59 |
| - ''' |
60 |
| - from numpy import ndarray |
61 |
| - from awkward import JaggedArray |
| 94 | + file A `Path` to the file containing the pandas data |
| 95 | +
|
| 96 | + Returns: |
| 97 | +
|
| 98 | + DataFrame A pandas dataframe |
| 99 | +
|
| 100 | + Note: |
| 101 | +
|
| 102 | + - Work is done on a second thread. |
| 103 | + - Pandas is only imported if this is called. |
| 104 | +
|
| 105 | + ''' |
| 106 | + import pandas as pd |
| 107 | + |
| 108 | + def do_the_work(file: Path) -> pd.DataFrame: |
| 109 | + return pd.read_parquet(str(file)) |
| 110 | + |
| 111 | + return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
| 112 | + |
| 113 | + async def _convert_root_to_awkward(self, file: Path): |
| 114 | + ''' |
| 115 | + Convert the contents of a ROOT file to an awkward dictionary. |
| 116 | +
|
| 117 | + Arguments: |
| 118 | +
|
| 119 | + file A `Path` to the file containing the pandas data |
| 120 | +
|
| 121 | + Returns: |
| 122 | +
|
| 123 | + DataFrame A pandas dataframe |
| 124 | +
|
| 125 | + Note: |
| 126 | +
|
| 127 | + - Work is done on a second thread. |
| 128 | + - Pandas is only imported if this is called. |
| 129 | +
|
| 130 | + ''' |
| 131 | + from numpy import ndarray |
| 132 | + from awkward import JaggedArray |
| 133 | + |
| 134 | + def do_the_work(file: Path) -> Dict[Union[str, bytes], Union[ndarray, JaggedArray]]: |
| 135 | + import uproot |
| 136 | + |
| 137 | + f_in = uproot.open(file) |
| 138 | + try: |
| 139 | + r = f_in[f_in.keys()[0]] |
| 140 | + return r.lazyarrays() # type: ignore |
| 141 | + finally: |
| 142 | + f_in._context.source.close() |
| 143 | + |
| 144 | + return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
| 145 | + |
| 146 | + async def _convert_parquet_to_awkward(self, file: Path): |
| 147 | + ''' |
| 148 | + Convert the contents of a parquet file to an awkward dictionary. |
| 149 | +
|
| 150 | + Arguments: |
| 151 | +
|
| 152 | + file A `Path` to the file containing the pandas data |
| 153 | +
|
| 154 | + Returns: |
| 155 | +
|
| 156 | + DataFrame A pandas dataframe |
| 157 | +
|
| 158 | + Note: |
| 159 | +
|
| 160 | + - Work is done on a second thread. |
| 161 | + - Pandas is only imported if this is called. |
62 | 162 |
|
63 |
| - def do_the_work(file: Path) -> Dict[bytes, Union[ndarray, JaggedArray]]: |
64 |
| - import uproot |
| 163 | + ''' |
| 164 | + import awkward as ak |
65 | 165 |
|
66 |
| - f_in = uproot.open(file) |
67 |
| - try: |
68 |
| - r = f_in[f_in.keys()[0]] |
69 |
| - return r.lazyarrays() # type: ignore |
70 |
| - finally: |
71 |
| - f_in._context.source.close() |
| 166 | + def do_the_work(file: Path) -> \ |
| 167 | + Union[Dict[Union[str, bytes], ak.ChunkedArray], ChunkedArray]: |
| 168 | + # TODO: When we move to awkward1, make sure this becomes lazy |
| 169 | + return ak.fromparquet(str(file)) |
72 | 170 |
|
73 |
| - return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
| 171 | + return await asyncio.wrap_future(_conversion_pool.submit(do_the_work, file)) |
0 commit comments