Time Series Data Generation with Mimesis

Time Series Data Generation with Mimesis

Time-based data lives at the heart of modern applications. Creating realistic temporal patterns requires more than random numbers; it demands an understanding of daily rhythms, weekly cycles, and seasonal variations. Mimesis offers an elegant solution to this challenge, allowing developers to generate temporal datasets that capture the natural ebb and flow of time-based phenomena.

In this guide, we’ll explore three essential patterns: daily metrics that mirror natural usage patterns, hourly variations that reflect human activity cycles, and seasonal trends that capture long-term business dynamics. We’ll show how Mimesis brings these patterns to life with clean, reproducible code.

Basic Daily Metrics Generation

Let’s start with generating basic daily metrics that might represent a web application’s usage statistics. We’ll create a dataset that includes daily user counts, average session durations, and total events. First, we’ll set up Mimesis with proper configuration for reproducibility:

 
from mimesis import Field, Schema
from mimesis.locales import Locale
from datetime import datetime, timedelta
import random

# Set up reproducibility
random.seed(42)

# Initialize field
field = Field(locale=Locale.EN, seed=42)

def daily_metrics_schema():
    # Generate a date between 2023-01-01 and 2023-12-31
    base_date = field('datetime.date', start=2023, end=2023)
    
    return {
        'date': base_date,
        'daily_users': field('random.randint', a=1000, b=5000),
        'avg_session_duration': field('random.randint', a=120, b=600),  # in seconds
        'total_events': field('random.randint', a=5000, b=20000)
    }

# Create schema for multiple days
schema = Schema(schema=daily_metrics_schema, iterations=5)
daily_data = schema.create()

# Print the results
for record in daily_data:
    print("\nDaily Metrics:")
    for key, value in record.items():
        print(f"{key}: {value}")

This code demonstrates several key concepts in Mimesis time series generation. 

1. Reproducibility Setup – We use random.seed(42) and initialize our Field with the same seed to ensure consistent results across different runs. 

2. Schema Definition – The daily_metrics_schema function defines our data structure, using Mimesis’s field generators to create realistic values:

  • Dates are generated within 2023 using field(‘datetime.date’, start=2023, end=2023)
  • Daily users are randomized between 1,000 and 5,000
  • Session durations range from 120 to 600 seconds
  • Total events scale from 5,000 to 20,000

3. Multiple Record Generation – We use the Schema class with iterations=5 to generate multiple days of data in one go.

When we run this code, we get output that represents five different days of metrics:

 
Daily Metrics:
date: 2023-01-24
daily_users: 3619
avg_session_duration: 177
total_events: 5409

Daily Metrics:
date: 2023-04-08
daily_users: 4037
avg_session_duration: 260
total_events: 9012

Daily Metrics:
date: 2023-12-04
daily_users: 1914
avg_session_duration: 191
total_events: 17066

Daily Metrics:
date: 2023-10-14
daily_users: 1419
avg_session_duration: 466
total_events: 17135

Daily Metrics:
date: 2023-01-03
daily_users: 4654
avg_session_duration: 399
total_events: 6424

Looking at this output, we can see that Mimesis has generated a realistic set of daily metrics. The dates are distributed throughout 2023, and the metrics show natural variation while staying within our defined bounds. 

Creating Hourly Traffic Patterns

Building upon our daily metrics, let’s explore how to generate more granular hourly data with realistic traffic patterns. This example demonstrates how to create web traffic data that simulates typical business hour fluctuations:

 
def generate_hourly_data():
    # Set random seed for reproducibility
    random.seed(42)
    field = Field(locale=Locale.EN, seed=42)
    
    # Start from January 1, 2023
    start_date = datetime(2023, 1, 1)
    hourly_data = []
    
    # Generate 24 hours of data
    for hour in range(24):
        current_time = start_date + timedelta(hours=hour)
        
        # Create realistic patterns - more traffic during business hours
        if 9 <= hour <= 17:  # Business hours
            traffic_multiplier = 1.5
        elif 0 <= hour <= 5:  # Early morning hours
            traffic_multiplier = 0.3
        else:  # Evening hours
            traffic_multiplier = 1.0
            
        base_traffic = field('random.randint', a=100, b=500)
        
        hourly_data.append({
            'timestamp': current_time.strftime('%Y-%m-%d %H:00:00'),
            'visitors': int(base_traffic * traffic_multiplier),
            'page_views': int(base_traffic * traffic_multiplier * field('random.uniform', a=2.0, b=4.0)),
            'server_load': round(field('random.uniform', a=20.0, b=80.0) * traffic_multiplier, 2)
        })
    
    return hourly_data

hourly_metrics = generate_hourly_data()

# Print first 5 hours of data
for record in hourly_metrics[:5]:
    print("\nHourly Metrics:")
    for key, value in record.items():
        print(f"{key}: {value}")

This code introduces several advanced concepts for time series generation. 
1. Time-Based Pattern Creation – We implement different traffic patterns using a multiplier system:

  • Business hours (9 AM – 5 PM): Traffic multiplier of 1.5x
  • Early morning hours (12 AM – 5 AM): Traffic multiplier of 0.3x
  • Evening hours: Base traffic (1.0x multiplier)

2. Metric Relationships – The code demonstrates how to create related metrics:

  • Base traffic is generated using field(‘random.randint’, a=100, b=500)
  • Page views are calculated as a multiple of visitors using field(‘random.uniform’, a=2.0, b=4.0)
  • Server load scales with traffic but maintains its own randomization

3. DateTime Handling – We use datetime and timedelta for precise time increments, formatting the output with strftime().

Here’s a sample of the generated hourly metrics:

 
Hourly Metrics:
timestamp: 2023-01-01 00:00:00
visitors: 128
page_views: 284
server_load: 19.35

Hourly Metrics:
timestamp: 2023-01-01 01:00:00
visitors: 67
page_views: 165
server_load: 19.26

Hourly Metrics:
timestamp: 2023-01-01 02:00:00
visitors: 133
page_views: 465
server_load: 15.82

Hourly Metrics:
timestamp: 2023-01-01 03:00:00
visitors: 120
page_views: 342
server_load: 6.54

Hourly Metrics:
timestamp: 2023-01-01 04:00:00
visitors: 63
page_views: 156
server_load: 16.84

The output shows the early morning hours (00:00-04:00) with appropriately reduced traffic levels, demonstrating how our multiplier system creates realistic variations in website activity. 

Incorporating Seasonal Patterns

Finally, let’s explore how to generate data with seasonal variations, which is often used for simulating business metrics that fluctuate throughout the year. This example shows how to create monthly data with built-in seasonal patterns:

 
def generate_seasonal_data():
    # Set random seed for reproducibility
    random.seed(42)
    field = Field(locale=Locale.EN, seed=42)
    
    # Generate 12 months of data with seasonal patterns
    base_date = datetime(2023, 1, 1)
    monthly_data = []
    
    for month in range(12):
        current_date = (base_date.replace(day=1) + 
                       timedelta(days=32 * month)).replace(day=1)
        
        # Create seasonal factors (higher in summer, lower in winter)
        season_factor = 1.0 + 0.5 * abs(abs(month - 5.5) - 6) / 6
        
        # Base metrics
        base_revenue = field('random.randint', a=50000, b=100000)
        base_customers = field('random.randint', a=1000, b=2000)
        
        monthly_data.append({
            'month': current_date.strftime('%Y-%m'),
            'revenue': int(base_revenue * season_factor),
            'active_customers': int(base_customers * season_factor),
            'avg_transaction': round(field('random.uniform', a=40.0, b=80.0), 2),
            'season_factor': round(season_factor, 2)
        })
    
    return monthly_data

seasonal_metrics = generate_seasonal_data()

# Print first 4 months of data
for record in seasonal_metrics[:4]:
    print("\nMonthly Seasonal Metrics:")
    for key, value in record.items():
        print(f"{key}: {value})

This code introduces several sophisticated concepts for seasonal data generation.
1. Seasonal Factor Calculation – We create a mathematical formula to simulate seasonal variations:

  • The formula 1.0 + 0.5 * abs(abs(month – 5.5) – 6) / 6 creates a curved pattern
  • Values peak during summer months and decrease during winter months
  • The season factor ranges from approximately 1.0 to 1.5

2. Date Handling for Months:

  • Uses datetime.replace() and timedelta to ensure proper month progression
  • Handles the varying lengths of months correctly

3. Business Metrics Generation:

  • Base revenue uses field(‘random.randint’, a=50000, b=100000)
  • Customer counts range from 1,000 to 2,000 before seasonal adjustment
  • Each metric is multiplied by the seasonal factor to reflect seasonal trends

Here’s a sample of the generated monthly data:

 
Monthly Seasonal Metrics:
month: 2023-01
revenue: 95734
active_customers: 1160
avg_transaction: 41.0
season_factor: 1.04

Monthly Seasonal Metrics:
month: 2023-02
revenue: 76527
active_customers: 1406
avg_transaction: 48.93
season_factor: 1.12

Monthly Seasonal Metrics:
month: 2023-03
revenue: 118736
active_customers: 1334
avg_transaction: 67.07
season_factor: 1.21

Monthly Seasonal Metrics:
month: 2023-04
revenue: 110748
active_customers: 1406
avg_transaction: 63.62
season_factor: 1.29

The output demonstrates how the seasonal factor gradually increases as we move from winter (January) towards summer months, with corresponding effects on revenue and customer metrics. 

Conclusion

Time series data generation with Mimesis strikes a delicate balance between structure and randomness. The examples we’ve explored demonstrate how to create temporal patterns that feel natural while maintaining complete control over the data generation process.

The techniques covered here – from daily metrics to seasonal variations – serve as foundational patterns that can be adapted and combined. By leveraging Mimesis’s precise control over random generation alongside temporal multipliers and seasonal factors, we can create datasets that capture the rhythms and patterns of real-world time series data.

These patterns can be extended further by incorporating additional factors like anomaly injection, trend modeling, or correlation between different metrics – all while maintaining the reproducibility that makes Mimesis such a powerful tool for temporal data generation.

Leave a Reply

Your email address will not be published. Required fields are marked *