def parse(target, outnetwork=Network): """ NNXSerializer.parse(target) -> Network object Loads an external XML-preserved neural network into its "Network" object * "target" is either a filename or a file object * "outnetwork" defines the output wrapper (output class) """ if not issubclass(outnetwork, Network): raise TypeError("The argument 1 must be a class derived from " "Network") Validators.bfileobject(target) root = ET.parse(target).getroot() L = [[ NeuronView.merge( map(float, neuron.get(NNXSerializer.ATTR_WEIGHTS).split(",")), float(neuron.get(NNXSerializer.ATTR_BIAS))) for neuron in layer ] for layer in root] speed = float(root.get(NNXSerializer.ATTR_SPEED)) alg = root.get(NNXSerializer.ATTR_ALG) if hasattr(Algorithms, alg): return Network(*L, n=speed, algorithm=getattr(Algorithms, alg)) return outnetwork(*L, n=speed)
def parse(target, outresult=ImageSelection): """ ISSerializer.parse(target) -> ImageSelection Loads an image selection from its XML-representation. * "target" is either a filename or a file object * "outresult" is either the ImageSelection class of a subclass thereof """ if not issubclass(outresult, ImageSelection): raise TypeError("'outresult' argument be either the" "ImageSelection class or a subclass thereof.") Validators.bfileobject(target) root = ET.parse(target).getroot() snipfunc = root.get(ISSerializer.ATTR_SNIPPER) bwidth = int(root.get(ISSerializer.ATTR_BLOCK_WIDTH)) bheight = int(root.get(ISSerializer.ATTR_BLOCK_HEIGHT)) snipfunc = getattr(imgsnipper, snipfunc) S = outresult(bwidth, bheight, snipper=snipfunc) for image in root: path = image.get(ISSerializer.ATTR_PATH) rfunc = image.get(ISSerializer.ATTR_RESULT_FUNC) result = getattr(imgresult, rfunc).from_string(image.text) S.append((path, result)) return S
def write(source, target): """ ISSerializer.write(source, target) -> None Writes your image selection to the specified file * "source" is an instance of ImageSelection or of a subclass thereof * "target" is either the name of a file of a binary file object """ if not isinstance(source, ImageSelection): raise TypeError("Invalid source type. ImageSelection " "implementations are only acceptible") Validators.bfileobject(target) snipfunc, bwidth, bheight = source.snipper #root attributes contain snipper parameters root = ET.Element(ISSerializer.TAG_ROOT, attrib={ ISSerializer.ATTR_SNIPPER: snipfunc.__name__, ISSerializer.ATTR_BLOCK_WIDTH: str(bwidth), ISSerializer.ATTR_BLOCK_HEIGHT: str(bheight) }) #Sub elements provide image links and result values for path, result in zip(source.paths(), source.results()): sub = ET.SubElement(root, ISSerializer.TAG_IMG, attrib={ ISSerializer.ATTR_PATH: path, ISSerializer.ATTR_RESULT_FUNC: result.__class__.__name__ }) sub.text = result.to_string() ET.ElementTree(root).write(target)
def write(source, target): """ NNXSerializer.write(source, target) -> None Writes your neural network to the specified file * "source" is an instance of "Network" or of a subclass thereof * "target" is either a file name or a binary file object """ if not isinstance(source, Network): raise TypeError( "The neural network to serialize must be " "an instance of Network, or of a subclass thereof, " "Not %s" % type(source)) Validators.bfileobject(target) #root attributes countain overall parameters and layers root = ET.Element(NNXSerializer.TAG_ROOT, attrib={ NNXSerializer.ATTR_SPEED: str(source.speed), NNXSerializer.ATTR_ALG: source.algorithm.__name__ }) #SubElement function provides a way to create new sub-elements for #a given element. for layer in source._links: #traverse all layers sub = ET.SubElement(root, NNXSerializer.TAG_LAYER) for neuron in map(NeuronView, layer): ET.SubElement(sub, NNXSerializer.TAG_NEURON, attrib={ NNXSerializer.ATTR_WEIGHTS: ", ".join(map(str, neuron.weights)), NNXSerializer.ATTR_BIAS: str(neuron.bias) }) #When encoding is US-ASCII or UTF-8 ET's output is binary! #Because the output is binary only BufferedIOBase-like objects #are accepted. ET.ElementTree(root).write(target)