<commit_before>import gdal
import numpy as np
from typing import List, Tuple
from osgeo import osr


class GeoData:
    """Container for geo-referenced raster data stored in a structured numpy array."""

    def __init__(self, array, x_offset, y_offset, cell_width, cell_height):
        assert cell_width > 0 and cell_height > 0, 'Origin must be on left-bottom'
        self.data = array
        self.x_offset = x_offset
        self.y_offset = y_offset
        self.cell_width = cell_width
        self.cell_height = cell_height
        projection = osr.SpatialReference()
        projection.ImportFromEPSG(2154)  # EPSG code for RGF93 / Lambert-93 projection
        self.projection = projection

    def __contains__(self, coordinates):
        (x, y) = coordinates
        x_lim_low = self.x_offset - self.cell_width/2
        x_lim_up = self.x_offset + (self.data.shape[0] + .5) * self.cell_width
        y_lim_low = self.y_offset - self.cell_height/2
        y_lim_up = self.y_offset + (self.data.shape[1] + .5) * self.cell_height

        return x_lim_low <= x <= x_lim_up and y_lim_low <= y <= y_lim_up

    def __repr__(self):
        return self.data.__repr__()

    def subset(self, area: Tuple[Tuple[float, float], Tuple[float, float]]) -> 'GeoData':
        ((x_min, x_max), (y_min, y_max)) = area
        (xi_min, yi_min) = self.array_index((x_min, y_min))
        (xi_max, yi_max) = self.array_index((x_max, y_max))
        ary = self.data[xi_min:xi_max+1, yi_min:yi_max+1]
        return GeoData(ary, *self.coordinates((xi_min, yi_min)), self.cell_width, self.cell_height)

    def array_index(self, coordinates: Tuple[float, float]) -> Tuple[int, int]:
        (x, y) = coordinates
        xi = int(round((x - self.x_offset) / self.cell_width))
        yi = int(round((y - self.y_offset) / self.cell_height))
        return xi, yi

    def coordinates(self, indices: Tuple[int, int]) -> Tuple[float, float]:
        (xi, yi) = indices
        x = self.x_offset + xi * self.cell_width
        y = self.y_offset + yi * self.cell_height
        return x, y

    def append_right(self, other: 'GeoData') -> 'GeoData':
        assert self.data.dtype == other.data.dtype<commit_msg>[geodata] Implement GeoData __getitem__ as access to internal data<commit_after>import gdal
import numpy as np
from typing import List, Tuple
from osgeo import osr


class GeoData:
    """Container for geo-referenced raster data stored in a structured numpy array."""

    def __init__(self, array, x_offset, y_offset, cell_width, cell_height):
        assert cell_width > 0 and cell_height > 0, 'Origin must be on left-bottom'
        self.data = array
        self.x_offset = x_offset
        self.y_offset = y_offset
        self.cell_width = cell_width
        self.cell_height = cell_height
        projection = osr.SpatialReference()
        projection.ImportFromEPSG(2154)  # EPSG code for RGF93 / Lambert-93 projection
        self.projection = projection

    def __contains__(self, coordinates):
        (x, y) = coordinates
        x_lim_low = self.x_offset - self.cell_width/2
        x_lim_up = self.x_offset + (self.data.shape[0] + .5) * self.cell_width
        y_lim_low = self.y_offset - self.cell_height/2
        y_lim_up = self.y_offset + (self.data.shape[1] + .5) * self.cell_height

        return x_lim_low <= x <= x_lim_up and y_lim_low <= y <= y_lim_up

    def __repr__(self):
        return self.data.__repr__()

    def __getitem__(self, item):
        return self.data

    def subset(self, area: Tuple[Tuple[float, float], Tuple[float, float]]) -> 'GeoData':
        ((x_min, x_max), (y_min, y_max)) = area
        (xi_min, yi_min) = self.array_index((x_min, y_min))
        (xi_max, yi_max) = self.array_index((x_max, y_max))
        ary = self.data[xi_min:xi_max+1, yi_min:yi_max+1]
        return GeoData(ary, *self.coordinates((xi_min, yi_min)), self.cell_width, self.cell_height)

    def array_index(self, coordinates: Tuple[float, float]) -> Tuple[int, int]:
        (x, y) = coordinates
        xi = int(round((x - self.x_offset) / self.cell_width))
        yi = int(round((y - self.y_offset) / self.cell_height))
        return xi, yi

    def coordinates(self, indices: Tuple[int, int]) -> Tuple[float, float]:
        (xi, yi) = indices
        x = self.x_offset + xi * self.cell_width
        y = self.y_offset + yi * self.cell_height
        return x, y

    def append_right(self, other: 'GeoData') -> 'GeoData':
        assert self.data.dtype == other.data.dtype
{'fraction_non_alphanumeric': 0.11888888888888889, 'fraction_numerical': 0.008, 'mean_word_length': 2.8339011925042588, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '92445', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1549, 'n_words': 374}