<commit_before>import OpenPNM
from OpenPNM.Utilities import misc
import scipy as _sp
import numpy as _np
import pickle as _pickle
from xml.etree import ElementTree as _ET


class PNM(object):

    def __init__(self,**kwargs):
        super().__init__(**kwargs)

    @staticmethod
    def save(network,filename=''):
        r'''
        Save the current simulation in it's entirity.

        Parameters
        ----------
        net : OpenPNM Network Object
            The network object of the simulation to be saved.  This will
            automatically save all Geometry, Phases and Physics objects
            associated with the Network, but will not save any Algorithms.

        filename : string, optional
            The file name to yse for saving.  Defaults to the Network's name.

        Notes
        -----
        This stores the simulation in a nested dictionary with the data dict
        of the object stored under ['data'][object.name], the object linking
        under ['tree'][object.name] and the information to reproduce the models
        under ['mods'][object.name].  The ``load`` method knows how to unpack
        this dictionary.

        Examples
        --------
        >>> # Saving
        >>> pn = OpenPNM.Network.Cubic(shape=[3,3,3])
        >>> geo = OpenPNM.Geometry.Stick_and_Ball(network=pn,pores=pn.pores(),throats=pn.throats(),name='geo_1')
        >>> air = OpenPNM.Phases.Air(network=pn)
        >>> phys = OpenPNM.Physics.Standard(network=pn,phase=air,pores=pn.pores(),throats=pn.throats())
        >>> import OpenPNM.Utilities.IO as io
        >>> io.PNM.save(pn,'test_pn')

        >>> # Loading
        >>> import OpenPNM.Utilities.IO as io
        >>> #pn = io.PNM.load('test_pn')

        >>> # Delete the new file
        >>> import os
        >>> os.remove('test_pn.pnm')

        See Also
        --------
        IO.PNM.load

        '''

        if filename != '':
            filename = filename
        else:
            filename = network.name

        #Setup nested dictionary to store simulation
        sim = {}

        #Collect all objects into a single list
        all_objs = [network]
        all_objs.extend(network._geometries)
        all_objs.extend(network._phases)
        all_objs.extend(network._physics)

        #Enter each object's data, object tree and models into dictionary
        for obj in all_objs:
            del obj._logger
            sim[obj.name] = {}
            sim[obj.name]['data'] = obj.copy()
            sim[obj.name]['associations'] = {'Geometries' : obj.geometries(),
                                             'Phases'     : obj.phases(),
                                             'Physics'    : obj.physics()}
            sim[obj.name]['info'] = {'mro'   : [item.__name__ for item in obj.__class__.__mro__],
                                     'class' : obj.__class__}
            sim[obj.name]['models'] = {}
            for prop in list(obj._models.keys()):
                sim[obj.name]['models'][prop] = PNM._save_model(obj,prop)
        #Save nested dictionary pickle
        _pickle.dump(sim,open(filename+'.pnm','wb'))
<commit_msg>Fiddling with IO.PNM<commit_after>import OpenPNM
from OpenPNM.Utilities import misc
import scipy as _sp
import numpy as _np
import os as _os
import pickle as _pickle
from xml.etree import ElementTree as _ET


class PNM(object):

    def __init__(self,**kwargs):
        super().__init__(**kwargs)

    @staticmethod
    def save(network,filename=''):
        r'''
        Save the current simulation in it's entirity.

        Parameters
        ----------
        net : OpenPNM Network Object
            The network object of the simulation to be saved.  This will
            automatically save all Geometry, Phases and Physics objects
            associated with the Network, but will not save any Algorithms.

        filename : string, optional
            The file name to yse for saving.  Defaults to the Network's name.

        Notes
        -----
        This stores the simulation in a nested dictionary with the data dict
        of the object stored under ['data'][object.name], the object linking
        under ['tree'][object.name] and the information to reproduce the models
        under ['mods'][object.name].  The ``load`` method knows how to unpack
        this dictionary.

        Examples
        --------
        >>> # Saving
        >>> pn = OpenPNM.Network.Cubic(shape=[3,3,3])
        >>> geo = OpenPNM.Geometry.Stick_and_Ball(network=pn,pores=pn.pores(),throats=pn.throats(),name='geo_1')
        >>> air = OpenPNM.Phases.Air(network=pn)
        >>> phys = OpenPNM.Physics.Standard(network=pn,phase=air,pores=pn.pores(),throats=pn.throats())
        >>> import OpenPNM.Utilities.IO as io
        >>> io.PNM.save(pn,'test_pn')

        >>> # Loading
        >>> import OpenPNM.Utilities.IO as io
        >>> #pn = io.PNM.load('test_pn')

        >>> # Delete the new file
        >>> import os
        >>> os.remove('test_pn.pnm')

        See Also
        --------
        IO.PNM.load

        '''

        if filename != '':
            filename = filename.split('.')[0]
        else:
            filename = network.name

        #Setup nested dictionary to store simulation
        sim = {}

        #Collect all objects into a single list
        all_objs = [network]
        all_objs.extend(network._geometries)
        all_objs.extend(network._phases)
        all_objs.extend(network._physics)

        #Enter each object's data, object tree and models into dictionary
        for obj in all_objs:
            try:
                del obj._logger
            except:
                print('No logger found, ignoring request to delete it')
            sim[obj.name] = {}
            sim[obj.name]['data'] = obj.copy()
            sim[obj.name]['associations'] = {'Geometries' : obj.geometries(),
                                             'Phases'     : obj.phases(),
                                             'Physics'    : obj.physics()}
            sim[obj.name]['info'] = {'mro'   : [item.__name__ for item in obj.__class__.__mro__],
                                     'class' : obj.__class__}
            sim[obj.name]['models'] = {}
            for prop in list(obj._models.keys()):
                sim[obj.name]['models'][prop] = PNM._save_model(obj,prop)
        #Save nested dictionary pickle
        _pickle.dump(sim,open(filename+'.pnm','wb'))
#        #Save nested dictionary as a Numpy zip file
#        _sp.savez_compressed(filename,**sim)
#        #Rename the zip extension to pnm for kicks
#        try:
#            _os.remove(filename+'.pnm')
#        except:
#            pass
#        _os.rename(filename+'.npz',filename+'.pnm')

{'fraction_non_alphanumeric': 0.1201544630922323, 'fraction_numerical': 0.0013366998366255755, 'mean_word_length': 1.8827054794520548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '319303', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1856, 'n_words': 510}