Plug
This module is responsible for handling data and executing I/O within the Paramaterial library.
The central components of the plug module are the DataSet and DataItem classes
DataItem: A data structure that encapsulates a single test's information and data. ADataItemobject holds:test_id: A string identifier for the test.data: A pandas DataFrame containing the test data.info: A pandas Series containing the metadata associated with the test.
DataSet: A container class that manages a collection ofDataItemobjects. It provides various methods for managing and manipulating the data, including reading from files, writing to files, filtering, sorting, and applying custom functions.
Key Interactions between DataSet and DataItem
- Loading Data: A
DataSetis initialized by providing paths to metadata and data files. It reads the files and constructs a collection ofDataItemobjects. - Accessing Data: You can access individual
DataItemobjects within aDataSetusing index-based or test_id-based access through the__getitem__method. - Applying Functions: You can use the
applymethod in theDataSetclass to apply a custom function to eachDataItem. This enables complex data transformations and analyses. - Iterating: The
DataSetclass supports iteration over itsDataItemobjects, allowing you to loop through the data items using a standardforloop. - Writing Data: The
write_outputmethod allows you to save the information in theDataSetback to files, preserving changes made to theDataItemobjects.
Examples:
>>> # Load a DataSet from files
>>> ds = DataSet('info.xlsx', 'data')
>>> # Access a specific DataItem by test_id
>>> di = ds['T01']
>>> # Apply a custom function to all DataItems
>>> def custom_function(di: DataItem) -> DataItem:
... di.data['Stress_MPa'] *= 2
... di.info['max_stress'] = di.data['Stress_MPa'].max()
... return di
>>> ds_modified = ds.apply(custom_function)
>>> # Save the DataSet to new files
>>> ds_modified.write_output('new_info.xlsx', 'new_data')
DataItem
dataclass
A storage class for data and metadata related to a single test.
Attributes:
| Name | Type | Description |
|---|---|---|
test_id |
str
|
The unique identifier for the test. |
data |
pd.DataFrame
|
A pandas DataFrame containing the data related to the test. |
info |
pd.Series
|
A pandas Series containing metadata related to the test. |
Source code in paramaterial\plug.py
DataSet
A class for handling data, loading from files, and performing various operations.
The DataSet class provides functionality for loading data from specified files, manipulating the data, and writing output. It contains a collection of DataItem objects, each representing a single test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info_path |
Optional[str]
|
The path to the info table file containing metadata. |
None
|
data_dir |
Optional[str]
|
The directory containing the data files. |
None
|
test_id_key |
str
|
The column name in the info table that contains the test IDs. |
'test_id'
|
Examples:
>>> ds = DataSet(info_path='info/01_prepared_info.xlsx', data_dir='data/01_prepared_data')
>>> len(ds)
10
Raises:
| Type | Description |
|---|---|
ValueError
|
If only one of info_path and data_dir is specified. |
FileNotFoundError
|
If a file is not found for a given test_id. |
Source code in paramaterial\plug.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
__getitem__(specifier)
Get a DataItem or a list of DataItems by index, test_id, or slice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
specifier |
Union[int, str, slice]
|
An int for index-based access, a str for test_id-based access, or a slice for slicing. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[DataItem], DataItem]
|
A DataItem or a list of DataItems depending on the specifier. |
Examples:
>>> di = ds[0] # Get the first DataItem
>>> di = ds['T01'] # Get the DataItem with test_id='T01'
>>> dis = ds[0:5] # Get the first five DataItems
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid specifier type is provided. |
Source code in paramaterial\plug.py
__iter__()
Iterate over the DataItems in the DataSet.
Yields:
| Name | Type | Description |
|---|---|---|
DataItem | The next DataItem in the DataSet. |
Examples:
Source code in paramaterial\plug.py
apply(func, **kwargs)
Apply a function to each DataItem in the DataSet and return a new DataSet with the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
Callable[[DataItem, Dict], DataItem]
|
The function to apply to each DataItem. It must take a DataItem and optional keyword arguments and |
required |
**kwargs |
Additional keyword arguments to pass to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
DataSet
|
A new DataSet containing the DataItems after applying the function. |
Examples:
>>> def double_stress(di: DataItem) -> DataItem:
... di.data['Stress_MPa'] *= 2
... return di
>>> ds_doubled = ds.apply(double_stress)
Source code in paramaterial\plug.py
copy()
Create a copy of the DataSet.
Returns:
| Type | Description |
|---|---|
DataSet
|
A copy of the DataSet. |
Examples:
Source code in paramaterial\plug.py
sort_by(column, ascending=True)
Sort a copy of the DataSet by a column in the info table and return the copy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column |
Union[str, List[str]]
|
Column or list of columns to sort by. |
required |
ascending |
bool
|
Whether to sort in ascending order. (Default: True) |
True
|
Returns:
| Type | Description |
|---|---|
DataSet
|
A new DataSet sorted by the specified column(s). |
Examples:
>>> ds = DataSet(info_path='info/prepared_info.xlsx', data_dir='data/prepared_data')
>>> ds_sorted = ds.sort_by('temperature')
Source code in paramaterial\plug.py
subset(filter_dict)
Create a subset of the DataSet based on specified filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filter_dict |
Dict[str, Union[str, List[Any]]]
|
A dictionary containing column names as keys and values or list of values to filter by. |
required |
Returns:
| Type | Description |
|---|---|
DataSet
|
A new DataSet containing only the filtered DataItems. |
Examples:
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid filter key is provided. |
Source code in paramaterial\plug.py
write_output(info_path, data_dir)
Write the DataSet to files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info_path |
str
|
The path to the info table file to be written. |
required |
data_dir |
str
|
The directory to write the data files to. |
required |
Examples:
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the data_dir does not exist. |