# Path: swhlab/core.py
# def abfIDfromFname(fname):
# def abfProtocol(fname):
# def headerHTML(header,fname):
#     def __init__(self, fname, createFolder=False):
#     def setsweep(self, sweep=0, channel=0):
#     def sweepList(self):
#     def setsweeps(self):
#     def comments_load(self):
#     def generate_protocol(self):
#     def get_protocol(self,sweep):
#     def get_protocol_sequence(self,sweep):
#     def clamp_values(self,timePoint=0):
#     def epochTimes(self,nEpoch=2):
#     def average(self,t1=0,t2=None,setsweep=False):
#     def averageSweep(self,sweepFirst=0,sweepLast=None):
#     def kernel_gaussian(self, sizeMS, sigmaMS=None, forwardOnly=False):
#     def sweepYfiltered(self):
#     def sweepYsmartbase(self):
#     def phasicNet(self,biggestEvent=50,m1=.5,m2=None):
#     def output_touch(self):
#     def output_clean(self):
#     def inspect(self):
# class ABF:
#             Y = proto[step]['fEpochInitLevel']+proto[step]['fEpochLevelInc']*self.sweep
#         I1,I2=int(t1*self.pointsPerSec),int(t2*self.pointsPerSec)
#         Y=self.sweepYsmartbase()[int(m1):int(m2)]


import os
import numpy as np
from scipy import ndimage
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from swhlab.core import common as cm #shorthand


def convert(fname,saveAs=True,showToo=False):
    """
    Convert weird TIF files into web-friendly versions.
    Auto contrast is applied (saturating lower and upper 0.1%).
        make saveAs True to save as .TIF.png
        make saveAs False and it won't save at all
        make saveAs "someFile.jpg" to save it as a different path/format
    """

    # load the image
    #im = Image.open(fname) #PIL can't handle 12-bit TIFs well
    im=ndimage.imread(fname) #scipy does better with it
    im=np.array(im,dtype=float) # now it's a numpy array

    # do all image enhancement here
    cutoffLow=np.percentile(im,.01)
    cutoffHigh=np.percentile(im,99.99)
    im[np.where(im<cutoffLow)]=cutoffLow
    im[np.where(im>cutoffHigh)]=cutoffHigh

    # IMAGE FORMATTING
    im-=np.min(im) #auto contrast
    im/=np.max(im) #normalize
    im*=255 #stretch contrast (8-bit)
    im = Image.fromarray(im)

    # IMAGE DRAWING
    msg="%s\n"%os.path.basename(fname)

# Path: swhlab/core.py
# def abfIDfromFname(fname):
# def abfProtocol(fname):
# def headerHTML(header,fname):
#     def __init__(self, fname, createFolder=False):
#     def setsweep(self, sweep=0, channel=0):
#     def sweepList(self):
#     def setsweeps(self):
#     def comments_load(self):
#     def generate_protocol(self):
#     def get_protocol(self,sweep):
#     def get_protocol_sequence(self,sweep):
#     def clamp_values(self,timePoint=0):
#     def epochTimes(self,nEpoch=2):
#     def average(self,t1=0,t2=None,setsweep=False):
#     def averageSweep(self,sweepFirst=0,sweepLast=None):
#     def kernel_gaussian(self, sizeMS, sigmaMS=None, forwardOnly=False):
#     def sweepYfiltered(self):
#     def sweepYsmartbase(self):
#     def phasicNet(self,biggestEvent=50,m1=.5,m2=None):
#     def output_touch(self):
#     def output_clean(self):
#     def inspect(self):
# class ABF:
#             Y = proto[step]['fEpochInitLevel']+proto[step]['fEpochLevelInc']*self.sweep
#         I1,I2=int(t1*self.pointsPerSec),int(t2*self.pointsPerSec)
#         Y=self.sweepYsmartbase()[int(m1):int(m2)]


# Path: doc/oldcode/indexing/image.py
import os
import numpy as np
from scipy import ndimage
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from swhlab.core import common as cm #shorthand



def convert(fname,saveAs=True,showToo=False):
    """
    Convert weird TIF files into web-friendly versions.
    Auto contrast is applied (saturating lower and upper 0.1%).
        make saveAs True to save as .TIF.png
        make saveAs False and it won't save at all
        make saveAs "someFile.jpg" to save it as a different path/format
    """

    # load the image
    #im = Image.open(fname) #PIL can't handle 12-bit TIFs well
    im=ndimage.imread(fname) #scipy does better with it
    im=np.array(im,dtype=float) # now it's a numpy array

    # do all image enhancement here
    cutoffLow=np.percentile(im,.01)
    cutoffHigh=np.percentile(im,99.99)
    im[np.where(im<cutoffLow)]=cutoffLow
    im[np.where(im>cutoffHigh)]=cutoffHigh

    # IMAGE FORMATTING
    im-=np.min(im) #auto contrast
    im/=np.max(im) #normalize
    im*=255 #stretch contrast (8-bit)
    im = Image.fromarray(im)

    # IMAGE DRAWING
    msg="%s\n"%os.path.basename(fname)

    msg+="%s\n"%cm.epochToString(os.path.getmtime(fname))