Skip to content

Commit

Permalink
Update with plot class for eddmaps.py
Browse files Browse the repository at this point in the history
  • Loading branch information
preethatr07 authored Dec 17, 2024
1 parent 60ba6cb commit 6d83e43
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion torchgeo/datasets/eddmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import pandas as pd
from rasterio.crs import CRS

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from typing import Tuple, Optional

Check failure on line 16 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

torchgeo/datasets/eddmaps.py:16:1: UP035 `typing.Tuple` is deprecated, use `tuple` instead

from .errors import DatasetNotFoundError
from .geo import GeoDataset
from .utils import BoundingBox, Path, disambiguate_timestamp
Expand Down Expand Up @@ -79,7 +83,7 @@ def __init__(self, root: Path = 'data') -> None:
coords = (x, x, y, y, mint, maxt)
self.index.insert(i, coords)
i += 1

def __getitem__(self, query: BoundingBox) -> dict[str, Any]:
"""Retrieve metadata indexed by query.
Expand All @@ -103,3 +107,104 @@ def __getitem__(self, query: BoundingBox) -> dict[str, Any]:
sample = {'crs': self.crs, 'bounds': bboxes}

return sample


def plot(
self,

Check failure on line 113 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ANN001)

torchgeo/datasets/eddmaps.py:113:5: ANN001 Missing type annotation for function argument `self`
query: Optional[BoundingBox] = None,

Check failure on line 114 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP007)

torchgeo/datasets/eddmaps.py:114:12: UP007 Use `X | Y` for type annotations
title: str = "EDDMapS Dataset",
point_size: int = 20,
point_color: str = 'blue',
query_color: str = 'red',
annotate: bool = False,
figsize: Tuple[int, int] = (12, 8)

Check failure on line 120 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

torchgeo/datasets/eddmaps.py:120:14: UP006 Use `tuple` instead of `Tuple` for type annotation
) -> None:

""" Plot the dataset points with optional query bounding box
Args:
query: The query to look for points, in the form of a bounding box: (minx,maxx,miny,maxy,mint,maxt)
title: Title of the plot
point_size: The size of the points plotted
point_color: The default color of the points, in case no such map is provided
query_color: color for the points which fall into the query
annotate: Controls if the points with timestamps are annotated
figsize: Size of drawn figure in the shape: (width, height)
Raises:
ValueError: When no points could be plotted because none were valid.
"""

Check failure on line 138 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D201)

torchgeo/datasets/eddmaps.py:123:5: D201 No blank lines allowed before function docstring (found 1)

Check failure on line 138 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D202)

torchgeo/datasets/eddmaps.py:123:5: D202 No blank lines allowed after function docstring (found 1)

Check failure on line 138 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D205)

torchgeo/datasets/eddmaps.py:123:5: D205 1 blank line required between summary line and description

Check failure on line 138 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D210)

torchgeo/datasets/eddmaps.py:123:5: D210 No whitespaces allowed surrounding docstring text

Check failure on line 138 in torchgeo/datasets/eddmaps.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (D415)

torchgeo/datasets/eddmaps.py:123:5: D415 First line should end with a period, question mark, or exclamation point

# Filtering valid lat and long rows
valid_data = self.data.dropna(subset = [ 'Latitude' , 'Longitude'])
if valid_data.empty:
raise ValueError("No valid lat/long data to plot.")

fig, ax = plt.subplots(figsize=figsize)

# Plot-at-all points

ax.scatter(

valid_data['Longitude'],

valid_data['Latitude'],

s = point_size,

c = point_color,

edgecolor = 'k',

alpha = 0.6,

label = 'All Observations'

)

#highlighting queried points (If) provided bounding box query

if query:
minx, maxx, miny, maxy, mint, maxt = query
hits = self.index.intersection((minx,maxx,miny,maxy,mint, maxt))

# Get coordinates of hits to highlight
query_points = valid_data.iloc[[list(hits)]]
ax.scatter(
query_points['Longitude'],
query_points['Latitude'],
s = point_size * 1.5,
c = query_color,
edgecolor = 'white',
alpha = 0.8,
label = 'Query Results'
)

# Draw a bounding box
bbox_patch = patches.rectangle(
(minx, miny), maxx - minx, maxy - miny,
linewidth = 2, edgecolor = 'red', facecolor='none', linestyle = '--', label = "Query Bounding Box"
)
ax.add_patch(bbox_patch)

# Optional annotations
if annotate:
for _, row in valid_data.iterrows():
ax.annotate(
row['ObsDate'], (row['Longitude'], row['Latitude']),
fontsize=8, alpha=0.7, textcoords="offset points", xytext=(0, 5), ha='center'
)

# Plot styling
ax.set_title(title, fontsize=14)
ax.set_xlabel("Longitude", fontsize=12)
ax.set_ylabel("Latitude", fontsize=12)
ax.grid(True, linestyle='--', alpha=0.5)
ax.legend()

plt.show()



0 comments on commit 6d83e43

Please sign in to comment.