<s> from contextlib import contextmanager <EOL> import os <EOL> import shutil <EOL> from tempfile import mkdtemp <EOL> import pytest <EOL> pytest . importorskip ( '<STR_LIT>' ) <EOL> from dask . array . image import imread as da_imread <EOL> import numpy as np <EOL> from skimage . io import imsave <EOL> @ contextmanager <EOL> def random_images ( n , shape ) : <EOL> dirname = mkdtemp ( ) <EOL> for i in range ( n ) : <EOL> fn = os . path . join ( dirname , '<STR_LIT>' % i ) <EOL> x = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = shape ) . astype ( '<STR_LIT>' ) <EOL> imsave ( fn , x ) <EOL> try : <EOL> yield os . path . join ( dirname , '<STR_LIT>' ) <EOL> finally : <EOL> shutil . rmtree ( dirname ) <EOL> def test_imread ( ) : <EOL> with random_images ( <NUM_LIT:4> , ( <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:3> ) ) as globstring : <EOL> im = da_imread ( globstring ) <EOL> assert im . shape == ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:3> ) <EOL> assert im . chunks == ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , ) , ( <NUM_LIT:6> , ) , ( <NUM_LIT:3> , ) ) <EOL> assert im . dtype == '<STR_LIT>' <EOL> assert im . compute ( ) . shape == ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:3> ) <EOL> assert im . compute ( ) . dtype == '<STR_LIT>' <EOL> def test_imread_with_custom_function ( ) : <EOL> def imread2 ( fn ) : <EOL> return np . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) , dtype = '<STR_LIT>' ) <EOL> with random_images ( <NUM_LIT:4> , ( <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:3> ) ) as globstring : <EOL> im = da_imread ( globstring , imread = imread2 ) <EOL> assert ( im . compute ( ) == np . ones ( ( <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) , dtype = '<STR_LIT>' ) ) . all ( ) <EOL> def test_preprocess ( ) : <EOL> def preprocess ( x ) : <EOL> x [ : ] = <NUM_LIT:1> <EOL> return x [ : , : , <NUM_LIT:0> ] <EOL> with random_images ( <NUM_LIT:4> , ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) as globstring : <EOL> im = da_imread ( globstring , preprocess = preprocess ) <EOL> assert ( im . compute ( ) == np . ones ( ( <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = '<STR_LIT>' ) ) . all ( ) </s>
