Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
cafriedb committed Sep 5, 2024
1 parent 6f25250 commit 6e7b4df
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
69 changes: 68 additions & 1 deletion dev/filter_sectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from openpyxl.chart import BarChart, Reference
import copy


def process_yaml_files(files_dict, database):
'''
- Runs through the files_dict reading the defined filters in the yaml files.
Expand Down Expand Up @@ -45,4 +46,70 @@ def process_yaml_files(files_dict, database):
# Add to the sectors_dict
main_dict[key]['activities'] = activities_list

return main_dict
return main_dict

# To calculate the median

database_name=ei39SSP2

# Initialize a results dictionary to store exchange data for each key
results = {}

# Iterate over each key in the premise_dict
for key, value in premise_dict.items():
# Dictionary to store exchange data for each activity under the current key
activities_data = {}

try:
# Get the list of activities for the current key
activities_list = premise_dict[key]['activities'][:3]
except KeyError:
print(f"KeyError: 'activities' not found for key: {key}")
continue

# Check if there are activities to process
if not activities_list:
print(f"No activities found for key: {key}")
continue

# Iterate over each activity in the activities_list
for activity in activities_list:
# Initialize the counter and amounts list for this activity
exchange_count = 0
exchange_amounts = []

# Retrieve the unique identifier for the activity (e.g., activity.key)
activity_key = activity.key # Adjust this line to match the correct attribute for key

# Loop through all activities in the database
for act in database_name: # Replace 'database_name' with your actual database object
# Loop through all exchanges in the current activity of the database
for exc in act.exchanges():
# Compare unique keys of exchange input and activity
if exc.input.key == activity_key: # Ensure we compare keys, not objects
exchange_count += 1
# Store the amount of the exchange
exchange_amounts.append(exc['amount'])

# Calculate the median of the exchange amounts if there are any
median_value = np.median(exchange_amounts) if exchange_amounts else None

# Store the exchange data for the current activity
activities_data[str(activity)] = {
"exchange_count": exchange_count,
"exchange_amounts": exchange_amounts,
"median_exchange_amount": median_value
}

# Store the activities data in the results dictionary under the current key
results[key] = activities_data

# Print or process the results as needed
for key, activity_data in results.items():
print(f"Results for key: {key}")
for activity, data in activity_data.items():
print(f" Activity: {activity}")
print(f" Total Exchanges: {data['exchange_count']}")
print(f" Median Exchange Amount: {data['median_exchange_amount']}")
print(f" Exchange Amounts: {data['exchange_amounts']}")
print("-" * 40)
2 changes: 1 addition & 1 deletion dopo/sector_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from dopo import generate_sets_from_filters
import cop<y
import copy
import numpy as np

def process_yaml_files(files_dict, database):
Expand Down

0 comments on commit 6e7b4df

Please sign in to comment.