Tuesday, November 15, 2016

Busy, Busy, Busy

I've been a busy boy while going back for my masters, I'm currently taking 2 classes, and have been posting quite a few videos called 60 Seconds to Success in OMSCS. Anyhow if you're in the program and you want some helpful tips go check it out.

https://www.youtube.com/channel/UCR-mQpFEIiBWd164H4-yhgw

I'll try to come back once in a while and post some fun stuff.

Lately I've been doing Hough Transforms, Disparity Maps, solving linear algebra problems for stereo imaging and calculating Optical Flow on sets of images. It's been pretty wild, and I'm getting ready for the end (2 more assignments left), but I can't wait to get setup using lightshowpi for my christmas lights.

Saturday, March 12, 2016

Pickling for easier testing

Today I want to talk about Pickling. This approach allows you to save off a variable/data for re-use later.

Why would you want to do this? Well suppose you're like me and you're working on an AI agent, and you have a LOT of problems you need to run through, and all these problems take a little bit of time, and say 50 of them are already solved by your agent, but that 51st isn't, instead of running all 50 over and over (unless your agent is learning things, in which case, you're good). You can pickle the passed in variable, and then make a simple "test" function that will call only the one you want to call so that it's way easier to test out individual parts.

For more general information about pickling take a look here: https://wiki.python.org/moin/UsingPickle

Lets get started.

Import Pickle in your file with the following line added to the top of your Agent.py file:
import pickle

(I"m going to be using some verbiage from my AI course, however the basic idea is this, the agent we have starts with normal class name, __init__ and must implement a Solve function).

Make the start of your Solve function look like this:

   def Solve(self,problem):
      pickle.dump( problem, open( './pickles/' + problem.name + ".p", "wb" ) )

Finally Make a new directory called pickles so pickle can save it.

Finally run your agent like you normally would.

If you notice there should be a bunch of .p files in your pickles directory.

Next lets setup a "test" agent that allows you to run a SINGLE test (I know folks on our forums have asked about this, and well... here it is!

Create a new file called test.py

Inside it you would add the following

import pickle
from Agent import Agent

A = Agent()
problem  = pickle.load( open( "./pickles/Basic Problem E-09.p", "rb" ) )
A.Solve(problem)

Replacing the .p filename with whatever you choose.

You can make this more "generic" if you'd like by accepting command line input and then you run it like this .

test.py Basic Problem E-09 and it would run the one you command it to. Pretty Slick right?

That would look like this:

import pickle
from Agent import Agent
from sys import argv

A = Agent()
script, name = argv
problem  = pickle.load( open( "./pickles/" +  name + ".p", "rb" ) )
A.Solve(problem)

and be ran by this command.

python test.py "Basic Problem E-09"


The final test.py can be found here:
https://gist.github.com/onaclov2000/d1d7fc01b22b98e0098e