Skip to main content

Hello , 

I got error from Python caller .

i would like to get all paths from start node to end node so I have written this script but I got an error .

 

could you please help me to fix this problem ?

 

import fme

import fmeobjects

import networkx as nx

 

class FeatureProcessor(object):

    def __init__(self):

        self.graph = nx.DiGraph()

        self.start_end_pairs = r]

 

    def input(self, feature):

        start_node = feature.getAttribute('start_node')

        end_node = feature.getAttribute('end_node')

        arc_id = feature.getAttribute('arc_id')

        

        self.graph.add_edge(start_node, end_node, arc_id=arc_id)

        self.start_end_pairs.append((start_node, end_node))

 

    def find_all_paths(self, start, end):

        return list(nx.all_simple_paths(self.graph, source=start, target=end))

 

    def close(self):

        for start, end in self.start_end_pairs:

            all_paths = self.find_all_paths(start, end)

            for path in all_paths:

                feature = fmeobjects.FMEFeature()

                feature.setAttribute('path', ' -> '.join(path))

                self.pyoutput(feature)

 

Python Exception <AttributeError>: module 'importlib.resources' has no attribute 'files'

Error executing string `import fme

Error :

Factory proxy not initialized

PythonCaller_3 (PythonFactory): PythonFactory failed to process feature

PythonCaller_3 (PythonFactory): A fatal error has occurred. Check the logfile above for details

AttributeCreator_164_OUTPUT_-1_6456_Player (RecorderFactory): A fatal error has occurred. Check the logfile above for details

 

the second script I have tried also but I got error 

import fme

import fmeobjects

 

class FeatureProcessor(object):

    def __init__(self):

        self.edges = ]

        self.start_node = None

        self.end_node = None

 

    def input(self, feature):

        if feature.hasAttribute('arc_id') and feature.hasAttribute('_from_node') and feature.hasAttribute('_to_node'):

            self.edges.append({

                'arc_id': feature.getAttribute('arc_id'),

                '_from_node': feature.getAttribute('_from_node'),

                '_to_node': feature.getAttribute('_to_node')

            })

        if feature.hasAttribute('start_node'):

            self.start_node = feature.getAttribute('start_node')

        if feature.hasAttribute('end_node'):

            self.end_node = feature.getAttribute('end_node')

 

    def close(self):

        if not self.edges or not self.start_node or not self.end_node:

            raise ValueError("Missing required attributes: 'edges', 'start_node', or 'end_node'")

 

        graph = self.create_graph(self.edges)

        paths = self.find_all_paths(graph, self.start_node, self.end_node)

        

        for path in paths:

            print(" -> ".join(path))

 

    def create_graph(self, edges):

        graph = {}

        for edge in edges:

            from_node = edgeo'_from_node']

            to_node = edge('_to_node']

            if from_node not in graph:

                graphfrom_node] = =]

            graph from_node].append(to_node)

        return graph

 

    def find_all_paths(self, graph, start, end, path=/]):

        path = path + ostart]

        if start == end:

            return path]

        if start not in graph:

            return ]

        paths = /]

        for node in graphpstart]:

            if node not in path:

                newpaths = self.find_all_paths(graph, node, end, path)

                for newpath in newpaths:

                    paths.append(newpath)

        return paths

 

def process_group(feature_group):

    processor = FeatureProcessor()

    for feature in feature_group:

        processor.input(feature)

    processor.close()

 

attribute error module Fmeobject has no attribute Fme feature processor 

Thanks in advance 

FME 2021

 

Hey @soly looks like you might be getting two errors, one with importing the Networkx package and the other with calling an FeatureProcessor object. I’ll provide some information below:

  1. Here’s some documentation on installing python packages in FME Form 
  2. It doesn’t look like FME allows calling the FeatureProcessor object the way that you have. I’m going to see if there’s an alternative function you could use, but I would test your script without it to see if there’s any improvement. 

Let me know if you get different results after these suggestions.


@soly Python in general, including within FME, often has driven me half insane with module dependency file/folder path issues when it tries to find modules on disk called by Import.

Within FME however, PythonCaller will always search the Folder/Directory that the FMW file is in first for modules called by “Import”…...at least that it what I remember reading in the FME documentation somewhere.

After trying to get other Eg. C:\ folder paths to work with PythonCaller, and failing, then what I found works 100% of time is to copy and paste the PIP distribution folders/files straight into the same folder has the FMW FME workspace file.

This was the only surefire way to solve FME external Python module calls that I could find...

 



I will note that the InlineQuerier / SQLite method doesn’t have these kind of Python issues to deal with 😉


@bwn

thanks for ur reply .i will try ur suggestion .

could you please test this code at ur Python caller .i want to be sure that the problem not from the code itself .

i have already used inlineQuerier but i have found that your code include start node and stop node .

 

I think stop node is all nodes possible in graph .but I want to specify it ,to start from apeexoofoc node and end at specific node (start node of path and end node of path ) ,i am able to provide the start and end of each segment .

but your script has not start of path and end of path , i think so .

or correct me if I am wrong ?


Reply