Example #1
0
    def _do_modify_data(self, uuid, dataname, dims, shape, offset):
        """
        Respond to a POST request to modify a subvolume of data.

        All parameters are strings from the REST string.
        """
        dataset = self._get_h5_dataset(uuid, dataname)
        roi_start, roi_stop = self._determine_request_roi( dataset, dims, shape, offset )
        # Prepend channel to make "full" roi
        full_roi_start = (0,) + roi_start
        full_roi_stop = (dataset.shape[0],) + roi_stop
        full_roi_shape = numpy.subtract(full_roi_stop, full_roi_start)
        slicing = tuple( slice(x,y) for x,y in zip(full_roi_start, full_roi_stop) )
        
        # If the user is writing data beoyond the current extents of the dataset,
        #  resize the dataset first.
        if (numpy.array(full_roi_stop) > dataset.shape).any():
            dataset.resize( full_roi_stop )
        
        voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)
        codec = VoxelsNddataCodec( voxels_metadata )
        data = codec.decode_to_ndarray(self.rfile, full_roi_shape)

        dataset[slicing] = data
        self.server.h5_file.flush()

        #self.send_response(httplib.NO_CONTENT) # "No Content" (accepted)
        self.send_response(httplib.OK)
        self.send_header("Content-length", 0 )
        self.end_headers()
 def test_metainfo_from_h5(self):
     shape = (3, 9, 10, 11)
     starting_metadata = VoxelsMetadata.create_default_metadata( shape, numpy.float32, "cxyz", 1.0, "nanometers" )
     f = h5py.File("dummy.h5", mode='w', driver='core', backing_store=False) # In-memory
     dset = f.create_dataset( 'dset', shape=shape, dtype=numpy.float32, chunks=True )
      
     metadata = VoxelsMetadata.create_from_h5_dataset( dset )
     assert metadata.dtype.type is numpy.float32
     assert metadata.shape == starting_metadata.shape, \
         "Wrong shape: {} vs. {}".format( metadata.shape, starting_metadata.shape )
Example #3
0
    def _do_modify_data(self, uuid, dataname, dims, shape, offset):
        """
        Respond to a POST request to modify a subvolume of data.

        All parameters are strings from the REST string.
        """
        dataset = self._get_h5_dataset(uuid, dataname)

        roi_start, roi_stop = self._determine_request_roi(
            dataset, dims, shape, offset)

        # Prepend channel to make "full" roi
        full_roi_start = (0, ) + roi_start
        full_roi_stop = (dataset.shape[0], ) + roi_stop
        full_roi_shape = numpy.subtract(full_roi_stop, full_roi_start)
        slicing = tuple(
            slice(x, y) for x, y in zip(full_roi_start, full_roi_stop))

        if 'dvid_metadata' in dataset.attrs:
            voxels_metadata = VoxelsMetadata(dataset.attrs['dvid_metadata'])
            del dataset.attrs['dvid_metadata']
        else:
            voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)

        # If the user is writing data beyond the current extents of the dataset,
        #  resize the dataset first.
        if (numpy.array(full_roi_stop) > dataset.shape).any():
            dataset.resize(numpy.maximum(full_roi_stop, dataset.shape))
            voxels_metadata.shape = tuple(
                map(int, numpy.maximum(voxels_metadata.shape, full_roi_stop)))

        # Overwrite minindex is needed
        if (numpy.array(full_roi_start) < voxels_metadata.minindex).any():
            voxels_metadata.minindex = tuple(
                numpy.minimum(voxels_metadata.minindex, full_roi_start))

        dataset.attrs['dvid_metadata'] = voxels_metadata.to_json()

        # Must read the entire message body, even if it isn't used below.
        codec = VoxelsNddataCodec(dataset.dtype)
        data = codec.decode_to_ndarray(self.rfile, full_roi_shape)

        if (numpy.array(roi_start) < 0).any():
            # We don't support negative coordinates in this mock server.
            #  But as a compromise, we don't choke here.
            #  Instead, we simply do nothing.
            pass
        else:
            dataset[slicing] = data
        self.server.h5_file.flush()

        #self.send_response(httplib.NO_CONTENT) # "No Content" (accepted)
        self.send_response(httplib.OK)
        self.send_header("Content-length", 0)
        self.end_headers()
Example #4
0
    def _do_get_volume_schema(self, uuid, dataname):
        """
        Respond to a query for dataset info.
        """
        dataset = self._get_h5_dataset(uuid, dataname)
        voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)
        json_text = json.dumps( voxels_metadata )

        self.send_response(httplib.OK)
        self.send_header("Content-type", "text/json")
        self.send_header("Content-length", str(len(json_text)))
        self.end_headers()
        self.wfile.write( json_text )
Example #5
0
    def _do_get_volume_schema(self, uuid, dataname):
        """
        Respond to a query for dataset info.
        """
        dataset = self._get_h5_dataset(uuid, dataname)
        voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)
        json_text = json.dumps(voxels_metadata)

        self.send_response(httplib.OK)
        self.send_header("Content-type", "text/json")
        self.send_header("Content-length", str(len(json_text)))
        self.end_headers()
        self.wfile.write(json_text)
Example #6
0
    def _do_modify_data(self, uuid, dataname, dims, shape, offset):
        """
        Respond to a POST request to modify a subvolume of data.

        All parameters are strings from the REST string.
        """
        dataset = self._get_h5_dataset(uuid, dataname)

        roi_start, roi_stop = self._determine_request_roi( dataset, dims, shape, offset )

        # Prepend channel to make "full" roi
        full_roi_start = (0,) + roi_start
        full_roi_stop = (dataset.shape[0],) + roi_stop
        full_roi_shape = numpy.subtract(full_roi_stop, full_roi_start)
        slicing = tuple( slice(x,y) for x,y in zip(full_roi_start, full_roi_stop) )

        if 'dvid_metadata' in dataset.attrs:
            voxels_metadata = VoxelsMetadata(dataset.attrs['dvid_metadata'])
            del dataset.attrs['dvid_metadata']
        else:
            voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)

        # If the user is writing data beyond the current extents of the dataset,
        #  resize the dataset first.
        if (numpy.array(full_roi_stop) > dataset.shape).any():
            dataset.resize( numpy.maximum(full_roi_stop, dataset.shape) )
            voxels_metadata.shape = tuple( map(int, numpy.maximum( voxels_metadata.shape, full_roi_stop )) )

        # Overwrite minindex is needed
        if (numpy.array(full_roi_start) < voxels_metadata.minindex).any():
            voxels_metadata.minindex = tuple( numpy.minimum( voxels_metadata.minindex, full_roi_start ) )

        dataset.attrs['dvid_metadata'] = voxels_metadata.to_json()

        # Must read the entire message body, even if it isn't used below.
        codec = VoxelsNddataCodec( dataset.dtype )
        data = codec.decode_to_ndarray(self.rfile, full_roi_shape)
    
        if (numpy.array(roi_start) < 0).any():        
            # We don't support negative coordinates in this mock server.
            #  But as a compromise, we don't choke here.
            #  Instead, we simply do nothing.
            pass
        else:
            dataset[slicing] = data
        self.server.h5_file.flush()

        #self.send_response(httplib.NO_CONTENT) # "No Content" (accepted)
        self.send_response(httplib.OK)
        self.send_header("Content-length", 0 )
        self.end_headers()
Example #7
0
    def test_metainfo_from_h5(self):
        shape = (3, 9, 10, 11)
        starting_metadata = VoxelsMetadata.create_default_metadata(
            shape, numpy.float32, "cxyz", 1.0, "nanometers")
        f = h5py.File("dummy.h5", mode='w', driver='core',
                      backing_store=False)  # In-memory
        dset = f.create_dataset('dset',
                                shape=shape,
                                dtype=numpy.float32,
                                chunks=True)

        metadata = VoxelsMetadata.create_from_h5_dataset(dset)
        assert metadata.dtype.type is numpy.float32
        assert metadata.shape == starting_metadata.shape, \
            "Wrong shape: {} vs. {}".format( metadata.shape, starting_metadata.shape )
Example #8
0
    def _do_get_data(self, uuid, dataname, dims, shape, offset):
        """
        Respond to a query for volume data.
        
        All parameters are strings from the REST string.
        """
        dataset = self._get_h5_dataset(uuid, dataname)
        roi_start, roi_stop = self._determine_request_roi( dataset, dims, shape, offset )
        # Prepend channel slicing
        slicing = (slice(None),) + tuple( slice(x,y) for x,y in zip(roi_start, roi_stop) )
        
        data = dataset[slicing]
        
        voxels_metadata = VoxelsMetadata.create_from_h5_dataset(dataset)
        codec = VoxelsNddataCodec( voxels_metadata )
        buffer_len = codec.calculate_buffer_len( data.shape )

        self.send_response(httplib.OK)
        self.send_header("Content-type", VoxelsNddataCodec.VOLUME_MIMETYPE)
        self.send_header("Content-length", str(buffer_len) )
        self.end_headers()

        codec.encode_from_ndarray( self.wfile, data )