<commit_before>"""Serializer fields"""
from django.contrib.gis import forms
from rest_framework.fields import FileField, WritableField
from greenwich.raster import Raster

from spillway.compat import json


class GeometryField(WritableField):
    type_name = 'GeometryField'
    type_label = 'geometry'
    form_field_class = forms.GeometryField
    # TODO: Introspect available formats from spatial backends.
    _formats = ('geohash', 'geojson', 'gml', 'kml', 'svg')

    def set_default_source(self):
        """Set a default source based on the selected renderer."""
        # Do not override an explicitly provided field source.
        if self.source:
            return
        view = self.context.get('view')
        # Alter the field source based on geometry output format.
        try:
            wants_default_renderer = view.wants_default_renderer()
        except AttributeError:
            wants_default_renderer = True
        # Alter the field source based on geometry output format.
        if not wants_default_renderer:
            renderer = view.request.accepted_renderer
            if renderer.format in self._formats:
                self.source = renderer.format
                # Single objects must use GEOSGeometry attrs to provide formats.
                if not self.parent.many:
                    self.source = '%s.%s' % (self.label, self.source)

    def to_native(self, value):
        # Create a dict from the GEOSGeometry when the value is not previously
        # serialized from the spatial db.
        try:
            return {'type': value.geom_type, 'coordinates': value.coords}
        # Value is already serialized as geojson, kml, etc.
        except AttributeError:
            return value

    def from_native(self, value):
        # forms.GeometryField cannot handle geojson dicts.
        if isinstance(value, dict):
            value = json.dumps(value)
        return super(GeometryField, self).from_native(value)


class NDArrayField(FileField):
    type_name = 'NDArrayField'
    type_label = 'ndarray'

    def to_native(self, value):
        params = self.context.get('params', {})
        geom = params.get('g')
        with Raster(getattr(value, 'path', value)) as r:
            arr = r.clip(geom).masked_array() if geom else r.array()
        return arr.tolist()
<commit_msg>No need for a masked array here<commit_after>"""Serializer fields"""
from django.contrib.gis import forms
from rest_framework.fields import FileField, WritableField
from greenwich.raster import Raster

from spillway.compat import json


class GeometryField(WritableField):
    type_name = 'GeometryField'
    type_label = 'geometry'
    form_field_class = forms.GeometryField
    # TODO: Introspect available formats from spatial backends.
    _formats = ('geohash', 'geojson', 'gml', 'kml', 'svg')

    def set_default_source(self):
        """Set a default source based on the selected renderer."""
        # Do not override an explicitly provided field source.
        if self.source:
            return
        view = self.context.get('view')
        # Alter the field source based on geometry output format.
        try:
            wants_default_renderer = view.wants_default_renderer()
        except AttributeError:
            wants_default_renderer = True
        # Alter the field source based on geometry output format.
        if not wants_default_renderer:
            renderer = view.request.accepted_renderer
            if renderer.format in self._formats:
                self.source = renderer.format
                # Single objects must use GEOSGeometry attrs to provide formats.
                if not self.parent.many:
                    self.source = '%s.%s' % (self.label, self.source)

    def to_native(self, value):
        # Create a dict from the GEOSGeometry when the value is not previously
        # serialized from the spatial db.
        try:
            return {'type': value.geom_type, 'coordinates': value.coords}
        # Value is already serialized as geojson, kml, etc.
        except AttributeError:
            return value

    def from_native(self, value):
        # forms.GeometryField cannot handle geojson dicts.
        if isinstance(value, dict):
            value = json.dumps(value)
        return super(GeometryField, self).from_native(value)


class NDArrayField(FileField):
    type_name = 'NDArrayField'
    type_label = 'ndarray'

    def to_native(self, value):
        params = self.context.get('params', {})
        geom = params.get('g')
        with Raster(getattr(value, 'path', value)) as r:
            if not geom:
                return r.array().tolist()
            with r.clip(geom) as clipped:
                return clipped.array().tolist()

{'fraction_non_alphanumeric': 0.08585858585858586, 'fraction_numerical': 0.0, 'mean_word_length': 2.4592430858806407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7347376', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1272, 'n_words': 405}