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