Sunday, August 29, 2010

R - Vectors, Lists, and DataFrames

  • A vector can be thought as contiguous cells containing elemental data.
Creation of vectors: x <- c(1,2,3)
Indexing: x[i], x[i,j]
Concatenating vectors x <- c(x,x)

  • A list (generic vector) has elements, each of which can contain any type of R object.
Creation of lists: x <- list(name_1=object_1, ..., name_m=object_m)
Indexing: x[[i]], x[[i,j]], x$a, x$"a", x[["a"]]
Concatenating lists: list.ABC <- c(list.A, list.B, list.C)
  • A data frame is a list with class "data.frame". There are restrictions:
    • The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames.
    • Matrices, lists, and data frames provide as many variables to the new data frame as they have columns, elements, or variables, respectively.
    • Numeric vectors, logicals and factors are included as is, and character vectors are coerced to be factors, whose levels are the unique values appearing in the vector.
    • Vector structures appearing as variables of the data frame must all have the same length, and matrix structures must all have the same row size.
Creation of dataframes: accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
Attach and dettach: (For lists in general). A useful facility would be somehow to make the components of a list or data frame temporarily visible as variables under their component name, without the need to quote the list name explicitly each time.The attach() function takes a `database' such as a list or data frame as its argument.

Thursday, August 26, 2010

Django - Deploy an application to alwaysdata.com (French web site)

www.alwaysdata.com is a French web site which allows free django hosting (and others) for websites up to 10Mb.
The site includes a fantastic but in French documentation which explains step by step how to publish a django site:
http://wiki.alwaysdata.com/wiki/D%C3%A9ployer_une_application_Django

Remarks:
  • ssh and ftp connections are possible.
  • There is an admin site https://admin.alwaysdata.com/ from where we need to enable the ssh user before using it.
  • The database must be created from the admin site.
  • When we make changes we may need to restart the fcgi process. This must be done from the admin web site, with the processes option, by killing the process. Immediately it is restarted.
  • Notice, as it is explained in the instructions, that we create a public directory. Some content, specially the static files, must be in that directory to be served, although the development server does not use them, it uses the files in the typical directory.
  • Important! Don't forget to redirect all queries to the main domain to the specific folder /appname/public where the project relies. This must be done from the admin panel.
  • Instructions for working with the development server are in http://wiki.alwaysdata.com/wiki/Utiliser_le_serveur_de_d%C3%A9veloppement_int%C3%A9gr%C3%A9_de_Django

HTML: How do we include one HTML file into another?

Here it is explained:
http://www.boutell.com/newfaq/creating/include.html

It depends on the way we do it: server-side, client-side, etc.
The way I used was by client side (I needed to add the page to a blogger page).
The code I used worked fine!

Saturday, August 21, 2010

R - Fitting data using Linear Models

Linear Model explanation
http://stat.ethz.ch/R-manual/R-devel/library/stats/html/lm.html

Formula object explanation
http://stat.ethz.ch/R-manual/R-devel/library/stats/html/formula.html

Example

x <- seq(0, 10, 0.1)

y <- 1 + x + cos(x) + rnorm(x)
yy <- 1 + x + cos(x)

plot(x, y, lwd = 3)
lines(x, yy, lwd = 3)

# 1 (default) includes intercept - 0 no
fit <- lm(y ~ 1 + x + I(x^2) + I(cos(x)))
fit
summary(fit)

newdf <- data.frame(x <- seq(11,2000,0.1))
newdf <- data.frame(newdf,y <- 1 + newdf$x + cos(newdf$x) + rnorm(newdf$x))
newp <- predict(fit,newdf)

plot(newdf$x,newdf$y,lwd=3)
lines(newdf$x,newp,lwd=3)

Wednesday, August 18, 2010

Django - Deploy an application to djangoeurope.com

I followed these steps to deploy a Django application into djangoeurope.com:
  • Register to djangoeurope.com and enter to the Panel (panel.djangoeurope.com)
  • Click into the installdjango option and create a project (it is called application in the webpage).
  • Run python manage.py startapp appname (the application should be called equal to our project's application).
  • Access with ssh and sftp to our space and put our original application into the corresponding folder. Overwrite all files except settings.py and settings_development.py.
  • Merge the files settings.py from djangoeurope.com and the one of our application.
  • Copy the content of settings.py to settings_development.py.
  • Run python manage.py syncdb.
  • Modify file lighttpd/django.conf to be correct.
  • Syncronize with init/appname restart, init/lightppd reload and init/lightppd restart.
Useful utilities when working are the commands
zip -r file.zip directory
,
which compresses the directory into the file, and
unzip file.zip,
which uncompresses the directory.
Caution: if we are in folder a/ and we execute unzip b/file.zip, the content of file.zip will be extracted in our working directory a/!

Sunday, August 15, 2010

How to find the package where a file is?

You can search for an ubuntu package containing a specific file (such as "algorithm.sty") by going to http://packages.ubuntu.com/. Scroll down to where it says "Search the contents of packages".

Wednesday, August 11, 2010

Django - Utilities for a beginner

In this post I recopile useful information I have found when looking for help.

  • How to specify a multiple field primary key? It is complicated, but what we can do is to specify a set of fields to be unique in the meta class of our model.
http://stackoverflow.com/questions/1624257/django-or-similar-for-composite-primary-keys
http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
  • Dynamic Django Queries
http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/

  • How can I make request.session parameters visible to a template without passing them as a parameter?
http://stackoverflow.com/questions/335231/in-django-is-it-possible-to-access-the-current-user-session-from-within-a-custom

  • A good way to customize templates for visualizing forms and form errors:
http://mikepk.com/2010/08/python_django_forms_errors_fieldsets/

  • A free online book which explains Django:
http://www.djangobook.com/

  • How to navigate in a template in a one-to-many relation:
http://solutions.treypiepmeier.com/2009/01/21/one-to-many-or-many-to-many-relationships-in-django-templates/

  • A tutorial to build a first application in Django, in four steps:
http://docs.djangoproject.com/en/dev/intro/tutorial01/

  • Query sets in Django. How can we use a field of a related object inside a filter? With object1.filter(obect2__field="aaa")
http://stackoverflow.com/questions/1086341/help-with-django-querysets

  • A person who gave up Django explains its reasons:
http://groups.google.com/group/django-users/browse_thread/thread/394701c83497e405?pli=1

  • The Catalan Django group:
http://groups.google.com/group/django-cat

  • A Django users Google Groups group
http://groups.google.com/group/django-users

  • A comparison of CMSApplications for Django
http://code.djangoproject.com/wiki/CMSAppsComparison

  • How to pass initial values to a form without validating it?
http://stackoverflow.com/questions/1882616/pass-an-initial-value-to-a-django-form-field

The general explanation of form fields is here:
http://docs.djangoproject.com/en/dev/ref/forms/fields/

  • Where can we host our Django application? A list of the most popular webs and its prices:
http://code.djangoproject.com/wiki/DjangoFriendlyWebHosts

  • Using reverse() in HttpResponseRedirect to avoid having to hardcode a URL in the view function:
http://adil.2scomplement.com/2009/01/django-using-reverse-instead-of-hardcoded-urls/

  • Changing the Model Choice Field Queryset:
from django import newforms as forms
from django.contrib.auth.models import User

class ComplaintForm(forms.Form):
user = forms.ModelChoiceField(queryset=User.objects.none())
message = forms.CharField(widget=forms.Textarea())

def __init__(self, *args, **kwargs):
super(ComplaintForm, self).__init__(*args, **kwargs)
self.fields["user"].queryset = User.objects.filter(is_staff=False)

http://oebfare.com/blog/2008/feb/23/changing-modelchoicefield-queryset/

  • Official documentation about generic views
http://docs.djangoproject.com/en/1.2/ref/generic-views/

Saturday, August 7, 2010

Django - Deploy an application

Once we have a Django application finished, we probably want to put it in a "real" web server.

This page explains how we can do it, using Apache and mod_wsgi, a module that allows Apache to support any Python application which supports the Python WSGI interface, like Django. We briefly summarize the steps in this post.
sudo aptitude install libapache2-mod-wsgi
  • We can test it by creating a wsgi script and modifying the Apache configuration file httpd.conf to allow the access to this file. For example we can make an script called /usr/var/www/scripts/myapp.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]


and adding some commands to the httpd.conf file, found in Ubuntu inside /etc/apache2 folder.
  • To deploy our Django application, we must create a file called django.wsig inside a folder, like mysite/apache, with the following content:
import os
import sys

sys.path.append("django_folder_application_path")
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

and the httpd.conf file must have the content explained in this page.

  • We must take into accout several things:
    • Make sure the folders have the correct permissions. Specifically, they must have read and execute permissions for the www-data user, the apache server user.
    • If we are using sqlite databases, the database file and its directory must have writing permissions, as it is explained here. Also we must ensure that the db name contains the full path.