quarta-feira, 29 de janeiro de 2014

NetworkX - Working with Graphs in Python #2 - Random Walks

This week we will work with the concept of Random Walks on Graphs.

A graph is, in very simple therms, a structure that associate Nodes with Edges. On example, think about a country, where the cities are connected with roads, so a City is a Node, and a Street that connects to another city is a Edge.

A random walk is a mathematic formalism of a path consisted of random "steps", a random choice of edges. For the statistical purpose, is a special case of a Markov Chain.

I've used a graph that is widely knows as Les Miserables, that connects the homonym book characters in order of apparition. The nodes are the characters and the edges are the amount of times that two characters appear together.


The code below executes 10 times the random walk with size 10000, starting on different nodes and random sorting the next path. The execution shows the 10 most visited vertex during the random walks. Don't forget to import NetworkX packages and MatPlotLib to plot the graphs on a window.

Random Walk Algorithm

#Reads the graph from a GML File, named lesmis.gml G = nx.read_gml("lesmis.gml",relabel=True) #Start the counter contador = 0 #Start execution counter execucoes = 0 #Execute 10 times this command sequence while execucoes < 10: #Choose a random start node vertexid = rdm.choice(G.nodes()) #Dictionary that associate nodes with the amount of times it was visited VisitedVertex = {} print("Execucao #%d" % (execucoes + 1)) #Execute the random walk with size 10000 (10000 steps) while contador < 10000: #Accumulate the amount of times each vertex is visited if vertexid in VisitedVertex: VisitedVertex[vertexid] += 1 else: VisitedVertex[vertexid] = 1 #Visualize the vertex neighborhood Vertex_Neighbors = G.neighbors(vertexid) #Choose a vertex from the vertex neighborhood to start the next random walk vertexid = rdm.choice(Vertex_Neighbors) #Iteration counter increment contador = contador + 1 #Organize the vertex list in most visited decrescent order mostvisited = sorted(VisitedVertex, key = VisitedVertex.get,reverse = True) #Separate the top 10 most visited vertex top_10 = mostvisited[:10] print(top_10) #Restart the cycle execucoes = execucoes + 1 contador = 0 print("Feche a janela de exibicao para encerrar.") #Draw graph to a window nx.draw_circular(G) plt.savefig('myfig') plt.show() This is useful to visualize the nodes that have more connections than others inside a graph. On a social network, this corresponds to a person that have many people connected to its profile.

Random Walks is a real complex concept, and you can check on Graph Theory books for more information. This is just a didatic sample to help you understand the concept.

Hope it's useful! Enjoy! \o

terça-feira, 28 de janeiro de 2014

Song Spot #2

This week I've rummaged my music library once more, now searching for a song to play on my graduation ceremony (yeah \o). Inside those j-rock/j-pop mountain, I've remembered some of cool "semi-classic" musics non-related to oriental music. This week I brought The Piano Guys (they have a channel on YouTube with all the clips produced - click HERE!).

Two of my prefered are:


You can buy the album collection from Amazon and iTunes.

I hope you enjoy it as much as I  =].


DX Review #1 - Xperia L Protective Cover

I was searching for a protective cover for my phone, since I'm not able to maintain its integrity for a long time without a protection. This king of things are a little difficult to find where I live, so I've used the mortal weapon to gadgets: DealeXtreme.

Most of you know what is DX, for those who do not, it's a chinese online shopping for everything (believe me). I usually buy electonic components - arduino, sensors, tools. This time, I've found a great cover for Xperia L.

This is very solid and fits perfectly on device, but is a little thicker than expected, making difficult to use the volume buttons, but not bad for the price.





 
 Also, I really recommend to buy the screen protector too. Right size, is very clear and you can use the touchscreen withouth prejudice.





Links for both products below:


Enjoy! =]

domingo, 19 de janeiro de 2014

NetworkX - Working with Graphs in Python #1

I'm a C/C++ programmer in essence (the language that I've most used during my graduation). Programming in C is essential for any human that will work with something related to IT, not just for learning a language, but to develop the "algorithmical intuition" on how to solve real-life problems.

C/C++ solves a huge amount of problems straight-forward but it can be a little boring to solve some problems, including working with "unconventional" paths. It's not impossible, but can be really painful and tedious.

During the course of Graph Theory I had the chance to work with Python (never programmed seriously in Python before - that's really natural and the learning curve is really fast) and NetworkX - a dedicated graph library for Python.

NetworkX eliminates all the worries with graph structures (nodes, edges, predetermined graphs) and allow to work with small to medium graphs with good performance. It have all support for graph files like GML and GraphML, and associated with MathPlot, can plot graphs directly from Python terminal. Really easy-to-use and straight-forward. The documentation is worth reading for more details.

Debian and Ubuntu have all the packages via repositories and you can install the necessary packages withouth trouble using aptitude or Synaptics, just search for python and networkx.

I recommend using an IDE for Python, specifically Ninja IDE. It will help you maintain your code clean and understandable for human-beings (because Python works with identation to isolate excerpts of code - it can turn into a real mess, believe me).

In the future I'll post some algorithms developed during my Graph Theory classes.

\o

Song Spot #1

Discovering new songs to update a little bit my overpast music library. This week, the japanese band Sakanaction. Cool selection of songs (mix of electronic and pop). Enjoy! =] Sakanaction - Music (Original title: サカナクション - ミュージック) Sakanaction - Yoru no Odoriko (Original title: サカナクション - 夜の踊り子)

terça-feira, 14 de janeiro de 2014

Players, players, players! Music Players!

I've been searching for an app that could manage my music library (near 8000+ musics, not so much, but it is a pain to search on WMP or musikCube - musikCube is a dead project, but I like the player so much that sometimes I use it just for the nostalgia).

Searching around blogs and other sources, just one conclusion, there are many players and just a little have a good support to manage oriental music (yes, japanese, korean, chinese - I'm a japanese descendant, judge me huahuahua). But, I have to admit, the player that is near to my needs is iTunes. Neither I have an iPod or any iThing, but it is just fine to use as a player on Windows (on Linux, I'm using Rhythmbox or Banshee, two good choices, none available on Windows unfortunatelly, moreover, there are ports of those two for Windows using Qt and KDE for Windows, without success I have to say, sad =[ ).

I'm a Linux and Android enthusiast, but Linux don't have a good support for games and multimedia yet (SteamOS, please, get out of this beta version ASAP!!), so I'm still locked on Uncle Bill's Windows for multimedia. On Linux, as I said, I'm using Rhythmbox or Banshee (Debian and ArchLinux, respectivelly), and those meet my needs. On Android, I was using Sony WALKMAN Player App, but it just messed all the infos about the songs on SDCard and after a little rage attack, I started to search for a new music player.

Many options: WINAMP, Real Player, BSPlayer - great names coming from the x86 to the Android domains. Any of those are tablet oriented - GUI messed and stretched to the limit of your screen, rotating menus ad infinitum if you tap your device. OK, must have millions of players - AAhnn NO! A tablet oriented player is a luxury on Google Play, but I found one. It's great and FREE - the (not) so famous doubleTwist Player. Good support for all the songs I had, connection to live radios and can manage even Podcasts!

Music Player Problem solved, at least for now. Links for most important apps below.

http://musikcube.com/

http://www.apple.com/itunes/?cid=OAS-US-DOMAINS-itunes.com

http://banshee.fm/download/

http://sourceforge.net/projects/rhythmbox/

http://www.doubletwist.com/

\o

domingo, 12 de janeiro de 2014

How to Install DSpace 3.1 on Ubuntu 12.04 LTS

Once upon a time (just kidding), i needed to install DSpace (an webapp that manage academic content - books, papers and other things) on a server running Ubuntu 12.04. Since the documentation don't have a straight-forward explanation about the installation, after some research on internet and some experimentation, I've compiled a guide to make DSpace work. This is a little modificated version of the tutorial on http://linuxhalwa.blogspot.com.br/2013/06/installing-dspace-3x-on-ubuntu-1204.html.

1) Installing Dependencies

Open a Terminal and execute the following commands (Those will install some dependencies):

$ sudo apt-get install openjdk-6-jdk -y
$ sudo apt-get install tasksel -y

Wait for the end of setup and execute:

$ sudo tasksel

A menu will appear with a list of packages. Select the following packages using spacebar and the directionals:

[*] LAMP server [*] PostgreSQL database [*] Tomcat Java server

Press tab to select OK, and enter. Packages will start to install.

If MySQL ask for a password, define a standard password (i.e. dspace).

Now, install Ant and Maven.

$ sudo apt-get install ant maven -y

2) Creating a database and users for DSpace

Log as postgresql standard user:

$ sudo su postgres

And execute

$ createuser -U postgres -d -A -P dspace

Enter the password for the new role (define it as dspace) and respond the question with 'n'.

Exit from the postgres user prompt:

$ exit

You will need to allow the dspace database user to connect to the database. Type in the command line:

$ sudo vi /etc/postgresql/9.1/main/pg_hba.conf

Go to the end of the file and insert:

local all dspace md5 

Save and exit (on vi, :wq!).

Now, restart the PostgreSQL database service:

$ /etc/init.d/postgresql restart

DSpace requires an UNIX user. Create this executing:

$ sudo useradd -m dspace
$ sudo passwd dspace

Define a password for dspace user (i.e. dspace)

Create a directory for dspace user:

$ sudo mkdir /dspace
$ sudo chown dspace /dspace

Create the dspace database:

$ sudo su dspace
$ createdb -U dspace -E UNICODE dspace

Configure Tomcat to know about the dspace webapp location:

$ sudo vi /etc/tomcat6/server.xml

Insert the following text above the </Host> tag:

<!-- Define a new context path for all DSpace web apps -->
<Context path="/xmlui" docBase="/dspace/webapps/xmlui" allowLinking="true"/>
<Context path="/sword" docBase="/dspace/webapps/sword" allowLinking="true"/>
<Context path="/oai" docBase="/dspace/webapps/oai" allowLinking="true"/>
<Context path="/jspui" docBase="/dspace/webapps/jspui" allowLinking="true"/>
<Context path="/lni" docBase="/dspace/webapps/lni" allowLinking="true"/>
<Context path="/solr" docBase="/dspace/webapps/solr" allowLinking="true"/>

Save and close the file.

3) Downloading and Installing DSpace

Open a Terminal and execute the following commands:

$ sudo mkdir /build
$ sudo chmod -R 777 /build
$ cd /build
$ wget http://downloads.sourceforge.net/project/dspace/DSpace%20Stable/3.1/dspace-3.1-release.tar.bz2 
$ tar -xvjf dspace-3.1-release.tar.bz2
$ cd /build/dspace-3.1-release
$ mvn -U package  

Wait for Maven bring the dependencies. After that execute:

$ cd dspace/target/dspace-3.1-build
$ sudo ant fresh_install 

Ant will build all the directory structure and prepare DSpace installation.

After Ant exits, fix Tomcat permissions and restart Tomcat Server, executing the following commands:

$ sudo chown tomcat6:tomcat6 /dspace -R

Creating an Administrator for DSpace:

Type the following commands and fill the required fields:

$ /dspace/bin/dspace create-administrator

4) Testing Dspace

Open a browser and type one of the following addresses (from the Host machine – where DSpace is installed):


Tomcat runs on 8080 port. The localhost refers to the local machine where DSpace is installed. XMLUI and JSPUI are two interfaces from DSpace application.


If you want to access DSpace from a remote computer (from internet, as an example), change 'localhost' to the IP Address that forward to the DSpace host machine. You will need to check your firewall rules about the 8080 port to make Tomcat webapps visible from network.