Skip to content

Commit

Permalink
experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Dec 17, 2024
1 parent 6e6e414 commit 12dd4e8
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 17 deletions.
16 changes: 5 additions & 11 deletions jord/networkx_utilities/construction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Mapping

import shapely
from networkx import MultiDiGraph
from networkx import MultiDiGraph, MultiGraph

__all__ = [
"assertive_add_edge",
Expand All @@ -18,7 +18,7 @@ class IllegalDuplicateEdgeException(Exception): ...


def assertive_add_edge(
graph: MultiDiGraph,
graph: MultiGraph,
u: int,
v: int,
uniqueid: int,
Expand Down Expand Up @@ -48,7 +48,6 @@ def assertive_add_edge(
if not allow_loops:
if u == v:
raise IllegalLoopException(f"{u} == {v}")
# assert u != v, f"{u} == {v}"

assert isinstance(u, int)
assert isinstance(v, int)
Expand All @@ -61,14 +60,11 @@ def assertive_add_edge(
raise IllegalDuplicateEdgeException(
f"Graph already contains the edge ({u} -> {v}) with {uniqueid=}"
)
# assert not graph.has_edge(u, v, uniqueid)

graph.add_edge(u, v, key=uniqueid, uniqueid=uniqueid, **attributes)


def add_shapely_node(
graph: MultiDiGraph, u: int, point: shapely.Point, **kwargs
) -> None:
def add_shapely_node(graph: MultiGraph, u: int, point: shapely.Point, **kwargs) -> None:
"""
Add a shapely point based node to the graph.
Expand All @@ -85,9 +81,7 @@ def add_shapely_node(

graph.add_node(
u,
lon=point.x,
lat=point.y,
x=point.x,
y=point.y,
x=float(point.x),
y=float(point.y),
**kwargs,
)
Empty file.
337 changes: 337 additions & 0 deletions jord/rasterio_utilities/instance_sampling.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\n",
"\n",
"from shapely.geometry import Point\n",
"from torch.utils.data import Dataset\n",
"\n",
"class ShapelyFeatureDataset(Dataset):\n",
"\n",
" def __init__(self, features, transform=None):\n",
" self.features = features\n",
" self.transform = transform\n",
"\n",
" def __len__(self):\n",
" return len(self.features)\n",
"\n",
" def __getitem__(self, idx):\n",
" feature = self.features[idx]\n",
"\n",
" if self.transform:\n",
" feature = self.transform(feature)\n",
"\n",
" raster = self.feature_to_raster(feature)\n",
" return raster\n",
"\n",
" def feature_to_raster(self, feature):\n",
" # Create a blank raster\n",
" raster = np.zeros((256, 256), dtype=np.uint8)\n",
" # Convert feature to raster\n",
" coords = np.array(feature.exterior.coords)\n",
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n",
" return raster\n",
"\n",
"class RandomTransform:\n",
"\n",
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n",
" self.scale_range = scale_range\n",
" self.rotation_range = rotation_range\n",
" self.noise_level = noise_level\n",
"\n",
" def __call__(self, feature):\n",
" # Random scale\n",
" scale_factor = random.uniform(*self.scale_range)\n",
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n",
"\n",
" # Random rotation\n",
" angle = random.uniform(*self.rotation_range)\n",
" feature = rotate(feature, angle, origin='center')\n",
"\n",
" # Add noise\n",
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n",
" noisy_coords = feature.exterior.coords + noise\n",
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n",
"\n",
" return feature\n",
"\n",
"# Example usage\n",
"features = [Point(128, 128).buffer(50)] # Example Shapely features\n",
"transform = RandomTransform()\n",
"dataset = ShapelyFeatureDataset(features, transform=transform)\n",
"\n",
"# Get a sample\n",
"sample = dataset[0]\n",
"print(sample)"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"\n",
"from shapely.geometry import Point\n",
"from torch.utils.data import Dataset\n",
"\n",
"class ShapelyFeatureDataset(Dataset):\n",
"\n",
" def __init__(self, features, transform=None, subset_size=5):\n",
" self.features = features\n",
" self.transform = transform\n",
" self.subset_size = subset_size\n",
"\n",
" def __len__(self):\n",
" return len(self.features)\n",
"\n",
" def __getitem__(self, idx):\n",
" subset = self.sample_subset()\n",
" rasters, masks = self.features_to_rasters(subset)\n",
" return rasters, masks\n",
"\n",
" def sample_subset(self):\n",
" return random.sample(self.features, self.subset_size)\n",
"\n",
" def features_to_rasters(self, features):\n",
" rasters = []\n",
" masks = []\n",
" placed_features = []\n",
" for feature in features:\n",
" if self.transform:\n",
" feature = self.transform(feature)\n",
" feature = self.ensure_no_overlap(feature, placed_features)\n",
" raster, mask = self.feature_to_raster(feature)\n",
" rasters.append(raster)\n",
" masks.append(mask)\n",
" placed_features.append(feature)\n",
" return rasters, masks\n",
"\n",
" def ensure_no_overlap(self, feature, placed_features):\n",
" minx, miny, maxx, maxy = feature.bounds\n",
" width, height = maxx - minx, maxy - miny\n",
" while True:\n",
" x_offset = random.randint(0, 256 - int(width))\n",
" y_offset = random.randint(0, 256 - int(height))\n",
" translated_feature = translate(feature, xoff=x_offset - minx, yoff=y_offset - miny)\n",
" if not any(translated_feature.intersects(pf) for pf in placed_features):\n",
" return translated_feature\n",
"\n",
" def feature_to_raster(self, feature):\n",
" # Create a blank raster and mask\n",
" raster = np.zeros((256, 256), dtype=np.uint8)\n",
" mask = np.zeros((256, 256), dtype=np.uint8)\n",
"\n",
" # Convert feature to raster\n",
" coords = np.array(feature.exterior.coords)\n",
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n",
" cv2.fillPoly(mask, [coords.astype(np.int32)], 1)\n",
" return raster, mask\n",
"\n",
"class RandomTransform:\n",
"\n",
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n",
" self.scale_range = scale_range\n",
" self.rotation_range = rotation_range\n",
" self.noise_level = noise_level\n",
"\n",
" def __call__(self, feature):\n",
" # Random scale\n",
" scale_factor = random.uniform(*self.scale_range)\n",
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n",
"\n",
" # Random rotation\n",
" angle = random.uniform(*self.rotation_range)\n",
" feature = rotate(feature, angle, origin='center')\n",
"\n",
" # Add noise\n",
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n",
" noisy_coords = feature.exterior.coords + noise\n",
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n",
"\n",
" return feature\n",
"\n",
"# Example usage\n",
"features = [Point(128, 128).buffer(50) for _ in range(10)] # Example Shapely features\n",
"transform = RandomTransform()\n",
"dataset = ShapelyFeatureDataset(features, transform=transform, subset_size=5)\n",
"\n",
"# Get a sample\n",
"rasters, masks = dataset[0]\n",
"for i, (raster, mask) in enumerate(zip(rasters, masks)):\n",
" print(f\"Raster {i}:\\n{raster}\\nMask {i}:\\n{mask}\")\n"
],
"id": "d3e8495b3d58b5bf"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import random\n",
"import cv2\n",
"import numpy as np\n",
"from shapely.affinity import rotate, scale, translate\n",
"from shapely.geometry import Point, shape\n",
"from torch.utils.data import Dataset\n",
"\n",
"class ShapelyFeatureDataset(Dataset):\n",
"\n",
" def __init__(self, categories, transform=None, subset_size=5):\n",
" self.categories = categories\n",
" self.transform = transform\n",
" self.subset_size = subset_size\n",
"\n",
" def __len__(self):\n",
" return sum(len(features) for features in self.categories.values())\n",
"\n",
" def __getitem__(self, idx):\n",
" subset = self.sample_subset()\n",
" rasters, masks = self.features_to_rasters(subset)\n",
" return rasters, masks\n",
"\n",
" def sample_subset(self):\n",
" subset = []\n",
" for category, features in self.categories.items():\n",
" subset.extend(random.sample(features, min(self.subset_size, len(features))))\n",
" return subset\n",
"\n",
" def features_to_rasters(self, features):\n",
" rasters = []\n",
" masks = []\n",
" placed_features = []\n",
" for feature in features:\n",
" if self.transform:\n",
" feature = self.transform(feature)\n",
" feature = self.ensure_no_overlap(feature, placed_features)\n",
" raster, mask = self.feature_to_raster(feature)\n",
" rasters.append(raster)\n",
" masks.append(mask)\n",
" placed_features.append(feature)\n",
" return rasters, masks\n",
"\n",
" def ensure_no_overlap(self, feature, placed_features):\n",
" minx, miny, maxx, maxy = feature.bounds\n",
" width, height = maxx - minx, maxy - miny\n",
" while True:\n",
" x_offset = random.randint(0, 256 - int(width))\n",
" y_offset = random.randint(0, 256 - int(height))\n",
" translated_feature = translate(feature, xoff=x_offset - minx, yoff=y_offset - miny)\n",
" if not any(translated_feature.intersects(pf) for pf in placed_features):\n",
" return translated_feature\n",
"\n",
" def feature_to_raster(self, feature):\n",
" # Create a blank raster and mask\n",
" raster = np.zeros((256, 256), dtype=np.uint8)\n",
" mask = np.zeros((256, 256), dtype=np.uint8)\n",
"\n",
" # Convert feature to raster\n",
" coords = np.array(feature.exterior.coords)\n",
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n",
" cv2.fillPoly(mask, [coords.astype(np.int32)], 1)\n",
" return raster, mask\n",
"\n",
"class RandomTransform:\n",
"\n",
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n",
" self.scale_range = scale_range\n",
" self.rotation_range = rotation_range\n",
" self.noise_level = noise_level\n",
"\n",
" def __call__(self, feature):\n",
" # Random scale\n",
" scale_factor = random.uniform(*self.scale_range)\n",
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n",
"\n",
" # Random rotation\n",
" angle = random.uniform(*self.rotation_range)\n",
" feature = rotate(feature, angle, origin='center')\n",
"\n",
" # Add noise\n",
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n",
" noisy_coords = feature.exterior.coords + noise\n",
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n",
"\n",
" return feature\n",
"\n",
"# Example usage\n",
"categories = {\n",
" 'chairs': [Point(128, 128).buffer(50) for _ in range(3)],\n",
" 'tables': [Point(64, 64).buffer(30) for _ in range(3)],\n",
" 'carpets': [Point(192, 192).buffer(40) for _ in range(3)],\n",
" 'plants': [Point(32, 32).buffer(20) for _ in range(3)],\n",
" 'walls': [Point(160, 160).buffer(60) for _ in range(3)]\n",
" }\n",
"transform = RandomTransform()\n",
"dataset = ShapelyFeatureDataset(categories, transform=transform, subset_size=2)\n",
"\n",
"# Get a sample\n",
"rasters, masks = dataset[0]\n",
"for i, (raster, mask) in enumerate(zip(rasters, masks)):\n",
" print(f\"Raster {i}:\\n{raster}\\nMask {i}:\\n{mask}\")"
],
"id": "16b0d6871c86332d"
},
{
"metadata": {
"jupyter": {
"is_executing": true
}
},
"cell_type": "code",
"source": [
"from matplotlib import pyplot\n",
"\n",
"# Example usage\n",
"features = [Point(128, 128).buffer(50) for _ in range(10)] # Example Shapely features\n",
"transform = RandomTransform()\n",
"dataset = ShapelyFeatureDataset(features, transform=transform, subset_size=5)\n",
"\n",
"# Get a sample\n",
"rasters, masks = dataset[0]\n",
"\n",
"# Visualize the rasters\n",
"fig, axes = pyplot.subplots(1, len(rasters), figsize=(15, 5))\n",
"for i, raster in enumerate(rasters):\n",
" axes[i].imshow(raster, cmap='gray')\n",
" axes[i].set_title(f'Raster {i}')\n",
" axes[i].axis('off')\n",
"\n",
"pyplot.show()\n"
],
"id": "f04530319e7b8596",
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 12dd4e8

Please sign in to comment.