def save(state, filename=None, desc='<STR_LIT>', extra=None):
if isinstance(state.image, util.RawImage):<EOL><INDENT>desc = desc or '<STR_LIT>'<EOL>filename = filename or state.image.filename + '<STR_LIT>' + desc + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if not filename:<EOL><INDENT>raise AttributeError("<STR_LIT>")<EOL><DEDENT><DEDENT>if extra is None:<EOL><INDENT>save = state<EOL><DEDENT>else:<EOL><INDENT>save = [state] + extra<EOL><DEDENT>if os.path.exists(filename):<EOL><INDENT>ff = "<STR_LIT>".format(filename)<EOL>if os.path.exists(ff):<EOL><INDENT>os.remove(ff)<EOL><DEDENT>os.rename(filename, ff)<EOL><DEDENT>pickle.dump(save, open(filename, '<STR_LIT:wb>'), protocol=<NUM_LIT:2>)<EOL>
Save the current state with extra information (for example samples and LL
from the optimization procedure).

Parameters
----------
state : peri.states.ImageState
    the state object which to save

filename : string
    if provided, will override the default that is constructed based on
    the state's raw image file.  If there is no filename and the state has
    a RawImage, the it is saved to RawImage.filename + "-peri-save.pkl"

desc : string
    if provided, will augment the default filename to be
    RawImage.filename + '-peri-' + desc + '.pkl'

extra : list of pickleable objects
    if provided, will be saved with the state