<commit_before>from RequestHandler import RequestHandler
from QueryLayer import InfoQuery
from QueryLayer import DataQuery
from urllib2 import URLError
import numpy as np

class API(RequestHandler):
    """ Responds to :data:`bfly.Webserver._webapp` /api endpoint

    Attributes
    -----------
    inherits: :class:`RequestHandler`


    :h:`Methods`

    """
    def parse(self, request):
        """ Extract details from any of the methods
        Overrides :meth:`Database.parse`

        Calls :meth:`_meta_info`, :meth:`_feature_info`, \
:meth:`_get_group`, or :meth:`_get_data` to return \
an :class:`InfoQuery` or :class:`DataQuery` as a \
response to the given ``method``

        Arguments
        ----------
        request: str
            The single method requested in the URL

        Returns
        ---------
        :class:`QueryLayer.Query`
            contains standard details for each request
        """

        super(API, self).parse(request)

        meths = self.INPUT.METHODS.LIST
        command = self._match_list('method', request, meths)

        if command == self.INPUT.METHODS.META.NAME:
            return self._meta_info
        if command == self.INPUT.METHODS.FEAT.NAME:
            return self._feature_info
        # Handle all methods the same if in same list
        if command in self.INPUT.METHODS.GROUP_LIST:
            return self._get_group(command)
        if command in self.INPUT.METHODS.IMAGE_LIST:
            return self.get_data(command)

        return 'Unsupported Request Category'

    @property
    def _meta_info(self):
        """ Loads :class:`InfoQuery` for ``INPUT.METHODS.META``

        Returns
        --------
        :class:`InfoQuery`
            made with info from :meth:`_get_group_dict`
        """
        # Get needed metadata
        info = self.OUTPUT.INFO
        in_info = self.INPUT.INFO
        methods = self.INPUT.METHODS
        # Get all the channel info
        meta_info = self._get_group_dict('')

        # The input format becomes the output format
        out_format = self._get_list_query(in_info.FORMAT)

        return InfoQuery(**{
            info.CHANNEL.NAME: meta_info[info.CHANNEL.NAME],
            info.PATH.NAME: meta_info[info.PATH.NAME],
            methods.NAME: methods.META.NAME,
            in_info.FORMAT.NAME: out_format
        })

    @property
    def _feature_info(self):
        """ Loads :class:`InfoQuery` for ``INPUT.METHODS.FEATURE``

        Returns
        --------
        :class:`InfoQuery`
            with a feature passed as `OUTPUT.INFO.NAMES` \
from :meth:`_id_feature` or :meth:`_box_feature`
        """
        # Get needed metadata
        info = self.OUTPUT.INFO
        in_info = self.INPUT.INFO
        methods = self.INPUT.METHODS
        feats = self.INPUT.FEATURES
        # Create empty parameters
        id_key = None
        all_queries = []
        target_bounds = []

        # Get all the channel info
        meta_info = self._get_group_dict('')
        # The path and the  output format
        path = meta_info[info.PATH.NAME]
        out_format = self._get_list_query(in_info.FORMAT)
        # Get the name of the feature to load from db
        feat = self._get_list_query(feats)

        # All features that need id
        if feat in feats.ID_LIST:
            # get the id
            id_key = self._get_int_query(in_info.ID)
            # Return names based on id
            names = self._id_feature(feat, path, id_key)
        # All features that need bounds and id
        elif feat in feats.ID_BOX_LIST:
            # get the id
            id_key = self._get_int_query(in_info.ID)
            # get resolution from input
            res_xy = self.INPUT.RESOLUTION.XY
            resolution = self._get_int_query(res_xy)
            # get bounds from input
            for key in ['Z','Y','X','DEPTH','HEIGHT','WIDTH']:
                term = getattr(self.INPUT.POSITION, key)
                target_bounds.append(self._get_int_query(term))
            # scale the bounds from resolution
            scale = 2**resolution
            scales = np.tile([1.0/scale ,scale, scale], 2)
            bounds = (np.array(target_bounds) * scales).astype(np.uint32)
            # Return names based on bounds
            names = self._id_box_feature(feat, path, id_key, bounds)
        # All features that need bounds
        elif feat in feats.BOX_LIST:
            # get resolution from input
            res_xy = self.INPUT.RESOLUTION.XY
            resolution = self._get_int_query(res_xy)
            # get bounds from input
            for key in ['Z','Y','X','DEPTH','HEIGHT','WIDTH']:
                term = getattr(self.INPUT.POSITION, key)
                target_bounds.append(self._get_int_query(term))
            # scale the bounds from resolution
            scale = 2**resolution
            scales = np.tile([1.0/scale ,scale, scale], 2)
            bounds = (np.array(target_bounds) * scales).astype(np.uint32)
            # Return names based on bounds
            names = self._box_feature(feat, path, bounds)<commit_msg>simplify z coordinate scales to match NDA update<commit_after>from RequestHandler import RequestHandler
from QueryLayer import InfoQuery
from QueryLayer import DataQuery
from urllib2 import URLError
import numpy as np

class API(RequestHandler):
    """ Responds to :data:`bfly.Webserver._webapp` /api endpoint

    Attributes
    -----------
    inherits: :class:`RequestHandler`


    :h:`Methods`

    """
    def parse(self, request):
        """ Extract details from any of the methods
        Overrides :meth:`Database.parse`

        Calls :meth:`_meta_info`, :meth:`_feature_info`, \
:meth:`_get_group`, or :meth:`_get_data` to return \
an :class:`InfoQuery` or :class:`DataQuery` as a \
response to the given ``method``

        Arguments
        ----------
        request: str
            The single method requested in the URL

        Returns
        ---------
        :class:`QueryLayer.Query`
            contains standard details for each request
        """

        super(API, self).parse(request)

        meths = self.INPUT.METHODS.LIST
        command = self._match_list('method', request, meths)

        if command == self.INPUT.METHODS.META.NAME:
            return self._meta_info
        if command == self.INPUT.METHODS.FEAT.NAME:
            return self._feature_info
        # Handle all methods the same if in same list
        if command in self.INPUT.METHODS.GROUP_LIST:
            return self._get_group(command)
        if command in self.INPUT.METHODS.IMAGE_LIST:
            return self.get_data(command)

        return 'Unsupported Request Category'

    @property
    def _meta_info(self):
        """ Loads :class:`InfoQuery` for ``INPUT.METHODS.META``

        Returns
        --------
        :class:`InfoQuery`
            made with info from :meth:`_get_group_dict`
        """
        # Get needed metadata
        info = self.OUTPUT.INFO
        in_info = self.INPUT.INFO
        methods = self.INPUT.METHODS
        # Get all the channel info
        meta_info = self._get_group_dict('')

        # The input format becomes the output format
        out_format = self._get_list_query(in_info.FORMAT)

        return InfoQuery(**{
            info.CHANNEL.NAME: meta_info[info.CHANNEL.NAME],
            info.PATH.NAME: meta_info[info.PATH.NAME],
            methods.NAME: methods.META.NAME,
            in_info.FORMAT.NAME: out_format
        })

    @property
    def _feature_info(self):
        """ Loads :class:`InfoQuery` for ``INPUT.METHODS.FEATURE``

        Returns
        --------
        :class:`InfoQuery`
            with a feature passed as `OUTPUT.INFO.NAMES` \
from :meth:`_id_feature` or :meth:`_box_feature`
        """
        # Get needed metadata
        info = self.OUTPUT.INFO
        in_info = self.INPUT.INFO
        methods = self.INPUT.METHODS
        feats = self.INPUT.FEATURES
        # Create empty parameters
        id_key = None
        all_queries = []
        target_bounds = []

        # Get all the channel info
        meta_info = self._get_group_dict('')
        # The path and the  output format
        path = meta_info[info.PATH.NAME]
        out_format = self._get_list_query(in_info.FORMAT)
        # Get the name of the feature to load from db
        feat = self._get_list_query(feats)

        # All features that need id
        if feat in feats.ID_LIST:
            # get the id
            id_key = self._get_int_query(in_info.ID)
            # Return names based on id
            names = self._id_feature(feat, path, id_key)
        # All features that need bounds and id
        elif feat in feats.ID_BOX_LIST:
            # get the id
            id_key = self._get_int_query(in_info.ID)
            # get resolution from input
            res_xy = self.INPUT.RESOLUTION.XY
            resolution = self._get_int_query(res_xy)
            # get bounds from input
            for key in ['Z','Y','X','DEPTH','HEIGHT','WIDTH']:
                term = getattr(self.INPUT.POSITION, key)
                target_bounds.append(self._get_int_query(term))
            # scale the bounds from resolution
            scale = 2**resolution
            scales = np.tile([1 ,scale, scale], 2)
            bounds = np.uint32(target_bounds * scales)
            # Return names based on bounds
            names = self._id_box_feature(feat, path, id_key, bounds)
        # All features that need bounds
        elif feat in feats.BOX_LIST:
            # get resolution from input
            res_xy = self.INPUT.RESOLUTION.XY
            resolution = self._get_int_query(res_xy)
            # get bounds from input
            for key in ['Z','Y','X','DEPTH','HEIGHT','WIDTH']:
                term = getattr(self.INPUT.POSITION, key)
                target_bounds.append(self._get_int_query(term))
            # scale the bounds from resolution
            scale = 2**resolution
            scales = np.tile([1 ,scale, scale], 2)
            bounds = np.uint32(target_bounds * scales)
            # Return names based on bounds
            names = self._box_feature(feat, path, bounds)
{'fraction_non_alphanumeric': 0.10093457943925234, 'fraction_numerical': 0.0023610427939006395, 'mean_word_length': 1.9707773232028054, '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': '1665292', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2810, 'n_words': 756}