Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Last active December 5, 2024 10:17
Show Gist options
  • Save mikkohei13/ee0d6a91649a1978119ec78ddad24766 to your computer and use it in GitHub Desktop.
Save mikkohei13/ee0d6a91649a1978119ec78ddad24766 to your computer and use it in GitHub Desktop.
Generate a line graph from a FinBIF API json data file aggregated by year and month
# Script to read FinBIF API json data file aggregated by year and month, and to generate line graph.
# Get data e.g. at
# https://api.laji.fi/v0/warehouse/query/unit/aggregate?aggregateBy=gathering.conversions.month%2Cgathering.conversions.year&onlyCount=true&taxonCounts=false&gatheringCounts=false&pairCounts=false&atlasCounts=false&excludeNulls=true&pessimisticDateRangeHandling=false&pageSize=1000&page=1&cache=false&useIdentificationAnnotations=true&includeSubTaxa=true&includeNonValidTaxa=true&time=2000%2F2024&collectionId=HR.3551&individualCountMin=1&qualityIssues=NO_ISSUES&access_token=TOKEN
import json
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
'''
# Load JSON data from the file
file_path = 'inaturalist-year-moth-2015-2024.json'
file_path = 'winterbird-year-moth-2020-2024.json'
file_path = 'ebird-year-moth-2015-2023.json'
file_path = 'tipu-year-moth-2020-2024.json'
file_path = 'kastikka-ark-year-moth-2020-2024.json'
file_path = 'mh-year-moth-2020-2024.json'
file_path = 'vihko-year-moth-2020-2024.json'
file_path = 'hyonteistietokanta-year-moth-2020-2024.json'
file_path = 'all-year-moth-2020-2024.json'
output_file = file_path.replace('.json', '.png')
with open(file_path, 'r') as file:
data = json.load(file)
'''
# Load data from API
import requests
TOKEN = "ADD YOUR TOKEN HERE"
collection_id = "HR.2951"
url = f"https://api.laji.fi/v0/warehouse/query/unit/aggregate?aggregateBy=gathering.conversions.month%2Cgathering.conversions.year&onlyCount=true&taxonCounts=false&gatheringCounts=false&pairCounts=false&atlasCounts=false&excludeNulls=true&pessimisticDateRangeHandling=false&pageSize=1000&page=1&cache=false&useIdentificationAnnotations=true&includeSubTaxa=true&includeNonValidTaxa=true&time=2000%2F2024&collectionId={ collection_id }&individualCountMin=1&qualityIssues=NO_ISSUES&access_token={ TOKEN }"
output_file = "LoLIFE-year-month-2020-2024.png"
# Fetch data from URL
response = requests.get(url)
data = response.json()
# Extract the relevant results
results = data['results']
# Prepare the data for visualization
records = defaultdict(lambda: 0) # Default count for missing months is 0
# Extract the relevant results and populate records
for item in results:
year = item['aggregateBy']['gathering.conversions.year']
month = item['aggregateBy']['gathering.conversions.month']
count = item['count']
if month: # Exclude records with empty month
records[(year, month.zfill(2))] = count # Populate with actual count values
# Generate a full list of months for each year in the dataset
years = {item['aggregateBy']['gathering.conversions.year'] for item in results}
complete_data = []
for year in years:
if not year:
print("Skipping empty year")
continue
for month in range(1, 13): # Ensure months 1-12 are included
if not month:
print("Skipping empty month")
continue
month_str = str(month).zfill(2)
count = records[(year, month_str)] # Retrieve from populated records
complete_data.append({'year_month': f"{year}-{month_str}", 'count': count})
# Convert to DataFrame
df = pd.DataFrame(complete_data)
# Sort by year_month
df['year_month'] = pd.to_datetime(df['year_month'])
df.sort_values(by='year_month', inplace=True)
# Plot the data
plt.figure(figsize=(12, 6))
plt.plot(df['year_month'], df['count'])
plt.title(output_file)
plt.xlabel('Year-Month')
plt.ylabel('Count')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
# Save the plot
plt.savefig(output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment