aarongarrett / inspyred

Python library for bio-inspired computational intelligence
MIT License
187 stars 58 forks source link

optional file handle parameters to analysis #3

Closed abbgrade closed 10 years ago

abbgrade commented 10 years ago

now the file_overser can write into an StringIO, which can be passed to the analysis function

aarongarrett commented 10 years ago

I like the concept, but can you show me an example of how you would use this, just so I can understand what your end goal is?

abbgrade commented 10 years ago

here is a snippet

    import cStringIO as string_io

    evolution.observer = inspyred.ec.observers.file_observer

    statistics_file = string_io.StringIO()
    individuals_file = string_io.StringIO()

    final_population = evolution.evolve(
        generator=problem.generator,
        evaluator=problem.evaluator,
        pop_size=500,
        bounder=problem.bounder,
        maximize=problem.maximize,
        max_evaluations=5000,
        statistics_file=statistics_file,
        individuals_file=individuals_file
    )

    statistics_file.seek(0)
    individuals_file.seek(0)

    inspyred.ec.analysis.generation_plot(file=statistics_file)
    inspyred.ec.analysis.allele_plot(file=individuals_file)
aarongarrett commented 10 years ago

OK. In that case, it seems like a better approach would be just to change the "filename" parameter to instead be "file" and act as a file-like object. The client code would just have to make sure that they pass the already-opened file to the plot functions, if they save the data in an actual disk file.

How do you feel about that change? Does it have any issues that you can see? (It slightly changes the way those few functions accept parameters, which would make it not backward compatible to older code, but I'm not all that concerned about that if it makes the system cleaner, overall.)

abbgrade commented 10 years ago

Cleaner is better! ;)

It think, it is not so complex to call open outside if it is required.

aarongarrett commented 10 years ago

I made the changes in the current version of the repo. Thanks for the suggestion.