Monday, August 8, 2016

Noise Fields, Noise Fields, Noise Fields

Noise Field (512 px x 512 px). An applied noise function. A.G. (c) 2016.

Here we have pretty much the same thing as before, i.e. a function called noisefield() that returns an image. This time what we did, or tried to do, was to generate a "LINE" first that is a list of 512 "RGB tuples" which in this case, as before, are randomly generated (via the noxel() function).

Then, simply enough, all we did was create an entity called "MATRIX" that is made up of 512 "LINEs". We used an "iter" function to create an object called "nixel", as you can see, that iterates over the "MATRIX" object which is basically just a very long sequence of RGB tuples made up of the concatenation of 512 "LINEs"

What you can see in the image above, created via this Python script, is an interesting series of LINEs, each with randomly chosen RGB tuple values.

Here is the code, hosted on Github as a Github Gist.

You might as well ask, since I know you're thinking it: Why go through all the troube? Couldn't you do this much more simply with less code and in a much more idiomatic style, plus much more elegantly? The answer is, yes, of course I could. This is only the beginning. I have structured the code this way because I want to be able to create what I call "mod" functions which are "modulations of the noise field", so to speak. I want to be able to create noise fields and then apply functions to them, either on a pixel-by-pixel basis or else on the whole image, or else on individual "LINEs" which in this case, as vectors if you will of random "RGB tuples", the LINEs that is, are actually "column" vectors and not "row" vectors, if you will. That is to say, the LINEs in the Image, of individual random RGB tuples, appear "horizontally", but the actual LINE in the code ends up being in the "vertical" direction. This is not a glitch, it's because in the code in the 5th line of the noisefield() function, we have (x,y), i.e.

im.putpixel((x, y), nixel.next())

We could just as easily make the uniform-colored lines appear on the vertical axis in the final image just by switching (x,y) to (y,x). To be continued...

Saturday, August 6, 2016

Prolegomenon to Noise Field Theory (via Signal Science)

Noise Field (512 px x 512 px). An applied noise function. A.G. (c) 2016.

Noise Field Theory is a somewhat complex domain that I have been investigating, exploring, now for many years. For now, I just want to share some actual working code in order to eventually develop the theory of Noise Fields in a way that people can actually understand. That way me critics can finally realize that I'm not so crazy after all.

Simply put, you have these "fields" called noise fields. The simplest way to imagine a noise field is to just think of a matrix of numbers, but not just any old matrix. Imagine a digital image, a Bitmap, say, that is 256 pixels by 256 pixels. The simplest form of a noise field is just such an image/matrix made up of random values.

As you shall see in the code, I create something called a "noxel" which is meant to be a kind of "pixel", i.e. a "noise pixel" = "noxel". In RGB, it has three values, each of which is a random number between 0 and 255, the possible values for an image inside the RGB model.

What we do is create an image and assign a new noxel to each pixel, creating essentially a random matrix, a.k.a. a noise field. What I will do a little later is explain what this all means, because I can assure you, there is a much deeper meaning that is not obvious at first. (It took me 20 years to get this far, this is not simple, yet it is quite elegant).

That's it for now. Stay tuned for more details on the noise field as data model - for we are using Python programming to explore Noise Field Theory. It becomes a language, if you will, to speak about noise fields. I will also show why Noise Field Theory is not only best explained through Python itself, but through what we have called Signal Science. In the above case in point, in the code, the "noxel" function is "passed" as an "input" into the "noisefield" function. This allows us to tackle the also complex subject of Signal Science, which essentially is a purely signal-based view of the world and everything in it. To be continued...

Wednesday, June 29, 2016

Pythonic Wrapper for Wikipedia API Redux

I mentioned a Pythonic wrapper for the Wikipedia API in a post on this blog a while back, about the so-called "Refcards-Project". Here I want to just "reflect" on this Wikipedia library/module for a few moments.

As stated before, it is easy enough to install, just type:

$ pip install wikipedia
So let's do something really simple. Let's take a Wikipedia page and extract a list of all the links found in that page (Links to Wikipedia pages, i.e. a list of the "titles" of the Wikipedia pages linked to WITHIN the given Wikipedia page we start with). It could go something like this:

>>> import wikipedia
>>> refcard = wikipedia.page("Reference card")
>>> refcard_links = refcard.links
>>> type(refcard_links)
<type 'list'>
>>> len(refcard_links)
18
>>> for i in refcard_links:
print i
Application program
Card stock
Cheat sheet
Computer platform
Data mining
Formal language
HTML
Idiom
Index card
LaTeX
Markup language
Memory
PDF
Printing
Programming language
R (programming language)
Syntax
User guide
That's it for now. I want to leave it at this as far as Python code goes, but as I said I want to "reflect" on this. I want to reflect on it in two fundamental ways. One way to reflect on Python code is to do it "philosophically" if you will. That's one form of reflection/introspection. You just "think" about it. The other way, though, is via the interpreter.

If you go back, you will see that I "asked" IDLE this question:


>>> type(refcard_links)
And what did it say, what was the answer?

>>> type(refcard_links)
<type 'list'>

Okay. So here's the point: I think it's important to "interrogate" the data that you are working with. It's important to know what the "types" of things are, and also many other "questions" can be asked. If you noticed, I also "asked" what the "length" of the list was:

>>> len(refcard_links)
18

Now, I'm not a computer scientist. I'm just an artist and researcher, a philosopher if you will. So for me "Reflection" just means "asking questions about something". You see, I "expected" that refcard_links would be of type list, but I had to make sure. I also wanted to know what the length was just in case it contained 13,000 different items. That would screw things up if I were writing a kind of web crawler that crawled every Wikipedia "page" in such a list of Wikipedia hyperlinks. Since there were only 18 items in the list (the "associative array") then I knew I was in business.

So what I could do, if I wanted, is write a simple script that takes any given Wikipedia page, given that it exists, and makes a list of say the first 5 links to other Wikipedia pages within that article. Then it could take say the first 2 links, and get/fetch those pages, and make lists of the first 5 links to THOSE pages, and so on and so forth.

I'm not trying to build a Wikipedia crawler, though. I'm actually much more interested in using Wikipedia to do things closer to what we might call Natural Language Processing. That is, as an author of fiction, I'd like to use content on Wikipedia to generate "cut-up novels", let's say, in the style of one William S. Burroughs.

Say I want to build a so-called "cut-up engine". I need text to "seed" the engine. So I take a bunch of pseudo-random Wikipedia pages, or Wikipedia pages that are "interlinked", and I fetch the content and then I can start building Markov chains or whatnot, using the content and other methods to generate new, creative texts algorithmically.

The possibilities are endless. Once you have a "source" of textual content, there are no limits to what you can do. I could build a conversational agent that uses Wikipedia content. Say someone says something... I can quickly scan the textual data of what they said, then find keywords, or try, then look them up.. then I can have a dozen Wikipedia pages about the different "objects" or events or people or places WITHIN THE TEXT I am analyzing.

Granted, I will need much more complex "machinery" under the hood, but to me this simple Wikipedia library/module already offers me a vast FIELD OF OPPORTUNITIES for testing my Pythonic skills. That's all for now. Just a reflection on how to simply get something going, how to start thinking about the possibilities of what can be built with just a quick "skim" over the basics of a given Python library or module. The rest is just up to two t hings: a) how much work you're willing to put into it, including further research and so forth and b) how fertile your imagination is, or how creative you are. The rest is just details.

I could also do simple data analysis. I could want to see how many pages link back to some given page. I could make graphs or networks of hyperlinks between Wikipedia pages. I could do visualizations. Hey look, this many pages link back to the "Philosophy" page. The links that appear in the first paragraph on any given Wikipedia page seem to have this or that property or attribute or characteristic. That's all data science is, data analysis. You need to start SOMEWHERE, though. And as an amateur/beginner in Python, I want to make things as fun as possible. So I explore libraries and modules like this to see what is the SIMPLEST thing I could build to do something INTERESTING.


Sunday, June 12, 2016

[FIELDS_MEDALISTS]

On the Wikipedia page for the "Fields Medal", there is a table of Fields Medalists who were awarded the very high honor in the field of Mathematics. For fun, I took the data from this table and started playing around with the content in the "citation" column.

That is to say, in the table on the Fields Medal Wikipedia page, one has "fields" of "metadata" if you will, for Year, ISM Location, Medalists, Affiliation (when awarded), Affiliation (current/last), and one for Citation. An example of one of the "records" in the table is (in a form of comma-separated values styled representation):

1936, "Oslo, Norway", "Lars Ahlfors", "University of Helsinki, Finland", "Harvard University, US", "Awarded medal for research on covering surfaces related to Riemann surfaces of inverse functions of entire and meromorphic functions. Opened up new fields of analysis."

What I wanted to do, in Python, was make a "list" ("associative array") of all of the data from the table. I would then play with random selections of given "citation" data in the table.

I did this and I had a lot of fun. Without going into too many details, let's say that I created a list with just the citations, if that's all I want. I would make a statement starting with:

FIELDS_MEDALISTS = [...]

...with a "nested list" of the citations, within the FIELDS_MEDALISTS list, or whatever I want to call it, i.e. "CITATIONS", etc.

Once I had access to the citation data, the text or content of them, if you will, I thought that I could randomly choose citations. Ideally, I'd like to be able to do something slightly more sophisticated with the text of the citations, like make a word cloud of the words, minus stop-words, that are most common.

I think that you can see where I am trying to go with this: CREATE A RANDOM FIELDS MEDALIST CITATION (probably using markov chains, not sure yet). The idea, then, would be to write a kind of "FIELDS MEDALIST PREDICTOR".. that optimally would be ablt o predict future winners of the award. Although, honestly, I doubt I can do a very good job, since I'm not yet all that masterful when it comes to machine learnig and so forth. But the dataset itself, the "table" I called it, is pretty small. There haven't been all that many Fields Medalists in human history.

[more to come]...

Tuesday, April 5, 2016

Introduction To The Refcards Project

Hi, all. I've been semi-secretly working on a project that I internally am calling The Refcards Project. It has gone by mamy names, and in any case for now let's just call it that.

So here it is. I am doing many things as a burgeoning young programmer, and here is some of that. First of all, I'm working on what I am calling "Internals". In Python, since everything I do in Python, "internals" just means the stuff that happens "internally", like at the level of compilation, at the level of the interpreter and so forth. I'm only interested in the stuff that happens "under the hood" when you actually "run Python" (whatever that means!).

So that's one project. Another is called The Datatypes Project: Abstract Assembly. I will get into that later. Another is called The Archives-Project, and in part it is a new novel I've been working on for many years, but it goes much further than that. Also a topic for another time.

Right now, though, I want to introduce this cute little Python module simply called "wikipedia". The wikipedia module is going to help me actually compose the very first official Refcards app.

Installing it is simple, type:

$ pip install wikipedia

You can instantly start doing interesting things. Check out the documentation. Here is a simple example of what you can do, from the documentation:

>>> import wikipedia
>>> wikipedia.search("Barack")
[u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama']
>>> wikipedia.suggest("Barak Obama")
u'Barack Obama'

That's all there is to it, really. The possibilities are endless, though. Also from the quickstart guide, you can get a summary of an article simply, by using  wikipedia.summary:

>>> wikipedia.summary("GitHub")
2011, GitHub was the most popular open source code repository site.\nGitHub Inc. was founded in 2008 and is based in San Francisco, California.\nIn July 2012, the company received $100 million in Series A funding, primarily from Andreessen Horowitz.'
>>> wikipedia.summary("Apple III", sentences=1)
u'The Apple III (often rendered as Apple ///) is a business-oriented personal computer produced and released by Apple Computer that was intended as the successor to the Apple II series, but largely considered a failure in the market'

Anyway that's it for now. I will be coming back to this. Just keep an eye out for future blog posts on both The Internals Project AND The Refcards Project. The idea is that I can create a simple app that does nothing more, really, than reading out the first couple of lines from any given Wikipedia article.

The thing is, though, that with other Pythonic magic, it's pretty easy to take a module like the wikipedia module and make it do interesting things. For instance, I can not only fetch the first few lines of an article, I can also basically "crawl" the page for certain links. With a little Pythonic magic, some regular expressions or whatnot, or an interesting chunk of code called "pattern", I can do amazing things just with a simple Wikipedia page.

The module already lets me fetch the "categories" for a given page. Granted, I will need to do some Natural Language Processing magic, but that's pretty easy in Python. So basically I can create a simple app that interfaces with Wikipedia in amazing ways. I can make it get more intelligent AS YOU USE IT, and I can make everything get stored automatically on Dropbox, or even as Gist on Github using the Github API.

Python is amazing in that way, it has a million and one ways it has been extended, and the tools exist to do just about everything. Anything you can think, you can likely do in Python. With that being said, I don't like Python as a language. I think it is too slow. I think that if you're going to write code in Python, you should just learn to write it in C. Period. Don't use Python, use C. You're just wasting your time with just a bunch of bloat and candy and sugar. Fuck it, it's not worth it.

But it's the only language I currently know how to code in. So I "tolerate" it, at least until I finish learning how to write in C. And I'm getting there. But that might just have to be on another blog, since this blog is called Pythonic Reflections. But that's what the output of my reflections have been, so far, that if you're going to write in Python, learn to write in C. Don't waste everyone's time. It ends up in C in any case. Python is just bloated complicated and slow C code. It's like deciding to speak in English, but deciding to speak in English but backwards. Okay, people will understand you as long as they have a "Reverse The Backwards" interpreter. But it's a waste of time, you are better to speak "Forwards English rather than the Backwards variety.

In short, that's what Python is: Backwards C.


Saturday, March 19, 2016

Testing The Django Framework - Documenting The Process

I have decided to go through the installation of the Django framework on my Windows machine. I am doing the same thing simultaneously on my Linux machine, but am currently only documenting the process on Windows. There are many reasons for this, mainly because doing anything with Python on a Windows machine is categorically more difficult than doing just about anything on a Linux machine. In other words, if you are running Linux, you don't need a tutorial on installing and working with the Django framework. (More or less).

Basically, I just opened up a terminal/console and typed in:

$ pip install Django==1.9.4

Then, for good measure, I typed the following, to make sure everything is working:

$ python -c "import django; print(django.get_version())"

This is all easy to find information on the Django project website. I'm just following the basic tutorials. The real reason I am doing this is not just to show how to install Django. It has more to do with the fact that I want to look at the way Django sets up projects.

In essence, I want to look at the directory structure and basic files of a "skinned down", "bare-bones" Django project. I plan to begin developing apps in Django, and will be doing it on Github, in Github repositories. I really want to get down to the fundamentals of the structure of a Django project. I decided to do this by documenting the entire process from start to finish. So this may just be the first post in a series of posts on the topic.

Now that Django is installed, I go onto the next command. First, I want to make sure that I start my first Django project in the right directory. It doesn't really matter where I stick the project, but I have a special folder called "Refcards" where I am doing most of my development. So I will "cd" into that directory, to create the Django project I will be starting following the tutorial (all on the Django project website, accessible to anyone with a web browser and an Internet connection).

Once I am in the proper directory, I type this in the console:

$ django-admin startproject mysite

This creates/generates the basic "scaffolding" for my Django app.

In essence, this is what my last command generated, this directory structure with these files:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

We will stop there for now and come back to this directory structure and these files, to see what they all mean.

I will quote directly from the Django project website, where I found this basic tutorial to build your first Django app:

These files are:

The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.

The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).

mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you’re a Python beginner.)

mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

We will come back to this in a following post. That's it for now. The main idea, for now, is the directory structure and files that Django created for us, what I called the "scaffolding", namely:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Next we will look at these files and see what they are all about.

Tuesday, March 15, 2016

Quick Thoughts On How I Became A Programmer

Dear readers,

Quickly, I'd like to tell you how I came to be a computer programmer (with proverbial beginner status don't worry!). Basically, 5 or so years ago, I was surfing the web like anyone else, and I stumbled upon these MOOCs, we called them, these Massive Open On-line Courses.

The first MOOC I took, I think, was on the Udacity platform. I took a course that taught me Python all while building an actual, working search engine. It was a really great course. It effectively taught me how to write programs in the Python language.

Then, of course, I ended up taking over 100 different MOOCs on a variety of subjects. I wasn't happy just knowing how to write code in Python. I wanted to learn the ins and outs of Computer Science, both the Theoretical side and the Practical.

So that's what I did. Once I knew how Python worked, then I started trying to figure out what I was going to build. There comes that time, right, in the early day's of one's new life as a programmer where one must decide on some "project" of some sort.

In any case, I ended up fiddling and tinkering with all sorts of scripts, playing around with different modules and libraries in Python. I ended up playing with the Twitter Search API, feeling super amazed at what I could do with so few lines of code. It was truly an amazing experience.

Then slowly the projects started getting a little bigger in scope and in scale. I ended up writing my first real program in Python. I will take a break but will get back to the topic. Here, for your viewing pleasure, is my first ever ACTUAL PYTHON PROGRAM. And it works too! It is an implementation, in Python, of a Geomancy type reading. Look it up! (Google query: GEOMANCY).


 Here is another related script I wrote in Python. This one draws a random Geomantic Figure.

Without Further Ado - Some Python Resources To Get You Started

What The Python???!

Greatings, visitors! Simply put, this blog is meant as a place for me to write about what I've learned about Python in the last 5 years, since I first learned how to program in Python.

Mostly, I want this to be a repository, if you will, of the many wonderful modules, libraries, and frameworks etc. that I have experimented with, or else discovered, during my daily practice of coding in Python. For all intents and purposes, even after 5 years, I'm still just a beginner as far as programming goes. However, I have spent countless hours going through hundreds if not thousands of different repositories of Python code, so I feel that I have a pretty good idea of what is out there in terms of resources for Python programmers.

I have had introductions to other languages and in the end, I don't really like Python all that much, but the fact of the sheer numbers of Python programmers out there makes it a great community, one that I am otherwise happy to belong to.

Python, as you know, is a "high-level, general-purpose" programming language, amongst other things, and that's kind of what I don't like about it. I don't like things to be sugar-coated. I don't necessarily want to work at a higher level. I don't find Python code all that readable, no matter how good the original coder is or was. I think it looks like spaghetti no matter what you code up in Python.

Nevertheless, as I said, Python has a huge community of users, developers, programmers, et al, and that makes it a good thing to have, I guess, an "asset" if you must. But as soon as I find something closer to my real needs, I'll probably switch to something else. In that regard, I'm relatively "agnostic" as far as Python goes. It gets the job done, at least for now. I'm not a "loyalist", though. Again, as soon as something more fitting comes along, I'll be the first to jump off the Python Mothership.

In any case, none of that affects the fact that this blog is going to be about my discoveries in Python-Land. Hope you enjoy my Reflections on Python // Pythonic Reflections. Of course, an introduction to a blog about All Things Python would not be worth its salt if it didn't have a comical piece of actual Python code. And so here goes nothing...

def WhatIsPython(garbage):
                    return garbage