Input/Output Reference
This section provides a detailed API reference for all modules related to data input, output, and framework interoperability in the datarec
library.
Core I/O Modules
These modules handle the fundamental tasks of reading, writing, and representing raw data.
RawData
Container for raw datasets in DataRec.
Wraps a pandas.DataFrame
and stores metadata about user, item, rating, and timestamp columns.
Provides lightweight methods for slicing, copying, and merging data.
Source code in datarec/io/rawdata.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
__init__(data=None, header=False, user=None, item=None, rating=None, timestamp=None)
Initialize a RawData object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
DataFrame of the dataset. Defaults to None. |
None
|
header
|
bool
|
Whether the file has a header. Defaults to False. |
False
|
user
|
str
|
Column name for user IDs. |
None
|
item
|
str
|
Column name for item IDs. |
None
|
rating
|
str
|
Column name for ratings. |
None
|
timestamp
|
str
|
Column name for timestamps. |
None
|
Source code in datarec/io/rawdata.py
append(new_data)
Append new rows to the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_data
|
DataFrame
|
DataFrame to append. |
required |
Returns:
Type | Description |
---|---|
None |
copy(deep=True)
Make a copy of the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
deep
|
bool
|
If True, return a deep copy of the dataset. |
True
|
Returns:
Type | Description |
---|---|
RawData
|
A copy of the dataset. |
__repr__()
__len__()
__getitem__(idx)
Return the item at the given index. Args: idx: index of the item to return.
Returns:
Type | Description |
---|---|
RawData
|
the sample at the given index. |
__add__(other)
Concatenate two RawData objects. Args: other (RawData): the other RawData to concatenate.
Returns:
Type | Description |
---|---|
RawData
|
the concatenated RawData object. |
Source code in datarec/io/rawdata.py
__iter__()
__check_rawdata_compatibility__(rawdata)
Check compatibility between RawData objects. Args: rawdata (RawData): RawData object to check.
Returns:
Type | Description |
---|---|
bool
|
True if compatibility is verified. |
Source code in datarec/io/rawdata.py
__check_rawdata_compatibility__(rawdata1, rawdata2)
Check compatibility between two RawData objects. Args: rawdata1 (RawData): First RawData object to check. rawdata2 (RawData): Second RawData object to check.
Returns:
Type | Description |
---|---|
bool
|
True if compatibility is verified. |
Source code in datarec/io/rawdata.py
fill_rawdata(data, user=None, item=None, rating=None, timestamp=None, path=None)
Create a RawData object from raw data and assign column names to RawData object attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
Data to create RawData object from. |
required |
user
|
str
|
Column name for user field. |
None
|
item
|
str
|
Column name for item field. |
None
|
rating
|
str
|
Column name for rating field. |
None
|
timestamp
|
str
|
Column name for timestamp field. |
None
|
path
|
str
|
Path where the original file is stored. |
None
|
Source code in datarec/io/readers.py
read_json(filepath, user_field=None, item_field=None, rating_field=None, timestamp_field=None, lines=True)
Reads a JSON file and returns it as a RawData object. Args: filepath (str): path to JSON file. user_field (str): JSON key for user field. item_field (str): JSON key for item field. rating_field (str): JSON key for rating field. timestamp_field (str): JSON key for timestamp field. lines (bool): Read the file as a JSON object per line.
Returns:
Type | Description |
---|---|
RawData
|
RawData object |
Source code in datarec/io/readers.py
read_tabular(filepath, sep, user_col=None, item_col=None, rating_col=None, timestamp_col=None, header='infer', skiprows=0)
Reads a tabular data file and returns it as a pandas DataFrame. Args: filepath (str): Path to tabular data file. sep (str): Separator to use. user_col (str): Column name for user field. item_col (str): Column name for item field. rating_col (str): Column name for rating field. timestamp_col (str): Column name for timestamp field. header (nt, Sequence of int, ‘infer’ or None): Row number(s) containing column labels and marking the start of the data (zero-indexed). Default behavior is to infer the column names. skiprows (int, list of int or Callable): Line numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file.
Returns:
Type | Description |
---|---|
RawData
|
RawData object. |
Source code in datarec/io/readers.py
read_inline(filepath, cols=None, user_col='user', item_col='item', col_sep=',', history_sep=';')
Read a CSV file and return a RawData object. Args: filepath (str): Path to CVS file. cols (list[str]): List of column names. user_col (str): Column name for user field.: item_col (str): Column name for item field. col_sep (str): Separator to use. history_sep (str): Separator for multiple items.
Returns:
Type | Description |
---|---|
RawData
|
RawData object. |
Source code in datarec/io/readers.py
read_inline_chunk(filepath, cols=None, user_col='user', item_col='item')
Read a CSV file a chunk of rows at a time and return a RawData object. Args: filepath (str): Path to CSV file. cols (list[str]): List of column names. user_col (str): Column name for user field. item_col (str): Column name for item field.
Returns:
Type | Description |
---|---|
RawData
|
RawData object. |
Source code in datarec/io/readers.py
write_tabular(rawdata, path, sep='\t', header=True, decimal='.', user=True, item=True, rating=True, timestamp=True, verbose=True)
Write a RawData dataset to a CSV/TSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rawdata
|
RawData
|
RawData instance. |
required |
path
|
str
|
Path to the CSV/TSV file. |
required |
sep
|
str
|
Separator to use. |
'\t'
|
header
|
bool or list[str]
|
Write out the column names. If a list of strings is given it is assumed to be aliases for the column names. |
True
|
decimal
|
str
|
Character recognized as decimal separator. |
'.'
|
user
|
bool
|
Whether to write the user information. If True, the user information will be written in the file. |
True
|
item
|
bool
|
Whether to write the item information. If True, the item information will be written in the file. |
True
|
rating
|
bool
|
Whether to write the rating information. If True, the rating information will be written in the file. |
True
|
timestamp
|
bool
|
Whether to write the timestamp information. If True, the timestamp information will be written in the file. |
True
|
verbose
|
bool
|
Print out additional information. |
True
|
Returns:
Type | Description |
---|---|
(CSV/TSV file) |
Source code in datarec/io/writers.py
write_json(rawdata, path, user=True, item=True, rating=True, timestamp=True)
Write a RawData dataset to a JSON file. Args: rawdata (RawData): RawData instance. path (str): Path to the JSON file. user (bool): Whether to write the user information. If True, the user information will be written in the file. item (bool): Whether to write the item information. If True, the item information will be written in the file. rating (bool): Whether to write the rating information. If True, the rating information will be written in the file. timestamp (bool): Whether to write the timestamp information. If True, the timestamp information will be written in the file.
Returns:
Type | Description |
---|---|
(JSON file) |
Source code in datarec/io/writers.py
get_cache_dir(app_name='datarec', app_author='sisinflab')
Returns the appropriate cache directory for the library, creating it if it doesn't exist. Respects the DATAREC_CACHE_DIR environment variable if set.
Returns:
Name | Type | Description |
---|---|---|
Path |
The absolute path to the cache directory. |
Source code in datarec/io/paths.py
dataset_directory(dataset_name, must_exist=False)
Given the dataset name returns the dataset directory Args: dataset_name (str): name of the dataset must_exist (bool): flag for forcing to check if the folder exists
Returns:
Type | Description |
---|---|
str
|
the path of the directory containing the dataset data |
Source code in datarec/io/paths.py
dataset_raw_directory(dataset_name)
Given the dataset name returns the directory containing the raw data of the dataset Args: dataset_name (str): name of the dataset
Returns:
Type | Description |
---|---|
str
|
the path of the directory containing the raw data of the dataset |
Source code in datarec/io/paths.py
dataset_processed_directory(dataset_name)
Given the dataset name returns the directory containing the processed data of the dataset Args: dataset_name (str): name of the dataset
Returns:
Type | Description |
---|---|
str
|
the path of the directory containing the processed data of the dataset |
Source code in datarec/io/paths.py
dataset_filepath(dataset_name)
Given the dataset name returns the path of the dataset data Args: dataset_name (str): name of the dataset
Returns:
Type | Description |
---|---|
str
|
the path of the dataset data |
Source code in datarec/io/paths.py
Framework Interoperability
This section covers the tools used to export DataRec
datasets into formats compatible with other popular recommender systems libraries.
FrameworkExporter
Exporter for converting RawData datasets to external recommender system frameworks.
Provides methods to format a RawData
object according to
the expected schema of supported libraries (e.g., Cornac, RecBole).
Source code in datarec/io/frameworks/exporter.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 |
|
__init__(output_path, user=True, item=True, rating=True, timestamp=False)
Initialize a FrameworkExporter object. Args: output_path (str): Path where to save the output file. user (bool): Whether to write the user information. If True, the user information will be written in the file. item (bool): Whether to write the item information. If True, the item information will be written in the file. rating (bool): Whether to write the rating information. If True, the rating information will be written in the file. timestamp (bool): Whether to write the timestamp information. If True, the timestamp information will be written in the file.
Source code in datarec/io/frameworks/exporter.py
to_clayrs(data)
Export to ClayRS format. Args: data (RawData): RawData object to convert to ClayRS format.
Source code in datarec/io/frameworks/exporter.py
to_cornac(data)
Export to Cornac format. Args: data (RawData): RawData object to convert to Cornac format.
Source code in datarec/io/frameworks/exporter.py
to_daisyrec(data)
Export to DaisyRec format. Args: data (RawData): RawData object to convert to DaisyRec format.
Source code in datarec/io/frameworks/exporter.py
to_lenskit(data)
Export to LensKit format. Args: data (RawData): RawData object to convert to LensKit format.
Source code in datarec/io/frameworks/exporter.py
to_recbole(data)
Export to RecBole format. Args: data (RawData): RawData object to convert to RecBole format.
Source code in datarec/io/frameworks/exporter.py
to_rechorus(train_data, test_data, val_data)
Export to Rechus format. Args: train_data (RawData): Training data as RawData object to convert to Rechus format. test_data (RawData): Test data as RawData object to convert to Rechus format. val_data (RawData): Validation data as RawData object to convert to Rechus format.
Source code in datarec/io/frameworks/exporter.py
to_recpack(data)
Export to RecPack format. Args: data (RawData): RawData object to convert to RecPack format.
Source code in datarec/io/frameworks/exporter.py
to_recommenders(data)
Export to Recommenders format. Args: data (RawData): RawData object to convert to Recommenders format.
Source code in datarec/io/frameworks/exporter.py
to_elliot(train_data, test_data, val_data)
Export to Elliot format. Args: train_data (DataRec): Training data as DataRec object to convert to Elliot format. test_data (DataRec): Test data as DataRec object to convert to Elliot format. val_data (DataRec): Validation data as DataRec object to convert to Elliot format.
Source code in datarec/io/frameworks/exporter.py
Framework
Base class for all framework exporters.
Source code in datarec/io/frameworks/manager.py
info_code()
info()
Print citation information for the framework including: paper name, DOI and bibtex citation. Print additional information such as: example code for integrating this framework with DataRec, repository URL and framework documentation URL. Returns:
Source code in datarec/io/frameworks/manager.py
ClayRS
ClayRS
Bases: Framework
ClayRS framework adapter.
Provide metadata, citation, and usage examples for ClayRS framework.
Source code in datarec/io/frameworks/clayrs/clayrs.py
__init__(timestamp, path)
Initialize ClayRS adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the ClayRS-compatible dataset is stored.
Source code in datarec/io/frameworks/clayrs/clayrs.py
info_code()
Provide the code to use in ClayRS to run experiments.
Source code in datarec/io/frameworks/clayrs/clayrs.py
Cornac
Cornac
Bases: Framework
Cornac framework adapter.
Provide metadata, citation, and usage examples for Cornac framework.
Source code in datarec/io/frameworks/cornac/cornac.py
__init__(timestamp, path)
Initialize Cornac adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the Cornac-compatible dataset is stored.
Source code in datarec/io/frameworks/cornac/cornac.py
info_code()
Provide the code to use in Cornac to run experiments.
Source code in datarec/io/frameworks/cornac/cornac.py
DaisyRec
DaisyRec
Bases: Framework
DaisyRec framework adapter.
Provide metadata, citation, and usage examples for DaisyRec framework.
Source code in datarec/io/frameworks/daisyrec/daisyrec.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
__init__(timestamp, path)
Initialize DaisyRec adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the DaisyRec-compatible dataset is stored.
Source code in datarec/io/frameworks/daisyrec/daisyrec.py
info_code()
Provide the code to use in DaisyRec to run experiments.
Source code in datarec/io/frameworks/daisyrec/daisyrec.py
load_rate(src='ml-100k', prepro='origin', binary=True, pos_threshold=None, level='ui')
Load certain raw data. Args: src (str): Name of dataset. prepro (str): Way to pre-process raw data input, expect 'origin', f'{N}core', f'{N}filter', N is integer value. binary (boolean): Whether to transform rating to binary label as CTR or not as Regression. pos_threshold (float): If not None, treat rating larger than this threshold as positive sample. level (str): which level to do with f'{N}core' or f'{N}filter' operation (it only works when prepro contains 'core' or 'filter').
Returns:
Type | Description |
---|---|
Dataframe
|
Rating information with columns: user, item, rating, (options: timestamp). |
int
|
The number of users in the dataset. |
int
|
The number of items in the dataset. |
Source code in datarec/io/frameworks/daisyrec/loader.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 |
|
get_ur(df)
Get user-rating pairs. Args: df (pd.DataFrame): Rating dataframe.
Returns:
Type | Description |
---|---|
dict
|
Dictionary which stores user-items interactions. |
Source code in datarec/io/frameworks/daisyrec/loader.py
get_ir(df)
Get item-rating pairs. Args: df (pd.DataFrame): Rating dataframe.
Returns:
Type | Description |
---|---|
dict
|
Dictionary which stores item-items interactions. |
Source code in datarec/io/frameworks/daisyrec/loader.py
build_feat_idx_dict(df, cat_cols=['user', 'item'], num_cols=[])
Encode feature mapping for FM. Args: df (pd.DataFrame): Feature dataframe. cat_cols (list): List of categorical column names. num_cols (list): List of numerical column names.
Returns:
Type | Description |
---|---|
dict
|
Dictionary with index-feature column mapping information. |
int
|
The number of features. |
Source code in datarec/io/frameworks/daisyrec/loader.py
convert_npy_mat(user_num, item_num, df)
Convert pd.Dataframe to numpy matrix. Args: user_num(int): Number of users. item_num (int): Number of items. df (pd.DataFrame): Rating dataframe.
Returns:
Type | Description |
---|---|
array
|
Rating matrix. |
Source code in datarec/io/frameworks/daisyrec/loader.py
build_candidates_set(test_ur, train_ur, item_pool, candidates_num=1000)
Build candidate items for ranking. Args: test_ur (dict): Ground truth that represents the relationship of user and item in the test set. train_ur (dict): The relationship of user and item in the train set. item_pool (list or set): Set of all items. candidates_num (int): Number of candidates.:
Returns:
Name | Type | Description |
---|---|---|
test_ucands |
dict
|
Dictionary storing candidates for each user in test set. |
Source code in datarec/io/frameworks/daisyrec/loader.py
get_adj_mat(n_users, n_items)
Get adjacency matrix. Args: n_users (int): Number of users. n_items (int): Number of items.
Returns:
Name | Type | Description |
---|---|---|
adj_mat |
csr_matrix
|
Adjacency matrix. |
norm_adj_mat |
csr_matrix
|
Normalized adjacency matrix. |
mean_adj_mat |
csr_matrix
|
Mean adjacency matrix. |
Source code in datarec/io/frameworks/daisyrec/loader.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
Elliot
Elliot
Bases: Framework
Elliot framework adapter.
Provide metadata, citation, and usage examples for Elliot framework.
Source code in datarec/io/frameworks/elliot/elliot.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
__init__(timestamp, path)
Initialize Elliot adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the Elliot-compatible dataset is stored.
Source code in datarec/io/frameworks/elliot/elliot.py
info_code()
Provide the code to use in Elliot to run experiments.
Source code in datarec/io/frameworks/elliot/elliot.py
LensKit
LensKit
Bases: Framework
LensKit framework adapter.
Provide metadata, citation, and usage examples for LensKit framework.
Source code in datarec/io/frameworks/lenskit/lenskit.py
__init__(timestamp, path)
Initialize LensKit adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the LensKit-compatible dataset is stored.
Source code in datarec/io/frameworks/lenskit/lenskit.py
info_code()
Provide the code to use in LensKit to run experiments.
Source code in datarec/io/frameworks/lenskit/lenskit.py
RecBole
RecBole
Bases: Framework
RecBole framework adapter.
Provide metadata, citation, and usage examples for RecBole framework.
Source code in datarec/io/frameworks/recbole/recbole.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 |
|
__init__(timestamp, path)
Initialize RecBole adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the RecBole-compatible dataset is stored.
Source code in datarec/io/frameworks/recbole/recbole.py
info_code()
Provide the code to use in RecBole to run experiments.
Source code in datarec/io/frameworks/recbole/recbole.py
ReChorus
ReChorus
Bases: Framework
ReChorus framework adapter.
Provide metadata, citation, and usage examples for ReChorus framework.
Source code in datarec/io/frameworks/rechorus/rechorus.py
__init__(timestamp, path)
Initialize ReChorus adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the ReChorus-compatible dataset is stored.
Source code in datarec/io/frameworks/rechorus/rechorus.py
info_code()
Provide the code to use in RecBole to run experiments.
Source code in datarec/io/frameworks/rechorus/rechorus.py
Recommenders
Recommenders
Bases: Framework
Recommenders framework adapter.
Provide metadata, citation, and usage examples for Recommenders framework.
Source code in datarec/io/frameworks/recommenders/recommenders.py
__init__(timestamp, path)
Initialize Recommenders adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the Recommenders-compatible dataset is stored.
Source code in datarec/io/frameworks/recommenders/recommenders.py
info_code()
Provide the code to use in Recommenders to run experiments.
Source code in datarec/io/frameworks/recommenders/recommenders.py
RecPack
RecPack
Bases: Framework
RecPack framework adapter.
Provide metadata, citation, and usage examples for RecPack framework.
Source code in datarec/io/frameworks/recpack/recpack.py
__init__(timestamp, path)
Initialize RecPack adapter. Args: timestamp (bool): Whether timestamps are included. path (str): Path where the RecPack-compatible dataset is stored.
Source code in datarec/io/frameworks/recpack/recpack.py
info_code()
Provide the code to use in RecPack to run experiments.
Source code in datarec/io/frameworks/recpack/recpack.py
DataRec
Bases: Dataset
Base class for DataRec Datasets
Source code in datarec/io/frameworks/recpack/datarec.py
USER_IX = 'userId'
class-attribute
instance-attribute
Name of the column in the DataFrame that contains user identifiers.
ITEM_IX = 'itemId'
class-attribute
instance-attribute
Name of the column in the DataFrame that contains item identifiers.
TIMESTAMP_IX = 'timestamp'
class-attribute
instance-attribute
Name of the column in the DataFrame that contains time of interaction in seconds since epoch.
DEFAULT_FILENAME
property
Default filename that will be used if it is not specified by the user.