<s> """<STR_LIT>""" <EOL> import os <EOL> import json <EOL> import numpy as np <EOL> from . . import dataset <EOL> from . . import volume <EOL> class Package ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> self . dataset = dataset . normalize ( data ) <EOL> self . uniques = data . uniques ( collapse = True ) <EOL> self . brains = dict ( ) <EOL> self . images = dict ( ) <EOL> for brain in self . uniques : <EOL> name = brain . name <EOL> self . brains [ name ] = brain . to_json ( simple = True ) <EOL> voldata = brain . volume <EOL> if isinstance ( brain , ( dataset . VolumeRGB , dataset . VertexRGB ) ) : <EOL> voldata = voldata . astype ( np . uint8 ) <EOL> self . brains [ name ] [ '<STR_LIT>' ] = True <EOL> else : <EOL> voldata = voldata . astype ( np . float32 ) <EOL> self . brains [ name ] [ '<STR_LIT>' ] = False <EOL> self . images [ name ] = [ volume . mosaic ( vol , show = False ) for vol in voldata ] <EOL> if len ( set ( [ shape for m , shape in self . images [ name ] ] ) ) != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . brains [ name ] [ '<STR_LIT>' ] = self . images [ name ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . images [ name ] = [ _pack_png ( m ) for m , shape in self . images [ name ] ] <EOL> @ property <EOL> def views ( self ) : <EOL> metadata = [ ] <EOL> for name , view in self . dataset : <EOL> meta = view . to_json ( simple = False ) <EOL> meta [ '<STR_LIT:name>' ] = name <EOL> if '<STR_LIT>' in meta [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = os . path . split ( meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) [ <NUM_LIT:1> ] <EOL> metadata . append ( meta ) <EOL> return metadata <EOL> @ property <EOL> def subjects ( self ) : <EOL> return set ( braindata . subject for braindata in self . uniques ) <EOL> def metadata ( self , ** kwargs ) : <EOL> return dict ( views = self . views , data = self . brains , images = self . image_names ( ** kwargs ) ) <EOL> def image_names ( self , fmt = "<STR_LIT>" ) : <EOL> names = dict ( ) <EOL> for name , imgs in self . images . items ( ) : <EOL> names [ name ] = [ fmt . format ( name = name , frame = i ) for i in range ( len ( imgs ) ) ] <EOL> return names <EOL> def _pack_png ( mosaic ) : <EOL> from PIL import Image <EOL> import cStringIO <EOL> buf = cStringIO . StringIO ( ) <EOL> if mosaic . dtype not in ( np . float32 , np . uint8 ) : <EOL> raise TypeError <EOL> y , x = mosaic . shape [ : <NUM_LIT:2> ] <EOL> im = Image . frombuffer ( '<STR_LIT>' , ( x , y ) , mosaic . data , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> im . save ( buf , format = '<STR_LIT>' ) <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> return buf . read ( ) </s>
