# Path: cloudvolume/exceptions.py
# class UnsupportedFormatError(Exception):
#   """Unable to interpret the format of this URI. e.g. precomputed://"""
#   pass
# 
# class DimensionError(BaseException):
#   """Wrong number of dimensions."""
#   pass
# 
# Path: cloudvolume/lib.py
# def generate_random_string(size=6):
#   return ''.join(random.SystemRandom().choice(
#     string.ascii_lowercase + string.digits) for _ in range(size)
#   )
# 
# Path: cloudvolume/paths.py
# def strict_extract(cloudpath, windows=None, disable_toabs=False):
#   """
#   Same as cloudvolume.paths.extract, but raise an additional 
#   cloudvolume.exceptions.UnsupportedProtocolError
#   if either dataset or layer is not set.
# 
#   Returns: ExtractedPath
#   """
#   path = extract(cloudpath, windows, disable_toabs)
# 
#   if path.dataset == '' or path.layer == '':
#     raise UnsupportedProtocolError(cloudpath_error(cloudpath))
# 
#   return path
# 
# def to_https_protocol(cloudpath):
#   if isinstance(cloudpath, ExtractedPath):
#     if cloudpath.protocol in ('gs', 's3', 'matrix', 'tigerdata'):
#       return extract(to_https_protocol(ascloudpath(cloudpath)))
#     return cloudpath
# 
#   return cloudfiles.paths.to_https_protocol(cloudpath)


import sys
import time
import multiprocessing as mp
import numpy as np
from typing import Optional, Union
from .exceptions import UnsupportedFormatError, DimensionError
from .lib import generate_random_string
from .paths import strict_extract, to_https_protocol
    from .frontends import CloudVolumePrecomputed
  @classmethod
  def from_numpy(cls, 
    arr, 
    vol_path='file:///tmp/image/' + generate_random_string(),
    resolution=(4,4,40), voxel_offset=(0,0,0), 
    chunk_size=(128,128,64), layer_type=None, max_mip=0,
    encoding='raw', compress=None
  ):
    """
    Create a new dataset from a numpy array.

    max_mip: (int) the maximum mip level id in the info file. 
    Note that currently the numpy array can only sit in mip 0,
    the max_mip was only created in info file.
    the numpy array itself was not downsampled. 
    """
    if not layer_type:
      if arr.dtype in (bool, np.uint32, np.uint64, np.uint16):
        layer_type = 'segmentation'
      elif np.issubdtype(arr.dtype, np.integer) \
                        or np.issubdtype(arr.dtype, np.floating):
        layer_type = 'image'
      else:
        raise ValueError(f"{arr.dtype} is not supported.")

    if arr.ndim == 3:
      num_channels = 1
    elif arr.ndim == 4:
      num_channels = arr.shape[-1]
    else:

# Path: cloudvolume/exceptions.py
# class UnsupportedFormatError(Exception):
#   """Unable to interpret the format of this URI. e.g. precomputed://"""
#   pass
# 
# class DimensionError(BaseException):
#   """Wrong number of dimensions."""
#   pass
# 
# Path: cloudvolume/lib.py
# def generate_random_string(size=6):
#   return ''.join(random.SystemRandom().choice(
#     string.ascii_lowercase + string.digits) for _ in range(size)
#   )
# 
# Path: cloudvolume/paths.py
# def strict_extract(cloudpath, windows=None, disable_toabs=False):
#   """
#   Same as cloudvolume.paths.extract, but raise an additional 
#   cloudvolume.exceptions.UnsupportedProtocolError
#   if either dataset or layer is not set.
# 
#   Returns: ExtractedPath
#   """
#   path = extract(cloudpath, windows, disable_toabs)
# 
#   if path.dataset == '' or path.layer == '':
#     raise UnsupportedProtocolError(cloudpath_error(cloudpath))
# 
#   return path
# 
# def to_https_protocol(cloudpath):
#   if isinstance(cloudpath, ExtractedPath):
#     if cloudpath.protocol in ('gs', 's3', 'matrix', 'tigerdata'):
#       return extract(to_https_protocol(ascloudpath(cloudpath)))
#     return cloudpath
# 
#   return cloudfiles.paths.to_https_protocol(cloudpath)


# Path: cloudvolume/cloudvolume.py
import sys
import time
import multiprocessing as mp
import numpy as np
from typing import Optional, Union
from .exceptions import UnsupportedFormatError, DimensionError
from .lib import generate_random_string
from .paths import strict_extract, to_https_protocol
    from .frontends import CloudVolumePrecomputed

  @classmethod
  def from_numpy(cls, 
    arr, 
    vol_path='file:///tmp/image/' + generate_random_string(),
    resolution=(4,4,40), voxel_offset=(0,0,0), 
    chunk_size=(128,128,64), layer_type=None, max_mip=0,
    encoding='raw', compress=None
  ):
    """
    Create a new dataset from a numpy array.

    max_mip: (int) the maximum mip level id in the info file. 
    Note that currently the numpy array can only sit in mip 0,
    the max_mip was only created in info file.
    the numpy array itself was not downsampled. 
    """
    if not layer_type:
      if arr.dtype in (bool, np.uint32, np.uint64, np.uint16):
        layer_type = 'segmentation'
      elif np.issubdtype(arr.dtype, np.integer) \
                        or np.issubdtype(arr.dtype, np.floating):
        layer_type = 'image'
      else:
        raise ValueError(f"{arr.dtype} is not supported.")

    if arr.ndim == 3:
      num_channels = 1
    elif arr.ndim == 4:
      num_channels = arr.shape[-1]
    else:

      raise DimensionError(f"CloudVolume only accepts 3 or 4 dimensional images. Got: {arr.ndim}")