### Setup Logger with Default Settings Source: https://context7.com/1kastner/conflowgen/llms.txt Initializes the logger with default settings. Use this for basic logging needs. ```python logger = conflowgen.setup_logger() logger.info("Starting ConFlowGen scenario generation") ``` -------------------------------- ### Complete ConFlowGen Workflow Example Source: https://context7.com/1kastner/conflowgen/llms.txt A comprehensive example demonstrating the typical ConFlowGen usage, including setting up logging, creating a database, configuring generation properties, defining container length distributions, adding vessel schedules, running previews and analyses, and exporting the generated data. ```python import datetime import conflowgen # 1. Setup logging logger = conflowgen.setup_logger() logger.info("Starting container flow generation") # 2. Create database db = conflowgen.DatabaseChooser(sqlite_databases_directory="./databases") db.create_new_sqlite_database("full_example.sqlite", assume_tas=True, overwrite=True) # 3. Configure generation properties flow_mgr = conflowgen.ContainerFlowGenerationManager() flow_mgr.set_properties( name="Full Example", start_date=datetime.date(2024, 7, 1), end_date=datetime.date(2024, 7, 14), transportation_buffer=1.15 ) # 4. Set container length distribution conflowgen.ContainerLengthDistributionManager().set_container_length_distribution({ conflowgen.ContainerLength.twenty_feet: 0.4, conflowgen.ContainerLength.forty_feet: 0.55, conflowgen.ContainerLength.forty_five_feet: 0.05, conflowgen.ContainerLength.other: 0.0 }) # 5. Add vessel schedules pcm = conflowgen.PortCallManager() # Deep-sea vessel (weekly) pcm.add_service_that_calls_terminal( vehicle_type=conflowgen.ModeOfTransport.deep_sea_vessel, service_name="Asia Express", vehicle_arrives_at=datetime.date(2024, 7, 3), vehicle_arrives_at_time=datetime.time(6, 0), average_vehicle_capacity=12000, average_inbound_container_volume=1800, vehicle_arrives_every_k_days=7, next_destinations=[("CNSHA", 0.5), ("SGSIN", 0.5)] ) # Feeder (twice weekly) pcm.add_service_that_calls_terminal( vehicle_type=conflowgen.ModeOfTransport.feeder, service_name="Baltic Link", vehicle_arrives_at=datetime.date(2024, 7, 2), vehicle_arrives_at_time=datetime.time(14, 0), average_vehicle_capacity=800, average_inbound_container_volume=300, vehicle_arrives_every_k_days=3 ) # Train (daily) pcm.add_service_that_calls_terminal( vehicle_type=conflowgen.ModeOfTransport.train, service_name="Inland Shuttle", vehicle_arrives_at=datetime.date(2024, 7, 1), vehicle_arrives_at_time=datetime.time(5, 0), average_vehicle_capacity=80, average_inbound_container_volume=70, vehicle_arrives_every_k_days=1 ) # 6. Run previews logger.info("Running previews...") conflowgen.run_all_previews(as_text=True, as_graph=False) # 7. Generate container flow logger.info("Generating container flow...") flow_mgr.generate() # 8. Run analyses logger.info("Running analyses...") conflowgen.run_all_analyses(as_text=True, as_graph=False) # 9. Export data export_mgr = conflowgen.ExportContainerFlowManager() export_path = export_mgr.export( folder_name="full_example_output", path_to_export_folder="./exports", file_format=conflowgen.ExportFileFormat.csv ) logger.info(f"Exported to: {export_path}") # 10. Cleanup db.close_current_connection() logger.info("Generation complete") ``` -------------------------------- ### Import Conflowgen and Setup Logger Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Imports the Conflowgen library and initializes its logger. Specify the directory for log files. ```python import conflowgen conflowgen.setup_logger( logging_directory="./data/logger", # use subdirectory relative to Jupyter Notebook ) ``` -------------------------------- ### Setting up ConFlowGen Source: https://github.com/1kastner/conflowgen/blob/main/docs/api.rst Information on how to set up and configure ConFlowGen, including database selection and logger setup. ```APIDOC ## Setting up ConFlowGen This section covers the initial setup and configuration of the ConFlowGen library. ### Database Configuration - `conflowgen.DatabaseChooser` - This class is used for selecting and configuring the database for ConFlowGen. ### Logger Setup - `conflowgen.setup_logger` - A function to set up the logging mechanism for ConFlowGen. ``` -------------------------------- ### Install ConFlowGen from repository Source: https://github.com/1kastner/conflowgen/blob/main/Readme.md Clone the ConFlowGen repository and install it locally. Ensure git-lfs is installed for larger files. ```bash git clone https://github.com/1kastner/conflowgen cd conflowgen pip install . ``` -------------------------------- ### Setup Logger with Custom Settings Source: https://context7.com/1kastner/conflowgen/llms.txt Configures the logger with a custom directory for log files and a specific format string. Useful for more controlled logging. ```python logger = conflowgen.setup_logger( logging_directory="./my_logs", format_string="%(asctime)s - %(levelname)s - %(message)s" ) ``` -------------------------------- ### Install ConFlowGen with Development Dependencies Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Clone the repository and install the project with optional development dependencies using pip. This command installs the package in editable mode and includes extras for development, such as testing and documentation. ```bash git clone https://github.com/1kastner/conflowgen cd conflowgen pip install -e .[dev] ``` -------------------------------- ### Install ConFlowGen using pip Source: https://github.com/1kastner/conflowgen/blob/main/Readme.md Install the latest version of ConFlowGen using pip from your command line interface. ```bash pip install conflowgen ``` -------------------------------- ### Initialize ModeOfTransportDistributionManager Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/visuals_for_ldic2022_paper.ipynb Initializes the ModeOfTransportDistributionManager. This is a setup step for managing transport mode distributions. ```python mode_of_transport_distribution_manager = ModeOfTransportDistributionManager() ``` -------------------------------- ### Install ConFlowGen using conda Source: https://github.com/1kastner/conflowgen/blob/main/Readme.md Install ConFlowGen from the conda-forge channel using the conda package manager. ```bash conda install conda-forge::conflowgen ``` -------------------------------- ### Get Container Dwell Time Distributions Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Initializes a manager to retrieve container dwell time distributions. This provides statistical summaries of dwell times. ```python container_dwell_time_distribution_manager = ( conflowgen.ContainerDwellTimeDistributionManager() ) distributions = ( container_dwell_time_distribution_manager.get_container_dwell_time_distribution() ) ``` -------------------------------- ### Initialize Container Flow Generation Manager Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/first_steps.ipynb Instantiate the ContainerFlowGenerationManager and set its properties, including a name, start date, and end date for the simulation. The end date is set to 21 days from the current date. ```python today = datetime.datetime.now().date() container_flow_generation_manager = conflowgen.ContainerFlowGenerationManager() container_flow_generation_manager.set_properties( name="Demo file", start_date=today, end_date=today + datetime.timedelta(days=21) ) ``` -------------------------------- ### Initialize Database and Container Flow Manager Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/analyses_with_missing_data.ipynb Initializes a database chooser and creates a new in-memory SQLite database. It also sets up the logger and configures the ContainerFlowGenerationManager with start and end dates for generating container flows. ```python database_chooser = conflowgen.DatabaseChooser() database_chooser.create_new_sqlite_database(":memory:") conflowgen.setup_logger( logging_directory="./data/logger" ) now = datetime.datetime.now() end_date = now + datetime.timedelta(days=30) manager = conflowgen.ContainerFlowGenerationManager() manager.set_properties( start_date=now, end_date=end_date ) ``` -------------------------------- ### Create a new SQLite database with ConFlowGen Source: https://github.com/1kastner/conflowgen/blob/main/Readme.md After installing the module, use this Python code to initialize ConFlowGen and create a new SQLite database file. ```python import conflowgen database_chooser = conflowgen.DatabaseChooser() database_chooser.create_new_sqlite_database("new_example.sqlite") ... ``` -------------------------------- ### Setup ConFlowGen Logger Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/first_steps.ipynb Configure the logger for ConFlowGen to capture messages. Specify the directory for log files and the desired format string. This helps in monitoring the generation process. ```python logger = conflowgen.setup_logger( logging_directory="./data/logger", # use subdirectory relative to Jupyter Notebook format_string="%(message)s" # only show log messages, discard timestamp etc. ) ``` -------------------------------- ### Get Container Weight Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Retrieves the default container weight distribution, which can vary by container length. This manager must be initialized after the database is set up. ```python container_weight_distribution_manager = conflowgen.ContainerWeightDistributionManager() weight_distribution = ( container_weight_distribution_manager.get_container_weight_distribution() ) weight_distribution ``` -------------------------------- ### Get and Modify Inbound/Outbound Vehicle Capacity Report Graph Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/previews.ipynb Retrieves the graph object for the vehicle capacity report, allowing for further programmatic manipulation before display. This example demonstrates changing the color of the first bar in the graph. ```python plt_ax = preview_report.get_report_as_graph() rectangles = list( filter(lambda x: isinstance(x, matplotlib.patches.Rectangle), plt_ax.get_children()) ) first_bar = rectangles[0] first_bar.set_color("black") plt.show() ``` -------------------------------- ### Build Documentation on Windows Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Generate the project documentation using Sphinx on Windows. This command should be run from the 'docs' directory. ```batch .\make.bat html ``` -------------------------------- ### Initialize and Configure Database Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/first_steps.ipynb Set up the database chooser to manage SQLite databases. Specify the directory for storing databases and create a new SQLite database file, overwriting if it already exists. ```python database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory="./data/db" # use subdirectory relative to Jupyter Notebook ) demo_file_name = "my_demo.sqlite" database_chooser.create_new_sqlite_database(demo_file_name, overwrite=True) ``` -------------------------------- ### Set up project path Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Configures the system path to include the Conflowgen library, allowing it to be imported. ```python path_to_conflowgen = os.path.abspath( os.path.join( os.pardir, # notebooks os.pardir, # tests os.pardir # conflowgen ) ) path_to_conflowgen ``` ```python sys.path.insert( 0, path_to_conflowgen ) ``` -------------------------------- ### Run All Previews Source: https://context7.com/1kastner/conflowgen/llms.txt Executes lightweight preview analyses on input distributions and schedules. Useful for validating configurations before full data generation. ```python import conflowgen # Run all previews with text output only conflowgen.run_all_previews( as_text=True, as_graph=False ) # Run previews with graphs in Jupyter notebook conflowgen.run_all_previews( as_text=True, as_graph=True, display_as_ipython_svg=True, display_in_markup_language="markdown" ) # Run previews with static graphs (for documentation) conflowgen.run_all_previews( as_text=True, as_graph=True, static_graphs=True ) ``` -------------------------------- ### Build Documentation on Linux Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Generate the project documentation using Sphinx on Linux. This command should be run from the 'docs' directory. ```bash make html ``` -------------------------------- ### Initialize and list SQLite databases Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/fast_analyses_for_proof_of_concept.ipynb Creates a DatabaseChooser instance to manage SQLite databases and lists all available databases in the specified directory. ```python database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory=os.path.join( "..", "..", "..", "docs", "notebooks", "data", "prepared_dbs" ) ) database_chooser.list_all_sqlite_databases() ``` -------------------------------- ### Initialize Database Chooser Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Sets up the DatabaseChooser to manage SQLite databases. Provide the directory where your databases are stored. ```python database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory="../../data/databases" # use subdirectory relative to Jupyter Notebook ) ``` -------------------------------- ### Import Required Modules Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Imports necessary libraries for data manipulation, plotting, and ConFlowGen functionalities. Ensure these are installed before running. ```python import datetime import itertools import matplotlib import matplotlib.pyplot as plt import numpy as np from IPython.display import Markdown import conflowgen ``` -------------------------------- ### Build Documentation with Strict Options Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Generate the project documentation using Sphinx with strict options to catch warnings and continue on errors. This command is equivalent to running 'python -m sphinx -W --keep-going ./docs ./docs/_build'. ```bash python -m sphinx -W --keep-going ./docs ./docs/_build ``` -------------------------------- ### Set Matplotlib Style Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/analyses.ipynb Sets a preferred Matplotlib style for consistent plot aesthetics. This style should be available in your Matplotlib installation. ```python preferred_matplotlib_style = "seaborn-v0_8-colorblind" ``` -------------------------------- ### Run Demo Script Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Executes a prerequisite Python script to generate necessary database entries for the analysis. This must be run before loading the database. ```bash python ./examples/Python_Script/demo_continental_gateway.py ``` -------------------------------- ### Get Container Length Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Retrieves the default distribution of container lengths. This manager must be initialized after the database is set up. ```python container_length_manager = conflowgen.ContainerLengthDistributionManager() length_distribution = container_length_manager.get_container_length_distribution() length_distribution ``` -------------------------------- ### Cache Decorator Example Source: https://context7.com/1kastner/conflowgen/llms.txt Demonstrates the usage of the cache decorator for methods that perform expensive calculations. The result is cached based on the method and its arguments. ```python class CustomAnalysis: @conflowgen.DataSummariesCache.cache_result def expensive_calculation(self, param): # Result will be cached based on method + arguments return compute_something(param) ``` -------------------------------- ### Load or Initialize SQLite Database Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/input_data_inspection/calibrating_the_traffic.ipynb Chooses a database handler and loads an existing SQLite database file if it exists, otherwise prints a message indicating the database is missing. ```python database_chooser = DatabaseChooser() demo_file_name = "demo_deham_cta.sqlite" if demo_file_name in database_chooser.list_all_sqlite_databases(): database_chooser.load_existing_sqlite_database(demo_file_name) else: print("Database is missing, nothing to do here") ``` -------------------------------- ### Get Mode of Transport Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Retrieve the current distribution of transport modes. This is useful for understanding the default or currently set routing probabilities. ```python mode_of_transport = conflowgen.ModeOfTransportDistributionManager() mode_of_transport_distribution = mode_of_transport.get_mode_of_transport_distribution() mode_of_transport_distribution ``` -------------------------------- ### Load Database for Conflowgen Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/analyses.ipynb Initializes the DatabaseChooser and loads a specific SQLite database. Ensure the 'data/prepared_dbs' directory exists and contains the specified database file. ```python import matplotlib.pyplot as plt from IPython.display import Markdown, display import conflowgen database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory="./data/prepared_dbs" # subdirectory relative to Jupyter Notebook ) database_chooser.load_existing_sqlite_database("demo_poc.sqlite") ``` -------------------------------- ### Run All Previews as Text Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/previews.ipynb Executes all available previews and prints their textual reports to the standard output. This is a convenience function for a comprehensive overview. ```python conflowgen.run_all_previews( as_text=True, display_text_func=print, ) ``` -------------------------------- ### Get Container Flow Properties Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/input_data_inspection/calibrating_the_traffic.ipynb Initializes the ContainerFlowGenerationManager and retrieves its properties, printing them to the console. This provides an overview of the current container flow configuration. ```python container_flow_generation_manager = ContainerFlowGenerationManager() container_flow_properties = container_flow_generation_manager.get_properties() for key, value in container_flow_properties.items(): print(f"{key:<30}: {value}") ``` -------------------------------- ### Set up Conflowgen Path Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Configures the system path to import the Conflowgen library. Ensure this path is correct for your project structure. ```python import os import sys import matplotlib.pyplot as plt import numpy as np path_to_conflowgen = os.path.abspath( os.path.join( os.pardir, # notebooks os.pardir, # tests os.pardir # conflowgen ) ) path_to_conflowgen ``` ```python sys.path.insert( 0, path_to_conflowgen ) ``` -------------------------------- ### Import Libraries for ConFlowGen Analysis Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/checking container properties.ipynb Imports necessary Python libraries for data manipulation, file system interaction, and visualization. Ensure these are installed in your environment. ```python import os import pathlib import ipywidgets as widgets import pandas as pd from IPython.display import Markdown import matplotlib.pyplot as plt ``` -------------------------------- ### Import and Initialize Conflowgen Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/previews_with_missing_data.ipynb Imports the conflowgen library and initializes the DatabaseChooser and ContainerFlowGenerationManager. Sets up logger and defines date properties for the manager. ```python import conflowgen conflowgen.__file__ ``` ```python database_chooser = conflowgen.DatabaseChooser() database_chooser.create_new_sqlite_database(":memory:") conflowgen.setup_logger( logging_directory="./data/logger" ) now = datetime.datetime.now() manager = conflowgen.ContainerFlowGenerationManager() manager.set_properties( start_date=now, end_date=now + datetime.timedelta(days=30) ) ``` -------------------------------- ### Run All Previews as Text and Graph with Markdown Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/previews.ipynb Generates all previews as both text and graphs, integrating them into Markdown for display within an IPython environment. This allows for a rich, side-by-side presentation of textual and visual data. ```python conflowgen.run_all_previews( as_text=True, as_graph=True, display_text_func=lambda text: display(Markdown(text)), display_in_markup_language="markdown", display_as_ipython_svg=True, ) ``` -------------------------------- ### Set up project path Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/fast_analyses_for_proof_of_concept.ipynb Determines the absolute path to the conflowgen project directory, which is necessary for importing the library. ```python path_to_conflowgen = os.path.abspath( os.path.join( os.pardir, # notebooks os.pardir, # tests os.pardir # conflowgen ) ) path_to_conflowgen ``` -------------------------------- ### Show preview and analysis reports Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Initializes and displays both the truck gate throughput preview report and the analysis report as graphs. ```python # Get analysis data truck_gate_throughput_analysis = conflowgen.TruckGateThroughputAnalysis() truck_gate_throughput_analysis_data = truck_gate_throughput_analysis.get_throughput_over_time() # Convert the dictionary to a pandas Series series_data = pd.Series(truck_gate_throughput_analysis_data) # Set the datetime values as the index series_data.index = pd.to_datetime(series_data.index) # Filter the data for the period in July 2021 filtered_data = series_data.loc['2021-07-5':'2021-07-31'] # Group the data by week starting on Monday weekly_grouped_data = filtered_data.groupby(pd.Grouper(freq="W-SUN")) # Plotting fig, ax = plt.subplots(figsize=(10, 6)) ``` -------------------------------- ### Import ConFlowGen and Libraries Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/first_steps.ipynb Import the ConFlowGen library along with other necessary Python modules like os, datetime, and pandas. Ensure these libraries are installed in your environment. ```python import os import datetime import pandas as pd import conflowgen ``` -------------------------------- ### Load SQLite Database Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/previews.ipynb Loads an existing SQLite database from a specified directory. Ensure the 'data/prepared_dbs' subdirectory exists and contains the 'demo_deham_cta.sqlite' file. ```python import datetime import matplotlib import matplotlib.pyplot as plt from IPython.display import Markdown import conflowgen database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory="./data/prepared_dbs" # subdirectory relative to Jupyter Notebook ) database_chooser.load_existing_sqlite_database("demo_deham_cta.sqlite") ``` -------------------------------- ### Get Truck Arrival Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Instantiate the TruckArrivalDistributionManager and retrieve the default truck arrival distribution. This distribution represents the probability of truck arrivals throughout the week. ```python truck_arrival_distribution_manager = conflowgen.TruckArrivalDistributionManager() truck_arrival_distribution = ( truck_arrival_distribution_manager.get_truck_arrival_distribution() ) truck_arrival_distribution ``` -------------------------------- ### Display truck gate throughput report for a specific date range Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Displays the truck gate throughput report as a graph for a specified start and end date. ```python truck_gate_throughput_analysis_report.show_report_as_graph( start_date=datetime.datetime(2021, 6, 28), end_date=datetime.datetime(2021, 7, 5) ) ``` -------------------------------- ### Run Conflowgen Previews Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/previews_with_missing_data.ipynb Executes all available previews, with options to display them as text and/or graphs. Uses a provided function to display text output. ```python conflowgen.run_all_previews( as_text=True, as_graph=True, display_text_func=print, ) ``` -------------------------------- ### Get and print truck gate throughput report as text Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Retrieves the truck gate throughput report as formatted text for a specified date range and prints it to the console. ```python text = truck_gate_throughput_analysis_report.get_report_as_text( start_date=datetime.datetime(2021, 6, 28), end_date=datetime.datetime(2021, 7, 5) ) print(text) ``` -------------------------------- ### Get Container Storage Requirement Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Initializes the StorageRequirementDistributionManager and retrieves the current storage requirement distribution. This distribution can be visualized to understand storage needs per container length. ```python storage_manager = conflowgen.StorageRequirementDistributionManager() storage_requirement_distribution = ( storage_manager.get_storage_requirement_distribution() ) storage_requirement_distribution ``` -------------------------------- ### Run All Analyses with Date Range Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/analyses_with_missing_data.ipynb Executes all analyses with specified start and end dates. This allows for focused analysis within a particular time frame, in addition to text and graph outputs. ```python conflowgen.run_all_analyses( as_text=True, as_graph=True, display_text_func=print, start_date=now, end_date=end_date ) ``` -------------------------------- ### Lint Project with Ruff Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Use Ruff to lint the entire project, including Jupyter notebooks. Execute from the project root. ```bash ruff check ``` -------------------------------- ### Set Truck Arrival Distribution Source: https://context7.com/1kastner/conflowgen/llms.txt Defines the weekly probability distribution for truck arrivals, where hours 0-167 represent Monday 00:00 through Sunday 23:00. This example sets custom probabilities for weekdays and weekends. ```python import conflowgen truck_manager = conflowgen.TruckArrivalDistributionManager() # Get current distribution current_dist = truck_manager.get_truck_arrival_distribution() # Set custom distribution (simplified example for working hours) # Each key is hour of the week, value is relative probability weekly_dist = {} for day in range(7): # 0=Monday to 6=Sunday for hour in range(24): week_hour = day * 24 + hour if day < 5: # Weekdays if 6 <= hour < 18: # Working hours weekly_dist[week_hour] = 1.0 elif 18 <= hour < 22: # Evening weekly_dist[week_hour] = 0.5 else: # Night weekly_dist[week_hour] = 0.1 else: # Weekend if 8 <= hour < 16: weekly_dist[week_hour] = 0.3 else: weekly_dist[week_hour] = 0.05 truck_manager.set_truck_arrival_distribution(weekly_dist) ``` -------------------------------- ### Instantiate Inbound/Outbound Vehicle Capacity Preview Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/previews.ipynb Creates a preview object to analyze vehicle capacity for a given date range and transportation buffer. The results can be displayed directly or accessed as used and maximum values. ```python inbound_and_outbound_vehicle_capacity_preview = ( conflowgen.InboundAndOutboundVehicleCapacityPreview( start_date=datetime.date(2021, 7, 1), end_date=datetime.date(2021, 7, 31), transportation_buffer=0.2, ) ) display( inbound_and_outbound_vehicle_capacity_preview.get_inbound_capacity_of_vehicles() ) outbound_capacities = ( inbound_and_outbound_vehicle_capacity_preview.get_outbound_capacity_of_vehicles() ) display(outbound_capacities.used) display(outbound_capacities.maximum) ``` -------------------------------- ### Configure Container Flow Generation with ContainerFlowGenerationManager Source: https://context7.com/1kastner/conflowgen/llms.txt Initialize `ContainerFlowGenerationManager` after selecting a database to set generation properties like date range, transportation buffer, and ramp-up/down periods. Check for existing data before generating. ```python import datetime import conflowgen # Initialize after database is selected flow_manager = conflowgen.ContainerFlowGenerationManager() # Configure generation properties flow_manager.set_properties( name="Hamburg Terminal Scenario July 2024", start_date=datetime.date(2024, 7, 1), end_date=datetime.date(2024, 7, 31), transportation_buffer=1.2, # Outbound can carry 20% more than inbound ramp_up_period=datetime.timedelta(days=3), # 3-day ramp-up ramp_down_period=datetime.timedelta(days=3) # 3-day ramp-down ) # Get current properties props = flow_manager.get_properties() print(f"Scenario: {props['name']}, from {props['start_date']} to {props['end_date']}") # Check if data already exists (useful when reloading databases) if not flow_manager.container_flow_data_exists(): # Generate container flow data - this is the core computation flow_manager.generate() else: print("Using existing container flow data") ``` -------------------------------- ### Get Text Report for Feeder to Truck Transport Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Retrieves a textual report of container dwell times for feeder-to-truck transport with standard storage requirements. This provides a detailed summary for this specific traffic flow. ```python text = container_dwell_time_analysis_report.get_report_as_text( container_delivered_by_vehicle_type=conflowgen.ModeOfTransport.feeder, container_picked_up_by_vehicle_type=conflowgen.ModeOfTransport.truck, storage_requirement=conflowgen.StorageRequirement.standard, ) print(text) ``` -------------------------------- ### Generate Truck Arrival Preview Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Generates a preview of weekly truck arrivals for a specified date range and plots it with a solid yellow line. Requires setting up a TruckGateThroughputPreview object. ```python # Plot preview start_date = datetime.date(year=2021, month=7, day=5) end_date = datetime.date(year=2021, month=7, day=31) transportation_buffer = 0.2 truck_gate_throughput_preview = conflowgen.TruckGateThroughputPreview(start_date, end_date, transportation_buffer) preview_data = truck_gate_throughput_preview.get_weekly_truck_arrivals(True, True) preview_series = pd.Series(preview_data) preview_series.index = pd.to_datetime(preview_series.index) average_week_range = range(0, len(preview_series)) ax.plot(average_week_range, preview_series.values, label='Preview', color='yellow', linestyle='solid') ``` -------------------------------- ### Load or Check Database Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/input_data_inspection/visuals_for_ldic_2022_presentation.ipynb Initializes the DatabaseChooser and checks if a specific SQLite database file exists. If it exists, it loads the database; otherwise, it prints a message indicating the database is missing. ```python database_chooser = conflowgen.DatabaseChooser() demo_file_name = "demo_deham_cta.sqlite" if demo_file_name in database_chooser.list_all_sqlite_databases(): database_chooser.load_existing_sqlite_database(demo_file_name) else: print("Database is missing, nothing to do here") ``` -------------------------------- ### Plot Weekly Truck Arrivals Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Iterates through weekly grouped data to plot truck arrivals. Uses an offset for the first week's data and labels each plot with the week's start date. ```python for i, (group_name, group_data) in enumerate(weekly_grouped_data): # Extract the start date of the week for labeling week_start = group_data.index[0].strftime('%Y-%m-%d') if i == 0: offset = 168 - len(group_data) else: offset = 0 # Plot the data for the week ax.plot(range(offset, offset + len(group_data)), group_data.values, label=week_start, linestyle='--') ``` -------------------------------- ### Run all Conflowgen previews Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Executes all preview analyses within Conflowgen and displays them as graphs. ```python conflowgen.run_all_previews(as_graph=True) ``` -------------------------------- ### Get Text Report for Specific Transport Mode Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Retrieves a textual report of container dwell times for a specific inbound and outbound vehicle type and storage requirement. This is useful for detailed examination of a single transport path. ```python text = container_dwell_time_analysis_report.get_report_as_text( container_delivered_by_vehicle_type=conflowgen.ModeOfTransport.truck, container_picked_up_by_vehicle_type=conflowgen.ModeOfTransport.feeder, storage_requirement=conflowgen.StorageRequirement.standard, ) print(text) ``` -------------------------------- ### Generating Previews Source: https://github.com/1kastner/conflowgen/blob/main/docs/api.rst Documentation for generating and reporting various previews of container flow data. ```APIDOC ## Generating Previews This section covers the classes and functions for generating various previews of the container flow data. ### Preview Classes - `conflowgen.ContainerFlowByVehicleTypePreview` - `conflowgen.ContainerFlowByVehicleTypePreviewReport` - `conflowgen.InboundAndOutboundVehicleCapacityPreview` - `conflowgen.InboundAndOutboundVehicleCapacityPreviewReport` - `conflowgen.ModalSplitPreview` - `conflowgen.ModalSplitPreviewReport` - `conflowgen.QuaySideThroughputPreview` - `conflowgen.QuaySideThroughputPreviewReport` - `conflowgen.VehicleCapacityExceededPreview` - `conflowgen.VehicleCapacityUtilizationOnOutboundJourneyPreviewReport` - `conflowgen.TruckGateThroughputPreview` - `conflowgen.TruckGateThroughputPreviewReport ### Preview Generation Function - `conflowgen.run_all_previews` - Executes all configured preview generation tasks. ``` -------------------------------- ### Derive and Plot Truck Arrival Distribution Slice Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/in_spotlight.ipynb Extracts a relevant slice of the truck arrival distribution starting from the earliest truck time slot and converts it into a DataFrame for plotting. This visualizes the probability of truck arrivals over time. ```python truck_arrival_distribution_slice = truck_arrival_distribution.get_distribution_slice( earliest_truck_time_slot ) truck_arrival_distribution_slice_as_dates = { (container_arrival_time_hour + datetime.timedelta(hours=hours_from_now)): fraction * 100 for hours_from_now, fraction in truck_arrival_distribution_slice.items() } df_truck_arrival_distribution = pd.Series( truck_arrival_distribution_slice_as_dates ).to_frame("Truck Arrival Distribution") df_truck_arrival_distribution.plot(legend=False) plt.ylabel("Probability (as percentage overall)") plt.show() ``` -------------------------------- ### Recreate Database with Logging Enabled Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb This command recreates the in-memory SQLite database. If logging is set up, this action will produce log output. ```python database_chooser.create_new_sqlite_database(":memory:") ``` -------------------------------- ### Convert SVG to EMF with Inkscape Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/visuals_for_ldic2022_paper.ipynb Provides a function to convert SVG files to EMF format using Inkscape. This is useful for including vector graphics in Microsoft Office documents. Requires Inkscape to be installed and its executable path configured. ```python if os.name == 'nt': path_to_inkscape_executable = r"C:\\Program Files\\Inkscape\\bin\\inkscape.exe" else: raise RuntimeException("Add your path to your inkscape executable here") def convert_to_emf(input_file): output_file = "".join(input_file.split(".")[0:-1]) + ".emf" print(f"Converting {input_file} to {output_file}...") !"{path_to_inkscape_executable}" "{input_file}" --export-filename "{output_file}" ``` -------------------------------- ### Get Outbound to Inbound Vehicle Capacity Utilization Text Report Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/analyses.ipynb Retrieves and prints the text report for outbound to inbound vehicle capacity utilization, filtered for deep sea vessels and feeders. This function returns a string that is then printed. ```python outbound_to_inbound_vehicle_capacity_utilization_report = ( conflowgen.OutboundToInboundVehicleCapacityUtilizationAnalysisReport() ) print( outbound_to_inbound_vehicle_capacity_utilization_report.get_report_as_text( vehicle_type={ conflowgen.ModeOfTransport.deep_sea_vessel, conflowgen.ModeOfTransport.feeder, } ) ) ``` -------------------------------- ### Initialize Container Dwell Time Analysis Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_container_dwell_time_distribution_with_results.ipynb Creates an instance of ContainerDwellTimeAnalysisReport to access dwell time data and analysis results. ```python container_dwell_time_analysis_report = conflowgen.ContainerDwellTimeAnalysisReport() container_dwell_time_analysis = container_dwell_time_analysis_report.analysis ``` -------------------------------- ### Set Custom Truck Arrival Distribution Source: https://github.com/1kastner/conflowgen/blob/main/docs/notebooks/input_distributions.ipynb Set a custom truck arrival distribution for the entire week. The dictionary keys represent the hour of the week (0-167), and values represent the probability of a truck arriving in the hour slot starting at that key. ```python truck_arrival_distribution_manager.set_truck_arrival_distribution( { 0: 0.006944444444444444, 1: 0.006944444444444444, 2: 0.006944444444444444, 3: 0.006944444444444444, 4: 0.006944444444444444, 5: 0.006944444444444444, 6: 0.006944444444444444, 7: 0.006944444444444444, 8: 0.006944444444444444, 9: 0.006944444444444444, 10: 0.006944444444444444, 11: 0.006944444444444444, 12: 0.006944444444444444, 13: 0.006944444444444444, 14: 0.006944444444444444, 15: 0.006944444444444444, 16: 0.006944444444444444, 17: 0.006944444444444444, 18: 0.006944444444444444, 19: 0.006944444444444444, 20: 0.006944444444444444, 21: 0.006944444444444444, 22: 0.006944444444444444, 23: 0.006944444444444444, 24: 0.006944444444444444, 25: 0.006944444444444444, 26: 0.006944444444444444, 27: 0.006944444444444444, 28: 0.006944444444444444, 29: 0.006944444444444444, 30: 0.006944444444444444, 31: 0.006944444444444444, 32: 0.006944444444444444, 33: 0.006944444444444444, 34: 0.006944444444444444, 35: 0.006944444444444444, 36: 0.006944444444444444, 37: 0.006944444444444444, 38: 0.006944444444444444, 39: 0.006944444444444444, 40: 0.006944444444444444, 41: 0.006944444444444444, 42: 0.006944444444444444, 43: 0.006944444444444444, 44: 0.006944444444444444, 45: 0.006944444444444444, 46: 0.006944444444444444, 47: 0.006944444444444444, 48: 0.006944444444444444, 49: 0.006944444444444444, 50: 0.006944444444444444, 51: 0.006944444444444444, 52: 0.006944444444444444, 53: 0.006944444444444444, 54: 0.006944444444444444, 55: 0.006944444444444444, 56: 0.006944444444444444, 57: 0.006944444444444444, 58: 0.006944444444444444, 59: 0.006944444444444444, 60: 0.006944444444444444, 61: 0.006944444444444444, 62: 0.006944444444444444 } ) ``` -------------------------------- ### Run Flake8 for Code Linting Source: https://github.com/1kastner/conflowgen/blob/main/Contributing.md Invoke Flake8 to lint the project code. Ensure you are in the project root directory. ```bash flake8 ``` -------------------------------- ### Get Scheduled Vehicle File Paths in Python Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/visuals_for_ldic2022_paper.ipynb This function constructs and validates file paths for different types of scheduled vehicles (deep-sea vessels, feeders, barges, trains) within a specified data folder. It asserts that each expected CSV file exists. ```python def get_scheduled_vehicle_file_paths(path_to_folder_data_visual: str): path_to_deep_sea_vessels = os.path.join( path_to_folder_data_visual, "deep_sea_vessels.csv" ) path_to_feeders = os.path.join( path_to_folder_data_visual, "feeders.csv" ) path_to_barges = os.path.join( path_to_folder_data_visual, "barges.csv" ) path_to_trains = os.path.join( path_to_folder_data_visual, "trains.csv" ) scheduled_vehicle_file_paths = { "deep_sea_vessels": path_to_deep_sea_vessels, "feeders": path_to_feeders, "barges": path_to_barges, "trains": path_to_trains } for name, path in scheduled_vehicle_file_paths.items(): print("Check file exists for vehicle " + name + f" in folder {path_to_folder_data_visual}.") assert os.path.isfile(path) return scheduled_vehicle_file_paths scheduled_vehicle_file_paths_1a = get_scheduled_vehicle_file_paths(path_to_folder_data_visual_1a) scheduled_vehicle_file_paths_1b = get_scheduled_vehicle_file_paths(path_to_folder_data_visual_1b) scheduled_vehicle_file_paths_1c = get_scheduled_vehicle_file_paths(path_to_folder_data_visual_1c) ``` -------------------------------- ### Initialize Conflowgen with Schedules Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/visuals_for_ldic2022_paper.ipynb This function initializes the ContainerFlowGenerationManager, loads or creates a SQLite database, and populates it with schedules for feeders and deep-sea vessels. It handles date filtering and checks for existing schedules to avoid duplicates. Use this to set up the simulation environment. ```python import conflowgen seeded_random = random.Random(x=1) import_root_dir = os.path.abspath( os.path.join( os.path.dirname(conflowgen.__path__[0]), "examples", "Python_Script", "data" ) ) import_deham_dir = os.path.join( import_root_dir, "DEHAM", "CT Altenwerder" ) df_deep_sea_vessels = pd.read_csv( os.path.join( import_deham_dir, "deep_sea_vessel_input.csv" ), index_col=[0] ) df_feeders = pd.read_csv( os.path.join( import_deham_dir, "feeder_input.csv" ), index_col=[0] ) df_barges = pd.read_csv( os.path.join( import_deham_dir, "barge_input.csv" ), index_col=[0] ) df_trains = pd.read_csv( os.path.join( import_deham_dir, "train_input.csv" ), index_col=[0] ) logger = setup_logger() database_chooser = DatabaseChooser() def initialize_conflowgen(figure_name: str, force_reload: bool = False) -> ContainerFlowGenerationManager: demo_file_name = f"demo_deham_cta_visual_{figure_name}_{datetime.datetime.now().date().isoformat()}.sqlite" if demo_file_name in database_chooser.list_all_sqlite_databases() and not force_reload: database_chooser.load_existing_sqlite_database(demo_file_name) container_flow_generation_manager = ContainerFlowGenerationManager() return container_flow_generation_manager else: database_chooser.create_new_sqlite_database(demo_file_name) container_flow_generation_manager = ContainerFlowGenerationManager() container_flow_start_date = datetime.date(year=2021, month=7, day=1) container_flow_end_date = datetime.date(year=2021, month=7, day=31) container_flow_generation_manager.set_properties( name="Demo DEHAM CTA Visual 1a", start_date=container_flow_start_date, end_date=container_flow_end_date ) port_call_manager = PortCallManager() for i, row in df_feeders.iterrows(): feeder_vehicle_name = row["vehicle_name"] + "-unique" capacity = row["capacity"] vessel_arrives_at_as_pandas_type = row["arrival (planned)"] vessel_arrives_at_as_datetime_type = pd.to_datetime(vessel_arrives_at_as_pandas_type) if vessel_arrives_at_as_datetime_type.date() < container_flow_start_date: logger.info(f"Skipping feeder '{feeder_vehicle_name}' because it arrives before the start") continue if vessel_arrives_at_as_datetime_type.date() > container_flow_end_date: logger.info(f"Skipping feeder '{feeder_vehicle_name}' because it arrives after the end") continue if port_call_manager.has_schedule(feeder_vehicle_name, vehicle_type=ModeOfTransport.feeder): logger.info(f"Skipping feeder '{feeder_vehicle_name}' because it already exists") continue logger.info(f"Add feeder '{feeder_vehicle_name}' to database") moved_capacity = int(round(capacity * seeded_random.uniform(0.3, 0.8) / 2)) port_call_manager.add_vehicle( vehicle_type=ModeOfTransport.feeder, service_name=feeder_vehicle_name, vehicle_arrives_at=vessel_arrives_at_as_datetime_type.date(), vehicle_arrives_at_time=vessel_arrives_at_as_datetime_type.time(), average_vehicle_capacity=capacity, average_moved_capacity=moved_capacity, vehicle_arrives_every_k_days=-1 ) logger.info("Start importing deep sea vessels...") for i, row in df_deep_sea_vessels.iterrows(): deep_sea_vessel_vehicle_name = row["vehicle_name"] + "-unique" capacity = row["capacity"] vessel_arrives_at_as_pandas_type = row["arrival (planned)"] vessel_arrives_at_as_datetime_type = pd.to_datetime(vessel_arrives_at_as_pandas_type) if vessel_arrives_at_as_datetime_type.date() < container_flow_start_date: logger.info(f"Skipping deep sea vessel '{deep_sea_vessel_vehicle_name}' because it arrives before the start") continue if vessel_arrives_at_as_datetime_type.date() > container_flow_end_date: logger.info(f"Skipping deep sea vessel '{deep_sea_vessel_vehicle_name}' because it arrives after the end") continue if port_call_manager.has_schedule(deep_sea_vessel_vehicle_name, vehicle_type=ModeOfTransport.deep_sea_vessel): logger.info(f"Skipping deep sea service '{deep_sea_vessel_vehicle_name}' because it already exists") continue logger.info(f"Add deep sea vessel '{deep_sea_vessel_vehicle_name}' to database") moved_capacity = int(round(capacity * seeded_random.uniform(0.25, 0.5) / 2)) ``` -------------------------------- ### Add Barge to Port Call Manager Source: https://github.com/1kastner/conflowgen/blob/main/examples/Jupyter_Notebook/output_data_inspection/visuals_for_ldic2022_paper.ipynb Imports and adds barge information to the port call manager. It includes checks for arrival dates relative to the container flow start and end dates, and verifies if the barge already exists in the schedule. The moved capacity is a rounded random value between 30% and 60% of the total capacity. ```python if vessel_arrives_at_as_datetime_type.date() < container_flow_start_date: logger.info(f"Skipping barge '{barge_vehicle_name}' because it arrives before the start") continue if vessel_arrives_at_as_datetime_type.date() > container_flow_end_date: logger.info(f"Skipping barge '{barge_vehicle_name}' because it arrives after the end") continue if port_call_manager.has_schedule(barge_vehicle_name, vehicle_type=ModeOfTransport.barge): logger.info(f"Skipping barge '{barge_vehicle_name}' because it already exists") continue logger.info(f"Add barge '{barge_vehicle_name}' to database") moved_capacity = int(round(capacity * seeded_random.uniform(0.3, 0.6))) port_call_manager.add_vehicle( vehicle_type=ModeOfTransport.barge, service_name=barge_vehicle_name, vehicle_arrives_at=vessel_arrives_at_as_datetime_type.date(), vehicle_arrives_at_time=vessel_arrives_at_as_datetime_type.time(), average_vehicle_capacity=capacity, average_moved_capacity=moved_capacity, vehicle_arrives_every_k_days=-1 ) ``` -------------------------------- ### Initialize Conflowgen logger and database chooser Source: https://github.com/1kastner/conflowgen/blob/main/conflowgen/tests/notebooks/compare_truck_arrival_distribution_with_results.ipynb Sets up the logger to save logs to a specified directory and initializes the database chooser to manage SQLite databases. ```python conflowgen.setup_logger( logging_directory="./data/logger", # use subdirectory relative to Jupyter Notebook ) database_chooser = conflowgen.DatabaseChooser( sqlite_databases_directory="../../data/databases" # use subdirectory relative to Jupyter Notebook ) ```