pdstools.pega_io.File ===================== .. py:module:: pdstools.pega_io.File Functions --------- .. autoapisummary:: pdstools.pega_io.File.read_ds_export pdstools.pega_io.File.import_file pdstools.pega_io.File.read_zipped_file pdstools.pega_io.File.read_multi_zip pdstools.pega_io.File.get_latest_file pdstools.pega_io.File.find_files pdstools.pega_io.File.cache_to_file pdstools.pega_io.File.read_dataflow_output Module Contents --------------- .. py:function:: read_ds_export(filename: Union[str, io.BytesIO], path: Union[str, os.PathLike] = '.', verbose: bool = False, **reading_opts) -> Optional[polars.LazyFrame] Read in most out of the box Pega dataset export formats Accepts one of the following formats: - .csv - .json - .zip (zipped json or CSV) - .feather - .ipc - .parquet It automatically infers the default file names for both model data as well as predictor data. If you supply either 'modelData' or 'predictorData' as the 'file' argument, it will search for them. If you supply the full name of the file in the 'path' directory, it will import that instead. Since pdstools V3.x, returns a Polars LazyFrame. Simply call `.collect()` to get an eager frame. :param filename: Can be one of the following: - A string with the full path to the file - A string with the name of the file (to be searched in the given path) - A BytesIO object containing the file data (e.g., from an uploaded file in a webapp) :type filename: Union[str, BytesIO] :param path: The location of the file :type path: str, default = '.' :param verbose: Whether to print out which file will be imported :type verbose: bool, default = True :keyword Any: Any arguments to plug into the scan_* function from Polars. :returns: * *pl.LazyFrame* -- The (lazy) dataframe * *Examples* -- >>> df = read_ds_export(filename='full/path/to/ModelSnapshot.json') >>> df = read_ds_export(filename='ModelSnapshot.json', path='data/ADMData') >>> df = read_ds_export(filename=uploaded_file) # Where uploaded_file is a BytesIO object .. py:function:: import_file(file: Union[str, io.BytesIO], extension: str, **reading_opts) -> polars.LazyFrame Imports a file using Polars :param File: The path to the file, passed directly to the read functions :type File: str :param extension: The extension of the file, used to determine which function to use :type extension: str :returns: The (imported) lazy dataframe :rtype: pl.LazyFrame .. py:function:: read_zipped_file(file: Union[str, io.BytesIO], verbose: bool = False) -> Tuple[io.BytesIO, str] Read a zipped NDJSON file. Reads a dataset export file as exported and downloaded from Pega. The export file is formatted as a zipped multi-line JSON file. It reads the file, and then returns the file as a BytesIO object. :param file: The full path to the file :type file: str :param verbose: Whether to print the names of the files within the unzipped file for debugging purposes :type verbose: str, default=False :returns: The raw bytes object to pass through to Polars :rtype: os.BytesIO .. py:function:: read_multi_zip(files: Iterable[str], zip_type: Literal['gzip'] = 'gzip', add_original_file_name: bool = False, verbose: bool = True) -> polars.LazyFrame Reads multiple zipped ndjson files, and concats them to one Polars dataframe. :param files: The list of files to concat :type files: list :param zip_type: At this point, only 'gzip' is supported :type zip_type: Literal['gzip'] :param verbose: Whether to print out the progress of the import :type verbose: bool, default = True .. py:function:: get_latest_file(path: Union[str, os.PathLike], target: str, verbose: bool = False) -> str Convenience method to find the latest model snapshot. It has a set of default names to search for and finds all files who match it. Once it finds all matching files in the directory, it chooses the most recent one. Supports [".json", ".csv", ".zip", ".parquet", ".feather", ".ipc"]. Needs a path to the directory and a target of either 'modelData' or 'predictorData'. :param path: The filepath where the data is stored :type path: str :param target: Whether to look for data about the predictive models ('model_data') or the predictor bins ('model_data') :type target: str in ['model_data', 'model_data'] :param verbose: Whether to print all found files before comparing name criteria for debugging purposes :type verbose: bool, default = False :returns: The most recent file given the file name criteria. :rtype: str .. py:function:: find_files(files_dir, target) .. py:function:: cache_to_file(df: Union[polars.DataFrame, polars.LazyFrame], path: Union[str, os.PathLike], name: str, cache_type: Literal['parquet'] = 'parquet', compression: polars._typing.ParquetCompression = 'uncompressed') -> pathlib.Path cache_to_file(df: Union[polars.DataFrame, polars.LazyFrame], path: Union[str, os.PathLike], name: str, cache_type: Literal['ipc'] = 'ipc', compression: polars._typing.IpcCompression = 'uncompressed') -> pathlib.Path Very simple convenience function to cache data. Caches in arrow format for very fast reading. :param df: The dataframe to cache :type df: pl.DataFrame :param path: The location to cache the data :type path: os.PathLike :param name: The name to give to the file :type name: str :param cache_type: The type of file to export. Default is IPC, also supports parquet :type cache_type: str :param compression: The compression to apply, default is uncompressed :type compression: str :returns: The filepath to the cached file :rtype: os.PathLike .. py:function:: read_dataflow_output(files: Union[Iterable[str], str], cache_file_name: Optional[str] = None, *, extension: Literal['json'] = 'json', compression: Literal['gzip'] = 'gzip', cache_directory: Union[str, os.PathLike] = 'cache') Reads the file output of a dataflow run. By default, the Prediction Studio data export also uses dataflows, thus this function can be used for those use cases as well. Because dataflows have good resiliancy, they can produce a great number of files. By default, every few seconds each dataflow node writes a file for each partition. While this helps the system stay healthy, it is a bit more difficult to consume. This function can take in a list of files (or a glob pattern), and read in all of the files. If `cache_file_name` is specified, this function caches the data it read before as a `parquet` file. This not only reduces the file size, it is also very fast. When this function is run and there is a pre-existing parquet file with the name specified in `cache_file_name`, it will read all of the files that weren't read in before and add it to the parquet file. If no new files are found, it simply returns the contents of that parquet file - significantly speeding up operations. In a future version, the functionality of this function will be extended to also read from S3 or other remote file systems directly using the same caching method. :param files: An iterable (list or a glob) of file strings to read. If a string is provided, we call glob() on it to find all files corresponding :type files: Union[str, Iterable[str]] :param cache_file_name: If given, caches the files to a file with the given name. If None, does not use the cache at all :type cache_file_name: str, Optional :param extension: The extension of the files, by default "json" :type extension: Literal["json"] :param compression: The compression of the files, by default "gzip" :type compression: Literal["gzip"] :param cache_directory: The file path to cache the previously read files :type cache_directory: os.PathLike :param Usage: :param -----: :param >>> from glob import glob: :param >>> read_dataflow_output(files=glob("model_snapshots_*.json")):