0

I am trying to implement a modified version of the XOR example from the Neat-Python library but keep running into the following error when attempting to run the below code:

Exception Unhandled: 'module' object is not callable

I have had a look at this comprehensive answer with regards to this type of error, but have not been able to solve the issue.

I suspect the problem lies in the location of the config file in my directory, but not sure where it should be (very much an amateur when it comes to Python I'm afraid).

All examples using Neat-Python I have found seem to manage this using more or less the exact code found in the XOR example, so I'm sure there's something very basic I'm getting wrong.

Your help is highly appreciated.

Code in full as it's rather short:

from sklearn.preprocessing import Normalizer
import numpy as np
import pandas as pd
import neat 
import pickle
import os

#Load csv file and define input and output columns
datafile = pd.read_csv('C:/Users/`/Desktop/Python Projects/BFA Dirty - NEAT-Python/BFADirty-train-header.csv')
datafile = datafile.set_index(["Date"])
df = datafile.values
df_input = df[:,0:39]
df_output = df[:,39]

#Normalise input for use in neural network (output is binary and need not be touched)
scaler = Normalizer().fit(df_input)
df_input_norm = scaler.transform(df_input)

#Show normalised input file
#np.set_printoptions(precision=3)
#print(df_input_norm)

def eval_genomes(genomes, config):
    for genome_id, genome in genomes:
        genome.fitness = 4.0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        for dfi, dfo in zip(df_input_norm, df_output):
            output = net.activate(dfi)
            genome.fitness -= (output[0] - dfo[0]) ** 2


def run(config_file):
    config = neat.config(neat.DefaultGenome, neat.DefaultReproduction, 
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file) # <--- Error occurs here (line 35)

    #Create the population
    p = neat.population(config)

    # Add a stdout reporter to show progress in the terminal.
    p.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    #p.add_reporter(neat.Checkpointer(5))

    #run neural net
    winner = p.run(eval_genomes)

    # Display the winning genome.
    print('\nBest genome:\n{!s}'.format(winner))

if __name__ == '__main__':
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-feedforward.txt')
    run(config_path) #Line 55

EDIT: the error is triggered by config_file line. Comment added in the code to highlight it.

EDIT2:

Call Stack

Locals

Traceback (most recent call last):
  File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 119, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py", line 39, in debug
    run()
  File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\`\Desktop\Python Projects\BFA Dirty - NEAT-Python\BFA_Dirty___NEAT_Python.py", line 57, in <module>
    run(config_path)
  File "C:\Users\`\Desktop\Python Projects\BFA Dirty - NEAT-Python\BFA_Dirty___NEAT_Python.py", line 35, in run
    config_file)
TypeError: 'module' object is not callable
youjustreadthis
  • 622
  • 3
  • 9
  • 24

2 Answers2

1

I did a short search for neat examples and found CodeReclaimers example.

The code seems pretty similar to yours but here the following line is used:

p = neat.Population(config)

With a capital "P" instead of the lower case "p" you used. Thus calling The class Population instead of the module .

user2563336
  • 196
  • 1
  • 8
  • Thanks for this, which I'm sure will have saved me some grief down the line. It did not solve my problem however. As you suggested I have now added the call stack and the locals (must admit this aspect of debugging is new to me so I'm learning!) Does that help in locating the issue or is there something else needed? – youjustreadthis Feb 28 '20 at 20:24
  • Figured I'd accept this answer as it did prompt me to look for this type of error. – youjustreadthis Mar 01 '20 at 18:29
0

Finally found the solution. I was missing a capital C in config = neat.config.

The below works:

def run(config_file):
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, 
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file)
youjustreadthis
  • 622
  • 3
  • 9
  • 24