cursus.steps.scripts.dummy_data_loading¶
Dummy Data Loading Processing Script
This script processes user-provided data instead of calling internal Cradle services. It serves as a drop-in replacement for CradleDataLoadingStep by reading data from an input channel, generating schema signatures and metadata, and outputting the processed data in the same format as the original Cradle data loading step.
- optimize_dtypes(df, log_func=None)[source]¶
Optimize DataFrame dtypes to reduce memory usage.
Applies the following optimizations: - Downcast numeric types (int64->int32, float64->float32) - Convert object columns with low cardinality to category
- Parameters:
df (DataFrame) – Input DataFrame
log_func (Callable | None) – Optional logging function
- Returns:
DataFrame with optimized dtypes
- Return type:
DataFrame
- detect_file_format(file_path)[source]¶
Detect the format of a data file based on extension and content.
- Parameters:
file_path (Path) – Path to the data file
- Returns:
‘csv’, ‘parquet’, ‘json’, or ‘unknown’
- Return type:
String indicating the format
- read_data_file(file_path, file_format)[source]¶
Read a data file based on its format.
- Parameters:
- Returns:
DataFrame containing the data
- Raises:
ValueError – If the format is unsupported
Exception – If reading fails
- Return type:
DataFrame
- generate_schema_signature(df)[source]¶
Generate a schema signature from a DataFrame.
The schema signature is just a list of column names from the input data.
- generate_mods_metadata(df)[source]¶
Generate MODS-compatible CSV metadata from a DataFrame.
MODS metadata format is a simple CSV with 3 columns: - varname: Column name - iscategory: “true” if string/object/category type, “false” otherwise - datatype: pandas dtype as string
This lightweight format can be generated from the first batch only, enabling true streaming mode without needing the full DataFrame.
- combine_files(data_files, max_workers=None, batch_size=10, streaming_batch_size=None)[source]¶
Combine multiple data files using parallel processing and optional streaming.
Uses parallel file reading and batch concatenation for improved performance. Memory-efficient approach with optional streaming mode.
Streaming Mode: When streaming_batch_size is set, processes files in batches to avoid loading all DataFrames into memory simultaneously. This is the most memory-efficient mode.
- Parameters:
max_workers (int | None) – Maximum number of parallel workers (default: cpu_count)
batch_size (int) – Number of DataFrames to concatenate at once (default: 10)
streaming_batch_size (int | None) – Number of files to process per batch (enables streaming mode) - If None: Loads all files into memory (original behavior) - If set: Processes files in batches, concatenating incrementally - Recommended: 10-20 files per batch for memory-constrained environments
- Returns:
Combined DataFrame from all files
- Return type:
DataFrame
- process_data_files(data_files)[source]¶
DEPRECATED: Legacy function for backward compatibility. Use combine_files() instead for better performance and memory efficiency.
Process multiple data files and combine them into a single DataFrame.
- write_signature_file(signature, output_dir)[source]¶
Write the signature file to the output directory in CSV format.
The signature file contains column names separated by commas, matching the format expected by tabular_preprocessing script.
- write_metadata_file(metadata, output_dir, format='JSON')[source]¶
Write the metadata file to the output directory in specified format.
Supports two formats: - JSON: Detailed metadata with statistics (requires Dict input) - MODS: Simple CSV with 3 columns (requires List[List[str]] input)
- Parameters:
- Returns:
Path to the written metadata file
- Raises:
ValueError – If format is unsupported or metadata type doesn’t match format
- Return type:
- write_single_shard(df, output_dir, shard_index, output_format)[source]¶
Write a single data shard in the specified format.
- Parameters:
- Returns:
Path to the written shard file
- Raises:
ValueError – If the format is unsupported
Exception – If writing fails
- Return type:
- write_data_shards(df, output_dir, shard_size, output_format)[source]¶
Write DataFrame as multiple data shards.
- write_single_data_file(df, output_dir, output_format)[source]¶
Write DataFrame as a single data file.
- Parameters:
- Returns:
Path to the written data file
- Raises:
ValueError – If the format is unsupported
Exception – If writing fails
- Return type:
- write_data_output(df, output_dir, write_shards=False, shard_size=10000, output_format='CSV')[source]¶
Write data output - either as shards or single file based on configuration.
- Parameters:
- Returns:
Path to single data file or list of shard file paths
- Return type:
- process_first_batch_for_metadata(first_batch_files, metadata_format, max_workers, batch_size)[source]¶
Process first batch of files to generate signature and metadata.
Extracts signature and metadata from the first batch only, enabling streaming mode to proceed without loading the full dataset.
- write_batch_as_shards(df, output_dir, shard_counter, shard_size, output_format)[source]¶
Write DataFrame batch as shards with continuous numbering.
- Parameters:
- Returns:
Tuple of (written_shard_paths, updated_counter)
- Return type:
- process_remaining_batches(remaining_files, data_output_dir, shard_counter, streaming_batch_size, shard_size, output_format, max_workers, batch_size)[source]¶
Stream and write remaining file batches.
Processes remaining files in batches, writing shards incrementally without loading the full dataset into memory.
- Parameters:
remaining_files (List[Path]) – List of remaining file paths to process
data_output_dir (Path) – Output directory for data shards
shard_counter (int) – Starting shard index number
streaming_batch_size (int) – Number of files per batch
shard_size (int) – Rows per shard
output_format (str) – Output data format
max_workers (int | None) – Number of parallel workers
batch_size (int) – DataFrame concat batch size
- Returns:
Tuple of (all_written_shards, total_rows_processed, final_counter)
- Return type:
- process_streaming_mode(data_files, signature_output_dir, metadata_output_dir, data_output_dir, metadata_format, streaming_batch_size, shard_size, output_format, max_workers, batch_size)[source]¶
True streaming mode: Never loads full DataFrame into memory.
Process data files in batches, generating outputs incrementally: 1. First batch → signature & metadata (from first batch only) 2. All batches → write shards incrementally 3. Free memory after each batch
Memory usage: ~1-2GB per batch (not dependent on total data size) Scales to: ANY data size (10GB, 100GB, 1TB+)
- Parameters:
signature_output_dir (Path) – Directory for signature output
metadata_output_dir (Path) – Directory for metadata output
data_output_dir (Path) – Directory for data shard output
metadata_format (str) – “JSON” or “MODS”
streaming_batch_size (int) – Number of files per batch
shard_size (int) – Rows per output shard
output_format (str) – Output data format
max_workers (int | None) – Number of parallel workers
batch_size (int) – DataFrame concat batch size
- Returns:
Dictionary of output file paths
- Return type: