1010import numpy
1111
1212
13- def scale_to_unit_interval (ndar ,eps = 1e-8 ):
13+ def scale_to_unit_interval (ndar , eps = 1e-8 ):
1414 """ Scales all values in the ndarray ndar to be between 0 and 1 """
1515 ndar = ndar .copy ()
1616 ndar -= ndar .min ()
17- ndar *= 1.0 / (ndar .max ()+ eps )
17+ ndar *= 1.0 / (ndar .max () + eps )
1818 return ndar
1919
2020
21- def tile_raster_images (X , img_shape , tile_shape ,tile_spacing = (0 ,0 ),
22- scale_rows_to_unit_interval = True , output_pixel_vals = True ):
21+ def tile_raster_images (X , img_shape , tile_shape , tile_spacing = (0 , 0 ),
22+ scale_rows_to_unit_interval = True ,
23+ output_pixel_vals = True ):
2324 """
2425 Transform an array with one flattened image per row, into an array in
2526 which images are reshaped and layed out like tiles on a floor.
@@ -70,15 +71,17 @@ def tile_raster_images(X, img_shape, tile_shape,tile_spacing = (0,0),
7071 assert len (X ) == 4
7172 # Create an output numpy ndarray to store the image
7273 if output_pixel_vals :
73- out_array = numpy .zeros ((out_shape [0 ], out_shape [1 ], 4 ), dtype = 'uint8' )
74+ out_array = numpy .zeros ((out_shape [0 ], out_shape [1 ], 4 ),
75+ dtype = 'uint8' )
7476 else :
75- out_array = numpy .zeros ((out_shape [0 ], out_shape [1 ], 4 ), dtype = X .dtype )
77+ out_array = numpy .zeros ((out_shape [0 ], out_shape [1 ], 4 ),
78+ dtype = X .dtype )
7679
7780 #colors default to 0, alpha defaults to 1 (opaque)
7881 if output_pixel_vals :
79- channel_defaults = [0 ,0 , 0 , 255 ]
82+ channel_defaults = [0 , 0 , 0 , 255 ]
8083 else :
81- channel_defaults = [0. ,0. ,0. ,1. ]
84+ channel_defaults = [0. , 0. , 0. , 1. ]
8285
8386 for i in xrange (4 ):
8487 if X [i ] is None :
@@ -87,12 +90,14 @@ def tile_raster_images(X, img_shape, tile_shape,tile_spacing = (0,0),
8790 dt = out_array .dtype
8891 if output_pixel_vals :
8992 dt = 'uint8'
90- out_array [:,:, i ] = numpy .zeros (out_shape ,
91- dtype = dt )+ channel_defaults [i ]
93+ out_array [:, :, i ] = numpy .zeros (out_shape ,
94+ dtype = dt ) + channel_defaults [i ]
9295 else :
9396 # use a recurrent call to compute the channel and store it
9497 # in the output
95- out_array [:,:,i ] = tile_raster_images (X [i ], img_shape , tile_shape , tile_spacing , scale_rows_to_unit_interval , output_pixel_vals )
98+ out_array [:, :, i ] = tile_raster_images (
99+ X [i ], img_shape , tile_shape , tile_spacing ,
100+ scale_rows_to_unit_interval , output_pixel_vals )
96101 return out_array
97102
98103 else :
@@ -106,28 +111,25 @@ def tile_raster_images(X, img_shape, tile_shape,tile_spacing = (0,0),
106111 dt = 'uint8'
107112 out_array = numpy .zeros (out_shape , dtype = dt )
108113
109-
110114 for tile_row in xrange (tile_shape [0 ]):
111115 for tile_col in xrange (tile_shape [1 ]):
112116 if tile_row * tile_shape [1 ] + tile_col < X .shape [0 ]:
117+ this_x = X [tile_row * tile_shape [1 ] + tile_col ]
113118 if scale_rows_to_unit_interval :
114119 # if we should scale values to be between 0 and 1
115120 # do this by calling the `scale_to_unit_interval`
116121 # function
117- this_img = scale_to_unit_interval (X [tile_row * tile_shape [1 ] + tile_col ].reshape (img_shape ))
122+ this_img = scale_to_unit_interval (
123+ this_x .reshape (img_shape ))
118124 else :
119- this_img = X [ tile_row * tile_shape [ 1 ] + tile_col ] .reshape (img_shape )
125+ this_img = this_x .reshape (img_shape )
120126 # add the slice to the corresponding position in the
121127 # output array
122128 c = 1
123129 if output_pixel_vals :
124130 c = 255
125131 out_array [
126- tile_row * (H + Hs ):tile_row * (H + Hs )+ H ,
127- tile_col * (W + Ws ):tile_col * (W + Ws )+ W
128- ] \
129- = this_img * c
132+ tile_row * (H + Hs ): tile_row * (H + Hs ) + xsH ,
133+ tile_col * (W + Ws ): tile_col * (W + Ws ) + W
134+ ] = this_img * c
130135 return out_array
131-
132-
133-
0 commit comments