<commit_before>from django.conf import settings
from django.http import HttpResponse
from vncbrowser.models import CELL_BODY_CHOICES, \
    ClassInstanceClassInstance, Relation, Class, ClassInstance, \
    Project, User, Treenode, TreenodeConnector, Connector
from vncbrowser.views import catmaid_login_required, my_render_to_response, \
    get_form_and_neurons
from vncbrowser.views.export import get_annotation_graph

import json
try:
    import numpy as np
    import h5py
    from PIL import Image
except ImportError:
    pass
from contextlib import closing
from random import choice
import os
import sys

# This file defines constants used to correctly define the metadata for NeuroHDF microcircuit data

VerticesTypeSkeletonRootNode = {
    'name': 'skeleton root',
    'id': 1
}

VerticesTypeSkeletonNode = {
    'name': 'skeleton',
    'id': 2
}

VerticesTypeConnectorNode = {
    'name': 'connector',
    'id': 3
}

ConnectivityNeurite = {
    'name': 'neurite',
    'id': 1
}

ConnectivityPresynaptic = {
    'name': 'presynaptic_to',
    'id': 2
}

ConnectivityPostsynaptic = {
    'name': 'postsynaptic_to',
    'id': 3
}

def get_tile(request, project_id=None, stack_id=None):

    scale = float(request.GET.get('scale', '0'))
    height = int(request.GET.get('height', '0'))
    width = int(request.GET.get('width', '0'))
    x = int(request.GET.get('x', '0'))
    y = int(request.GET.get('y', '0'))
    z = int(request.GET.get('z', '0'))
    col = request.GET.get('col', 'y')
    row = request.GET.get('row', 'x')
    file_extension = request.GET.get('file_extension', 'png')
    hdf5_path = request.GET.get('hdf5_path', '/')

    fpath=os.path.join( settings.HDF5_STORAGE_PATH, '{0}_{1}.hdf'.format( project_id, stack_id ) )
    
    #print 'exists', os.path.exists(fpath)
    #with closing(h5py.File(fpath, 'r')) as hfile:
    #    image_data=hfile[str(z)].value

    w,h=1000,800
    # img = np.empty((width,height), np.uint32)
    #img.shape=height,width
    img = np.random.random_integers(0, 256, (height,width) ).astype(np.uint8)
    #img[0,0]=0x800000FF
    # img[:400,:400]=0xFFFF0000
    pilImage = Image.frombuffer('RGBA',(width,height),img,'raw','L',0,1)
    response = HttpResponse(mimetype="image/png")
    pilImage.save(response, "PNG")
    return response
<commit_msg>Read tiles from multiscale NeuroHDF<commit_after>from django.conf import settings
from django.http import HttpResponse
from vncbrowser.models import CELL_BODY_CHOICES, \
    ClassInstanceClassInstance, Relation, Class, ClassInstance, \
    Project, User, Treenode, TreenodeConnector, Connector
from vncbrowser.views import catmaid_login_required, my_render_to_response, \
    get_form_and_neurons
from vncbrowser.views.export import get_annotation_graph

import json
try:
    import numpy as np
    import h5py
    from PIL import Image
except ImportError:
    pass
from contextlib import closing
from random import choice
import os
import sys

# This file defines constants used to correctly define the metadata for NeuroHDF microcircuit data

VerticesTypeSkeletonRootNode = {
    'name': 'skeleton root',
    'id': 1
}

VerticesTypeSkeletonNode = {
    'name': 'skeleton',
    'id': 2
}

VerticesTypeConnectorNode = {
    'name': 'connector',
    'id': 3
}

ConnectivityNeurite = {
    'name': 'neurite',
    'id': 1
}

ConnectivityPresynaptic = {
    'name': 'presynaptic_to',
    'id': 2
}

ConnectivityPostsynaptic = {
    'name': 'postsynaptic_to',
    'id': 3
}

def get_tile(request, project_id=None, stack_id=None):

    scale = float(request.GET.get('scale', '0'))
    height = int(request.GET.get('height', '0'))
    width = int(request.GET.get('width', '0'))
    x = int(request.GET.get('x', '0'))
    y = int(request.GET.get('y', '0'))
    z = int(request.GET.get('z', '0'))
    col = request.GET.get('col', 'y')
    row = request.GET.get('row', 'x')
    file_extension = request.GET.get('file_extension', 'png')
    hdf5_path = request.GET.get('hdf5_path', '/')

    fpath=os.path.join( settings.HDF5_STORAGE_PATH, '{0}_{1}.hdf'.format( project_id, stack_id ) )
    
    #print 'exists', os.path.exists(fpath)
    
    with closing(h5py.File(fpath, 'r')) as hfile:
        hdfpath = hdf5_path + '/scale/' + str(scale) + '/data'
        print >> sys.stderr, 'hdf5 path', hdfpath
        image_data=hfile[hdfpath].value
        data=image_data[y:y+height,x:x+width,z]
        pilImage = Image.frombuffer('RGBA',(width,height),data,'raw','L',0,1)
        response = HttpResponse(mimetype="image/png")
        pilImage.save(response, "PNG")
        return response


    w,h=1000,800
    # img = np.empty((width,height), np.uint32)
    #img.shape=height,width
    img = np.random.random_integers(0, 150, (height,width) ).astype(np.uint8)
    #img[0,0]=0x800000FF
    # img[:400,:400]=0xFFFF0000
    pilImage = Image.frombuffer('RGBA',(width,height),img,'raw','L',0,1)
    response = HttpResponse(mimetype="image/png")
    pilImage.save(response, "PNG")
    return response

{'fraction_non_alphanumeric': 0.14629741119807346, 'fraction_numerical': 0.022877784467188442, 'mean_word_length': 3.9444444444444446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7550860', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1750, 'n_words': 365}