def from_pickle(filename, convert=True): """ Load a pandapower format Network from pickle file INPUT: **filename** (string or file) - The absolute or relative path to the input file or file-like object **convert** (bool, True) - If True, converts the format of the net loaded from pickle from the older version of pandapower to the newer version format OUTPUT: **net** (dict) - The pandapower format network EXAMPLE: >>> net1 = pp.from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path >>> net2 = pp.from_pickle("example2.p") #relative path """ net = pandapowerNet(io_utils.get_raw_data_from_pickle(filename)) io_utils.transform_net_with_df_and_geo(net, ["bus_geodata"], ["line_geodata"]) if convert: convert_format(net) return net
def from_pickle(filename, convert=True): """ Load a pandapower format Network from pickle file INPUT: **filename** (string) - The absolute or relative path to the input file. OUTPUT: **net** (dict) - The pandapower format network EXAMPLE: >>> net1 = pp.from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path >>> net2 = pp.from_pickle("example2.p") #relative path """ if not os.path.isfile(filename): raise UserWarning("File %s does not exist!!" % filename) with open(filename, "rb") as f: if sys.version_info >= (3, 0): net = pickle.load(f, encoding='latin1') # with encoding in python 3 else: net = pickle.load(f) # without encoding in python 2 net = pandapowerNet(net) if convert: convert_format(net) return net
def from_pickle(filename, convert=True): """ Load a pandapower format Network from pickle file INPUT: **filename** (string or file) - The absolute or relative path to the input file or file-like object OUTPUT: **net** (dict) - The pandapower format network EXAMPLE: >>> net1 = pp.from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path >>> net2 = pp.from_pickle("example2.p") #relative path """ def read(f): if sys.version_info >= (3,0): return pickle.load(f, encoding='latin1') else: return pickle.load(f) if hasattr(filename, 'read'): net = read(filename) elif not os.path.isfile(filename): raise UserWarning("File %s does not exist!!" % filename) else: with open(filename, "rb") as f: net = read(f) net = pandapowerNet(net) for key, item in net.items(): if isinstance(item, dict) and "DF" in item: df_dict = item["DF"] if "columns" in item["DF"]: net[key] = pd.DataFrame(columns=df_dict["columns"], index=df_dict["index"], data=df_dict["data"]) else: net[key] = pd.DataFrame.from_dict(item["DF"]) if "columns" in item: net[key] = net[key].reindex_axis(item["columns"], axis=1) if "dtypes" in item: try: #only works with pandas 0.19 or newer net[key] = net[key].astype(item["dtypes"]) except: #works with pandas <0.19 for column in net[key].columns: net[key][column] = net[key][column].astype(item["dtypes"][column]) if convert: convert_format(net) return net
def from_pickle(filename, convert=True): """ Load a pandapower format Network from pickle file INPUT: **filename** (string or file) - The absolute or relative path to the input file or file-like object OUTPUT: **net** (dict) - The pandapower format network EXAMPLE: >>> net1 = pp.from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path >>> net2 = pp.from_pickle("example2.p") #relative path """ def read(f): if sys.version_info >= (3, 0): return pickle.load(f, encoding='latin1') else: return pickle.load(f) if hasattr(filename, 'read'): net = read(filename) elif not os.path.isfile(filename): raise UserWarning("File %s does not exist!!" % filename) else: with open(filename, "rb") as f: net = read(f) net = pandapowerNet(net) try: epsg = net.gis_epsg_code except AttributeError: epsg = None for key, item in net.items(): if isinstance(item, dict) and "DF" in item: df_dict = item["DF"] if "columns" in df_dict: # make sure the index is Int64Index try: df_index = pd.Int64Index(df_dict['index']) except TypeError: df_index = df_dict['index'] if GEOPANDAS_INSTALLED and "geometry" in df_dict["columns"] \ and epsg is not None: # convert primitive data-types to shapely-objects if key == "bus_geodata": data = { "x": [row[0] for row in df_dict["data"]], "y": [row[1] for row in df_dict["data"]] } geo = [ Point(row[2][0], row[2][1]) for row in df_dict["data"] ] elif key == "line_geodata": data = {"coords": [row[0] for row in df_dict["data"]]} geo = [LineString(row[1]) for row in df_dict["data"]] net[key] = GeoDataFrame(data, crs=from_epsg(epsg), geometry=geo, index=df_index) else: net[key] = pd.DataFrame(columns=df_dict["columns"], index=df_index, data=df_dict["data"]) else: # TODO: is this legacy code? net[key] = pd.DataFrame.from_dict(df_dict) if "columns" in item: try: net[key] = net[key].reindex(item["columns"], axis=1) except TypeError: # legacy for pandas <0.21 net[key] = net[key].reindex_axis(item["columns"], axis=1) if "dtypes" in item: if "columns" in df_dict and "geometry" in df_dict["columns"]: pass else: try: # only works with pandas 0.19 or newer net[key] = net[key].astype(item["dtypes"]) except: # works with pandas <0.19 for column in net[key].columns: net[key][column] = net[key][column].astype( item["dtypes"][column]) if convert: convert_format(net) return net