<commit_before># Copyright 2014 NeuroData (http://neurodata.io)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import networkx as nx
from contextlib import closing
from operator import add, sub
import tempfile
from ndproj.ndproject import NDProject
from ndramon import annotation, ramondb
import spdb.spatialdb
from webservices.ndwserror import NDWSError
import logging
logger = logging.getLogger("neurodata")

def getAnnoIds(proj, ch, resolution, xmin, xmax, ymin, ymax, zmin, zmax):
  """Return a list of anno ids restricted by equality predicates. Equalities are alternating in field/value in the url."""

  mins = (xmin, ymin, zmin)
  maxs = (xmax, ymax, zmax)
  offset = proj.datasetcfg.get_offset(resolution)
  # Add a comment
  corner = map(max, zip(*[mins, map(sub, mins, offset)]))
  dim = map(sub, maxs, mins)

  if not proj.datasetcfg.checkCube(resolution, corner, dim):
    logger.error("Illegal cutout corner={}, dim={}".format(corner, dim))
    raise NDWSError("Illegal cutout corner={}, dim={}".format(corner, dim))

  with closing (spatialdb.SpatialDB(proj)) as sdb:
    cutout = sdb.cutout(ch, corner, dim, resolution)

  if cutout.isNotZeros():
    annoids = np.unique(cutout.data)
  else:
    annoids = np.asarray([0], dtype=np.uint32)

  if annoids[0] == 0:
    return annoids[1:]
  else:
    return annoids

def genGraphRAMON(token_name, channel, graphType="graphml", xmin=0, xmax=0, ymin=0, ymax=0, zmin=0, zmax=0):
  """Generate the graph based on different inputs"""
  
  # converting all parameters to integers
  [xmin, xmax, ymin, ymax, zmin, zmax] = [int(i) for i in [xmin, xmax, ymin, ymax, zmin, zmax]]
  proj = NDProject.fromTokenName(token_name)

  with closing (ramondb.RamonDB(proj)) as db:
    ch = proj.getChannelObj(channel)
    resolution = ch.getResolution()

    cubeRestrictions = xmin + xmax + ymin + ymax + zmin + zmax
    matrix = []
    
    # assumption that the channel is a neuron channel
    if cubeRestrictions != 0:
      idslist = getAnnoIds(proj, ch, resolution, xmin, xmax, ymin, ymax, zmin, zmax)
    else:
      # entire cube<commit_msg>[django] Fixing issues in ndgraph.<commit_after># Copyright 2014 NeuroData (http://neurodata.io)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import networkx as nx
from contextlib import closing
from operator import add, sub
import tempfile
from ndproj.ndproject import NDProject
from ndramon import annotation, ramondb
from spdb.spatialdb import SpatialDB
from webservices.ndwserror import NDWSError
import logging
logger = logging.getLogger("neurodata")

def getAnnoIds(proj, ch, resolution, xmin, xmax, ymin, ymax, zmin, zmax):
  """Return a list of anno ids restricted by equality predicates. Equalities are alternating in field/value in the url."""

  mins = (xmin, ymin, zmin)
  maxs = (xmax, ymax, zmax)
  offset = proj.datasetcfg.get_offset(resolution)
  # Add a comment
  corner = map(max, zip(*[mins, map(sub, mins, offset)]))
  dim = map(sub, maxs, mins)

  if not proj.datasetcfg.checkCube(resolution, corner, dim):
    logger.error("Illegal cutout corner={}, dim={}".format(corner, dim))
    raise NDWSError("Illegal cutout corner={}, dim={}".format(corner, dim))

  with closing (SpatialDB(proj)) as sdb:
    cutout = sdb.cutout(ch, corner, dim, resolution)

  if cutout.isNotZeros():
    annoids = np.unique(cutout.data)
  else:
    annoids = np.asarray([0], dtype=np.uint32)

  if annoids[0] == 0:
    return annoids[1:]
  else:
    return annoids

def genGraphRAMON(token_name, channel, graphType="graphml", xmin=0, xmax=0, ymin=0, ymax=0, zmin=0, zmax=0):
  """Generate the graph based on different inputs"""
  
  # converting all parameters to integers
  [xmin, xmax, ymin, ymax, zmin, zmax] = [int(i) for i in [xmin, xmax, ymin, ymax, zmin, zmax]]
  proj = NDProject.fromTokenName(token_name)

  with closing (ramondb.RamonDB(proj)) as db:
    ch = proj.getChannelObj(channel)
    resolution = ch.getResolution()

    cubeRestrictions = xmin + xmax + ymin + ymax + zmin + zmax
    matrix = []
    
    # assumption that the channel is a neuron channel
    if cubeRestrictions != 0:
      idslist = getAnnoIds(proj, ch, resolution, xmin, xmax, ymin, ymax, zmin, zmax)
    else:
      # entire cube
{'fraction_non_alphanumeric': 0.10746951219512195, 'fraction_numerical': 0.008003048780487805, 'mean_word_length': 4.212512413108242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6506924', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1693, 'n_words': 595}