<commit_before># -*- coding: utf-8 -*-
import logging
from pathlib import Path
from datetime import datetime
import numpy as np
from time import sleep
from picamera import PiCamera
import picamera.array

KEY = '/imgs'  # handle to write inside the output file
CLVL = 1  # ZIP compression level
REDGAIN = 1.
BLUEGAIN = 1.

def pibayerraw(Nimg:int, exposure_sec:float, bit8:bool=False,
               preview=None, outfn:Path=None):
    """
    loop image acquisition, optionally plotting

    see http://picamera.readthedocs.io/en/release-1.13/recipes1.html?highlight=close#recording-video-to-a-file
    if you wish to record video to a file

    http://picamera.readthedocs.io/en/release-1.13/recipes1.html?highlight=close#capturing-to-a-pil-image
    if you wish to save image(s)

    Demosaick reference:
        https://github.com/scivision/pysumix/blob/master/pysumix/demosaic.py#L58
        may need adaptation for Raspberry Pi camera
    """
    with PiCamera() as cam: #load camera driver
        setparams(cam, exposure_sec) #wait till after sleep() so that gains settle before turning off auto
        getparams(cam)
#%% optional setup plot
        hi,ht = _preview(cam, preview, bit8)
        if isinstance(preview,(int,float)): # GPU preview
            return
#%% optional setup output file
        f = _writesetup(outfn, Nimg, grabframe(cam, bit8), cam)
#%% main loop
        try:
            for i in range(Nimg):
                img = grabframe(cam, bit8)
#%% write this frame to output file
                writeframe(f, i, img)
#%% plot--not recommended due to very slow 10 seconds update
                updatepreview(img, hi, ht)
        except KeyboardInterrupt:
            pass # cleanup, close camera. Might need to press Ctrl C a couple times.

    if f is not None:
        f.close()

def grabframe(cam:PiCamera, bit8:bool=False):
#   tic = time()
    with picamera.array.PiBayerArray(cam, output_dims=2) as S:
        cam.capture(S, 'jpeg', bayer=True, use_video_port=False)

        img = S.array  # must be under 'with'

    assert isinstance(img,np.ndarray) and img.ndim == 2
#%% linear scale 10-bit to 8-bit
    if bit8:
        img = sixteen2eight(img, (0,2**10))
#   print('{:.1f} sec. to grab frame'.format(time()-tic))
    return img


def writeframe(f, i:int, img:np.ndarray):
    if f is None:
        return


    assert img.ndim == 2

    print('writing image #',i,'\r',end="",flush=True)

    if 'h5py' in str(f.__class__): # HDF5
        f[KEY][i,:,:] = img
    elif 'tifffile' in str(f.__class__): # TIFF
        f.save(img, compress=CLVL)


def updatepreview(img, hi, ht):
    if hi is not None:
        from matplotlib.pyplot import draw,pause
#       tic = time()
        hi.set_data(img) #2.7 sec
        ht.set_text(str(datetime.now()))
        draw()
        pause(0.01)
#       print('{:.1f} sec. to update plot'.format(time()-tic))


def _writesetup(outfn:Path, Nimg:int, img:np.ndarray, cam:PiCamera):
    if not outfn:
        return

    outfn = Path(outfn).expanduser()

    # note: both these file types must be .close() when done!
    if outfn.suffix == '.h5':
        import h5py
        f = h5py.File(outfn,'w',libver='earliest')
        f.create_dataset(KEY,
                         shape=(Nimg,img.shape[0],img.shape[1]),
                         dtype=img.dtype,
                         compression='gzip',
                         compression_opts=CLVL,
                         chunks=True)
    elif outfn.suffix in ('.tif','.tiff'):
        import tifffile
        f = tifffile.TiffWriter(str(outfn)) # NO append/compress keywords
        f.save(extratags=[
                (33434,'f',1,cam.exposure_speed/1e6,True)  # exposure (sec)
               ]
               )
    else:
        raise ValueError('unknown file type {}'.format(outfn))

    print('writing',outfn)

    return f


def _preview(cam:PiCamera, preview, bit8:bool):

    hi=None; ht=None

    if isinstance(preview,(int,float)):
        print('Preview-only mode runs for ',preview,' seconds, or Ctrl-C.')
        try:
            cam.start_preview()
            sleep(preview)
        except KeyboardInterrupt:
            cam.stop_preview()
        finally:
            cam.stop_preview()
    elif preview=='mpl':
        from matplotlib.pyplot import figure<commit_msg>write inline exposure once<commit_after># -*- coding: utf-8 -*-
import logging
from pathlib import Path
from datetime import datetime
import numpy as np
from time import sleep
from picamera import PiCamera
import picamera.array

KEY = '/imgs'  # handle to write inside the output file
CLVL = 1  # ZIP compression level
REDGAIN = 1.
BLUEGAIN = 1.

def pibayerraw(Nimg:int, exposure_sec:float, bit8:bool=False,
               preview=None, outfn:Path=None):
    """
    loop image acquisition, optionally plotting

    see http://picamera.readthedocs.io/en/release-1.13/recipes1.html?highlight=close#recording-video-to-a-file
    if you wish to record video to a file

    http://picamera.readthedocs.io/en/release-1.13/recipes1.html?highlight=close#capturing-to-a-pil-image
    if you wish to save image(s)

    Demosaick reference:
        https://github.com/scivision/pysumix/blob/master/pysumix/demosaic.py#L58
        may need adaptation for Raspberry Pi camera
    """
    with PiCamera() as cam: #load camera driver
        setparams(cam, exposure_sec) #wait till after sleep() so that gains settle before turning off auto
        getparams(cam)
#%% optional setup plot
        hi,ht = _preview(cam, preview, bit8)
        if isinstance(preview,(int,float)): # GPU preview
            return
#%% optional setup output file
        f = _writesetup(outfn, Nimg, grabframe(cam, bit8))
#%% main loop
        try:
            for i in range(Nimg):
                img = grabframe(cam, bit8)
#%% write this frame to output file
                writeframe(f, i, img, cam)
#%% plot--not recommended due to very slow 10 seconds update
                updatepreview(img, hi, ht)
        except KeyboardInterrupt:
            pass # cleanup, close camera. Might need to press Ctrl C a couple times.

    if f is not None:
        f.close()

def grabframe(cam:PiCamera, bit8:bool=False):
#   tic = time()
    with picamera.array.PiBayerArray(cam, output_dims=2) as S:
        cam.capture(S, 'jpeg', bayer=True, use_video_port=False)

        img = S.array  # must be under 'with'

    assert isinstance(img,np.ndarray) and img.ndim == 2
#%% linear scale 10-bit to 8-bit
    if bit8:
        img = sixteen2eight(img, (0,2**10))
#   print('{:.1f} sec. to grab frame'.format(time()-tic))
    return img


def writeframe(f, i:int, img:np.ndarray, cam:PiCamera):
    if f is None:
        return


    assert img.ndim == 2

    print('writing image #',i,'\r',end="",flush=True)

    if 'h5py' in str(f.__class__): # HDF5
        f[KEY][i,:,:] = img
    elif 'tifffile' in str(f.__class__): # TIFF
        if i==0:
            f.save(img, compress=CLVL,
               extratags=[(33434,'f',1,cam.exposure_speed/1e6,True)  # exposure (sec)
               ])
        else:
            f.save(img, compress=CLVL)


def updatepreview(img, hi, ht):
    if hi is not None:
        from matplotlib.pyplot import draw,pause
#       tic = time()
        hi.set_data(img) #2.7 sec
        ht.set_text(str(datetime.now()))
        draw()
        pause(0.01)
#       print('{:.1f} sec. to update plot'.format(time()-tic))


def _writesetup(outfn:Path, Nimg:int, img:np.ndarray):
    if not outfn:
        return

    outfn = Path(outfn).expanduser()

    # note: both these file types must be .close() when done!
    if outfn.suffix == '.h5':
        import h5py
        f = h5py.File(outfn,'w',libver='earliest')
        f.create_dataset(KEY,
                         shape=(Nimg,img.shape[0],img.shape[1]),
                         dtype=img.dtype,
                         compression='gzip',
                         compression_opts=CLVL,
                         chunks=True)
    elif outfn.suffix in ('.tif','.tiff'):
        import tifffile
        f = tifffile.TiffWriter(str(outfn)) # NO append/compress keywords
    else:
        raise ValueError('unknown file type {}'.format(outfn))

    print('writing',outfn)

    return f


def _preview(cam:PiCamera, preview, bit8:bool):

    hi=None; ht=None

    if isinstance(preview,(int,float)):
        print('Preview-only mode runs for ',preview,' seconds, or Ctrl-C.')
        try:
            cam.start_preview()
            sleep(preview)
        except KeyboardInterrupt:
            cam.stop_preview()
        finally:
            cam.stop_preview()
    elif preview=='mpl':
        from matplotlib.pyplot import figure
{'fraction_non_alphanumeric': 0.11997690531177829, 'fraction_numerical': 0.013048498845265589, 'mean_word_length': 2.376608187134503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1261788', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2746, 'n_words': 736}