File size: 6,506 Bytes
a16f583 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
import os
from pathlib import Path
import re
import glob
class RasterTensorDatasetMapping(Dataset):
def __init__(self, base_path):
"""
Initialize the dataset
Parameters:
base_path: str, base path to RasterTensorData directory
subfolder: str, name of the subfolder (e.g., 'Elevation')
"""
# Replace "OC_LUCAS_LFU_LfL_Coordinates" with "RasterTensorData" in the base path
self.base_path = Path(base_path.replace("Coordinates1Mil", "RasterTensorData"))
self.folder_path = self.base_path
# Create ID to filename mapping
self.id_to_file = self._create_id_mapping()
# Load all numpy arrays into memory (optional, can be modified to load on demand)
self.data_cache = {}
for id_num, filepath in self.id_to_file.items():
self.data_cache[id_num] = np.load(filepath)
def _create_id_mapping(self):
"""Create a dictionary mapping IDs to their corresponding file paths"""
id_to_file = {}
for file_path in self.folder_path.glob("*.npy"):
# Extract ID number from filename
match = re.search(r'ID(\d+)N', file_path.name)
if match:
id_num = int(match.group(1))
id_to_file[id_num] = file_path
return id_to_file
def get_tensor_by_location(self, id_num, x, y, window_size=17):
"""
Get a window_size x window_size square around the specified x,y coordinates
Parameters:
id_num: int, ID number from filename
x: int, x coordinate
y: int, y coordinate
window_size: int, size of the square window (default 17)
Returns:
torch.Tensor: window_size x window_size tensor
"""
if id_num not in self.id_to_file:
raise ValueError(f"ID {id_num} not found in dataset")
# Get the data array
if id_num in self.data_cache:
data = self.data_cache[id_num]
else:
data = np.load(self.id_to_file[id_num])
# Calculate window boundaries
half_window = window_size // 2
x_start = int(max(0, x - half_window))
x_end = int(min(data.shape[0], x + half_window + 1))
y_start = int(max(0, y - half_window))
y_end = int(min(data.shape[1], y + half_window + 1))
# Extract window
window = data[x_start:x_end, y_start:y_end]
# Pad if necessary
if window.shape != (window_size, window_size):
padded_window = np.zeros((window_size, window_size))
x_offset = half_window - (x - x_start)
y_offset = half_window - (y - y_start)
padded_window[
x_offset:x_offset+window.shape[0],
y_offset:y_offset+window.shape[1]
] = window
window = padded_window
return torch.from_numpy(window).float()
def __len__(self):
return len(self.id_to_file)
def __getitem__(self, idx):
# This is a placeholder implementation
# Modify according to your specific needs
id_num = list(self.id_to_file.keys())[idx]
return self.data_cache[id_num]
# Example usage:
"""
# Initialize the dataset
base_path = "/content/drive/MyDrive/Colab Notebooks/MappingSOC/Data/RasterTensorData"
dataset = RasterTensorDataset(base_path, "Elevation")
# Get the dictionary mapping IDs to filenames
id_mapping = dataset.id_to_file
print("ID to filename mapping:", id_mapping)
# Get a 17x17 window for a specific location
id_num = 10 # example ID
x, y = 100, 100 # example coordinates
window = dataset.get_tensor_by_location(id_num, x, y)
print("Window shape:", window.shape)
# Create a DataLoader if needed
from torch.utils.data import DataLoader
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
"""
class MultiRasterDatasetMapping(Dataset):
def __init__(self, subfolders, dataframe):
"""
Parameters:
subfolders: list of str, names of subfolders to include
dataframe: pandas.DataFrame, contains columns GPS_LONG, GPS_LAT, and OC (target variable)
"""
self.subfolders = subfolders
self.dataframe = dataframe
self.datasets = {
subfolder: RasterTensorDatasetMapping(subfolder)
for subfolder in subfolders
}
self.coordinates = {
subfolder: np.load(f"{subfolder}/coordinates.npy")
for subfolder in subfolders
}
def find_coordinates_index(self, subfolder, longitude, latitude):
"""
Finds the index of the matching coordinates in the subfolder's coordinates.npy file.
Parameters:
subfolder: str, name of the subfolder
longitude: float, longitude to match
latitude: float, latitude to match
Returns:
tuple: (id_num, x, y) if match is found, otherwise raises an error
"""
coords = self.coordinates[subfolder]
# Assuming the first two columns of `coordinates.npy` are longitude and latitude
match = np.where((coords[:, 1] == longitude) & (coords[:, 0] == latitude))[0]
if match.size == 0:
raise ValueError(f"Coordinates ({longitude}, {latitude}) not found in {subfolder}")
# Return id_num, x, y from the same row
return coords[match[0], 2], coords[match[0], 3], coords[match[0], 4]
def __getitem__(self, index):
"""
Retrieve tensor and target value for a given index.
Parameters:
index: int, index of the row in the dataframe
Returns:
tuple: (tensor, OC), where tensor is the data and OC is the target variable
"""
row = self.dataframe.iloc[index]
longitude, latitude = row["longitude"], row["latitude"]
tensors = {}
for subfolder in self.subfolders:
id_num, x, y = self.find_coordinates_index(subfolder, longitude, latitude)
tensors[subfolder] = self.datasets[subfolder].get_tensor_by_location(id_num, x, y)
return longitude, latitude, tensors
def __len__(self):
"""
Return the number of samples in the dataset.
"""
return len(self.dataframe)
def get_tensor_by_location(self, subfolder, id_num, x, y):
"""Get tensor from specific subfolder dataset"""
return self.datasets[subfolder].get_tensor_by_location(id_num, x, y)
|