<commit_before>import numpy as N
from PIL import Image
import os

def make_viewer(mat):
    """ Given filters in rows, guesses dimensions of patchse
        and nice dimensions for the PatchViewer and returns a PatchViewer
        containing visualizations of the filters"""
    grid_shape = PatchViewer.pickSize(mat.shape[0])

    patch_shape = PatchViewer.pickSize(mat.shape[1])


    rval = PatchViewer( grid_shape, patch_shape)

    patch_shape = (patch_shape[0], patch_shape[1], 1)

    for i in xrange(mat.shape[0]):
        rval.add_patch( mat[i,:].reshape(*patch_shape), rescale = True )
    #

    return rval
#

class  PatchViewer:

    def __init__(self,grid_shape, patch_shape, is_color = False):
        assert len(grid_shape) == 2
        assert len(patch_shape) == 2

        self.is_color = is_color

        self.pad = (3,3)

        height  = self.pad[0]*(1+grid_shape[0])+grid_shape[0]*patch_shape[0]
        width = self.pad[1]*(1+grid_shape[1])+grid_shape[1]*patch_shape[1]


        if is_color:
            image_shape = (height, width, 3)
        else:
            image_shape = (height, width, 1)
        #
        self.image = N.zeros( image_shape ) + 0.5
        self.curPos = (0,0)

        self.patch_shape = patch_shape
        self.grid_shape = grid_shape

        #print "Made patch viewer with "+str(grid_shape)+" panels and patch size "+str(patch_shape)

    def clear(self):
		self.image[:] = 0.5
		self.curPos = (0,0)

	#0 is perfect gray. If not rescale, assumes images are in [-1,1]
    def add_patch(self, patch , rescale = True, recenter = False):
        if patch.shape[0:2] != self.patch_shape:
            raise ValueError('Expected patch with shape '+str(self.patch_shape)+', got '+str(patch.shape))

        if recenter:
            assert patch.shape[0] < self.patch_shape[0]
            assert patch.shape[1] < self.patch_shape[1]<commit_msg>improvements to patch viewer<commit_after>import numpy as N
from PIL import Image
import os

def make_viewer(mat, grid_shape  = None):
    """ Given filters in rows, guesses dimensions of patchse
        and nice dimensions for the PatchViewer and returns a PatchViewer
        containing visualizations of the filters"""
    if grid_shape is None:
        grid_shape = PatchViewer.pickSize(mat.shape[0])

    patch_shape = PatchViewer.pickSize(mat.shape[1])


    rval = PatchViewer( grid_shape, patch_shape)

    patch_shape = (patch_shape[0], patch_shape[1], 1)

    for i in xrange(mat.shape[0]):
        #rval.add_patch( N.ones(patch_shape) )
        rval.add_patch( mat[i,:].reshape(*patch_shape), rescale = True )
    #

    return rval
#

class  PatchViewer:

    def __init__(self,grid_shape, patch_shape, is_color = False):
        assert len(grid_shape) == 2
        assert len(patch_shape) == 2

        self.is_color = is_color

        self.pad = (3,3)

        height  = self.pad[0]*(1+grid_shape[0])+grid_shape[0]*patch_shape[0]
        width = self.pad[1]*(1+grid_shape[1])+grid_shape[1]*patch_shape[1]


        if is_color:
            image_shape = (height, width, 3)
        else:
            image_shape = (height, width, 1)
        #
        self.image = N.zeros( image_shape ) + 0.5
        self.curPos = (0,0)

        self.patch_shape = patch_shape
        self.grid_shape = grid_shape

        #print "Made patch viewer with "+str(grid_shape)+" panels and patch size "+str(patch_shape)

    def clear(self):
		self.image[:] = 0.5
		self.curPos = (0,0)

	#0 is perfect gray. If not rescale, assumes images are in [-1,1]
    def add_patch(self, patch , rescale = True, recenter = False):

        if (patch.min() == patch.max()) and (rescale or patch.min() == 0.0):
            print "Warning: displaying totally blank patch"

        if patch.shape[0:2] != self.patch_shape:
            raise ValueError('Expected patch with shape '+str(self.patch_shape)+', got '+str(patch.shape))

        if recenter:
            assert patch.shape[0] < self.patch_shape[0]
            assert patch.shape[1] < self.patch_shape[1]
{'fraction_non_alphanumeric': 0.1196029776674938, 'fraction_numerical': 0.019851116625310174, 'mean_word_length': 2.7637721755368814, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4982334', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1312, 'n_words': 332}