rpy2-example
changeset 0:688586ffd484 tip
repo ready for publishing
| author | Anibal Pacheco <apacheco.uy@gmail.com> |
|---|---|
| date | Sat Aug 22 00:50:47 2009 -0300 (2009-08-22) |
| parents | |
| children | |
| files | .hgignore __init__.py base_hogares.txt manage.py media/placeholder prueba/__init__.py prueba/models.py prueba/tests.py prueba/views.py settings.py templates/gini.html urls.py |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Sat Aug 22 00:50:47 2009 -0300 1.3 @@ -0,0 +1,11 @@ 1.4 +syntax:glob 1.5 +*.py[co] 1.6 +*.swp 1.7 +para_seba.txt 1.8 +base hogares.txt 1.9 + 1.10 +syntax:regexp 1.11 +^dist 1.12 +^.*\.egg-info 1.13 +^media/grafica.png 1.14 +^django$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/base_hogares.txt Sat Aug 22 00:50:47 2009 -0300 2.3 @@ -0,0 +1,5 @@ 2.4 +correlativ pesoano confI vivI mtI comuI prom_edu edad_prom edad_tramos Yajustado Ypercapita CLU_5 clusterfuz prob_1 prob_2 prob_3 prob_4 prob_5 2.5 +38667,00 14,00 -0,29 -0,12 -0,45 -0,19 5,00 29,80 3,00 11983,15 2396,63 2,00 2,00 0,08 0,03 0,19 0,27 0,43 1,00 2.6 +38668,00 13,00 -0,85 -1,64 -0,34 -0,80 4,50 21,25 2,00 17956,30 2244,54 1,00 1,00 0,06 0,03 0,69 0,10 0,12 2,00 2.7 +38670,00 14,00 -0,56 -0,33 0,09 -0,80 6,00 35,50 3,00 11365,05 5682,52 3,00 2,00 0,09 0,03 0,26 0,24 0,37 3,00 2.8 +38671,00 14,00 -0,56 0,40 -0,73 -0,19 8,00 57,00 5,00 4943,37 2471,68 2,00 2,00 0,13 0,04 0,15 0,31 0,36 4,00
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/manage.py Sat Aug 22 00:50:47 2009 -0300 3.3 @@ -0,0 +1,11 @@ 3.4 +#!/usr/bin/env python 3.5 +from django.core.management import execute_manager 3.6 +try: 3.7 + import settings # Assumed to be in the same directory. 3.8 +except ImportError: 3.9 + import sys 3.10 + sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) 3.11 + sys.exit(1) 3.12 + 3.13 +if __name__ == "__main__": 3.14 + execute_manager(settings)
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/prueba/models.py Sat Aug 22 00:50:47 2009 -0300 4.3 @@ -0,0 +1,3 @@ 4.4 +from django.db import models 4.5 + 4.6 +# Create your models here.
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/prueba/tests.py Sat Aug 22 00:50:47 2009 -0300 5.3 @@ -0,0 +1,23 @@ 5.4 +""" 5.5 +This file demonstrates two different styles of tests (one doctest and one 5.6 +unittest). These will both pass when you run "manage.py test". 5.7 + 5.8 +Replace these with more appropriate tests for your application. 5.9 +""" 5.10 + 5.11 +from django.test import TestCase 5.12 + 5.13 +class SimpleTest(TestCase): 5.14 + def test_basic_addition(self): 5.15 + """ 5.16 + Tests that 1 + 1 always equals 2. 5.17 + """ 5.18 + self.failUnlessEqual(1 + 1, 2) 5.19 + 5.20 +__test__ = {"doctest": """ 5.21 +Another way to test that 1 + 1 is equal to 2. 5.22 + 5.23 +>>> 1 + 1 == 2 5.24 +True 5.25 +"""} 5.26 +
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/prueba/views.py Sat Aug 22 00:50:47 2009 -0300 6.3 @@ -0,0 +1,49 @@ 6.4 +# Create your views here. 6.5 +from django.conf import settings 6.6 +from django.http import HttpResponse 6.7 +from django.shortcuts import render_to_response 6.8 + 6.9 + 6.10 +def prueba(request): 6.11 + 6.12 + from rpy2 import robjects 6.13 + 6.14 + r = robjects.r 6.15 + 6.16 + ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14]) 6.17 + trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69]) 6.18 + group = r.gl(2, 10, 20, labels = ["Ctl","Trt"]) 6.19 + weight = ctl + trt 6.20 + 6.21 + robjects.globalEnv["weight"] = weight 6.22 + robjects.globalEnv["group"] = group 6.23 + lm_D9 = r.lm("weight ~ group") 6.24 + anova = r.anova(lm_D9) 6.25 + 6.26 + return HttpResponse(anova) 6.27 + 6.28 + ##lm_D90 = r.lm("weight ~ group - 1") 6.29 + ##print(r.summary(lm_D90)) 6.30 + 6.31 + 6.32 +def gini(request): 6.33 + # importamos rpy2 y hacemos un shortcut para el objeto r 6.34 + from rpy2 import robjects 6.35 + r = robjects.r 6.36 + # cargamos la base y obtenemos el summary 6.37 + base = r['read.table'](settings.BASE_HOGARES, sep="\t", dec=",", h=True) 6.38 + summary = r.summary(base) 6.39 + # el vector utilizado para el grafico es la columna Yajustado tomada como fila 6.40 + grafico = base.r['Yajustado'][0] 6.41 + # cargamos el paquete ineq y hacemos la curva de Lorenz 6.42 + r.library('ineq') 6.43 + lc2 = r.Lc(grafico) 6.44 + # redireccionamos la salida grafica a un archivo png y ploteamos 6.45 + r.png(settings.MEDIA_ROOT + '/grafica.png') 6.46 + plot = r.plot(lc2) 6.47 + # apagamos la salida grafica para que nuestro plot se guarde correctamente 6.48 + r['dev.off']() 6.49 + # calculamos el Gini 6.50 + gini = round(r.ineq(grafico, type = r.c("Gini"))[0], 6) 6.51 + return render_to_response('gini.html', {'summary': summary, 'gini': gini}) 6.52 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/settings.py Sat Aug 22 00:50:47 2009 -0300 7.3 @@ -0,0 +1,84 @@ 7.4 +# Django settings for opp project. 7.5 +import os 7.6 + 7.7 +DEBUG = True 7.8 +TEMPLATE_DEBUG = DEBUG 7.9 + 7.10 +ADMINS = ( 7.11 + # ('Your Name', 'your_email@domain.com'), 7.12 +) 7.13 + 7.14 +MANAGERS = ADMINS 7.15 + 7.16 +DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 7.17 +DATABASE_NAME = '' # Or path to database file if using sqlite3. 7.18 +DATABASE_USER = '' # Not used with sqlite3. 7.19 +DATABASE_PASSWORD = '' # Not used with sqlite3. 7.20 +DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. 7.21 +DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. 7.22 + 7.23 +# Local time zone for this installation. Choices can be found here: 7.24 +# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 7.25 +# although not all choices may be available on all operating systems. 7.26 +# If running in a Windows environment this must be set to the same as your 7.27 +# system time zone. 7.28 +TIME_ZONE = 'America/Chicago' 7.29 + 7.30 +# Language code for this installation. All choices can be found here: 7.31 +# http://www.i18nguy.com/unicode/language-identifiers.html 7.32 +LANGUAGE_CODE = 'en-us' 7.33 + 7.34 +SITE_ID = 1 7.35 + 7.36 +# If you set this to False, Django will make some optimizations so as not 7.37 +# to load the internationalization machinery. 7.38 +USE_I18N = True 7.39 + 7.40 +# Absolute path to the directory that holds media. 7.41 +# Example: "/home/media/media.lawrence.com/" 7.42 +MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media') 7.43 +SERVE_MEDIA_FILES = True 7.44 + 7.45 +# URL that handles the media served from MEDIA_ROOT. Make sure to use a 7.46 +# trailing slash if there is a path component (optional in other cases). 7.47 +# Examples: "http://media.lawrence.com", "http://example.com/media/" 7.48 +MEDIA_URL = '' 7.49 + 7.50 +# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a 7.51 +# trailing slash. 7.52 +# Examples: "http://foo.com/media/", "/media/". 7.53 +ADMIN_MEDIA_PREFIX = '/media/' 7.54 + 7.55 +# Make this unique, and don't share it with anybody. 7.56 +SECRET_KEY = 'uso&a*=6@p3l!4%972mswvg7k@i+tcs6w^&i)axd!ipecgtjya' 7.57 + 7.58 +# List of callables that know how to import templates from various sources. 7.59 +TEMPLATE_LOADERS = ( 7.60 + 'django.template.loaders.filesystem.load_template_source', 7.61 + 'django.template.loaders.app_directories.load_template_source', 7.62 +# 'django.template.loaders.eggs.load_template_source', 7.63 +) 7.64 + 7.65 +MIDDLEWARE_CLASSES = ( 7.66 + 'django.middleware.common.CommonMiddleware', 7.67 + 'django.contrib.sessions.middleware.SessionMiddleware', 7.68 + 'django.contrib.auth.middleware.AuthenticationMiddleware', 7.69 +) 7.70 + 7.71 +ROOT_URLCONF = 'opp.urls' 7.72 + 7.73 +TEMPLATE_DIRS = ( 7.74 + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 7.75 + # Always use forward slashes, even on Windows. 7.76 + # Don't forget to use absolute paths, not relative paths. 7.77 + os.path.join(os.path.dirname(__file__), 'templates'), 7.78 +) 7.79 + 7.80 +INSTALLED_APPS = ( 7.81 + 'django.contrib.auth', 7.82 + 'django.contrib.contenttypes', 7.83 + 'django.contrib.sessions', 7.84 + 'django.contrib.sites', 7.85 +) 7.86 + 7.87 +BASE_HOGARES = os.path.join(os.path.dirname(__file__), 'base_hogares.txt')
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/templates/gini.html Sat Aug 22 00:50:47 2009 -0300 8.3 @@ -0,0 +1,48 @@ 8.4 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 8.5 +<html lang="en"> 8.6 + <head> 8.7 + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 8.8 + <title>Prueba de gráfica y cálculo de Gini con rpy2</title> 8.9 + <style type="text/css"> 8.10 + body{font-family:Verdana;} 8.11 + h1{font-size:1.5em;} 8.12 + h2{font-size:1em;} 8.13 + span{color:#808080;} 8.14 + </style> 8.15 + </head> 8.16 + <body> 8.17 + <h1>Prueba de gráfica y cálculo de Gini con rpy2</h1> 8.18 + <p>(Usando 'base hogares.txt')</p> 8.19 + <div><h2>"summary" de la base:</h2><pre>{{ summary }}</pre></div> 8.20 + <div> 8.21 + <h2>Plot de la curva de Lorenz de la columna "Yajustado"</h2> 8.22 + <img src="/opp/media/grafica.png" /> 8.23 + <p><strong>Gini:</strong> {{ gini }}</p> 8.24 + </div> 8.25 + <div> 8.26 + <h2>Código python utilizado para este ejemplo:</h2> 8.27 + <pre> 8.28 + <span># importamos rpy2 y hacemos un shortcut para el objeto r</span> 8.29 + from rpy2 import robjects 8.30 + r = robjects.r 8.31 + <span># cargamos la base y obtenemos el summary</span> 8.32 + base = r['read.table']("base hogares.txt", sep="\t", dec=",", h=True) 8.33 + summary = r.summary(base) 8.34 + <span># el vector utilizado para el grafico es la columna Yajustado tomada como fila</span> 8.35 + grafico = base.r['Yajustado'][0] 8.36 + <span># cargamos el paquete ineq y hacemos la curva de Lorenz</span> 8.37 + r.library('ineq') 8.38 + lc2 = r.Lc(grafico) 8.39 + <span># redireccionamos la salida grafica a un archivo png y ploteamos</span> 8.40 + r.png('grafica.png') 8.41 + plot = r.plot(lc2) 8.42 + <span># apagamos la salida grafica para que nuestro plot se guarde correctamente</span> 8.43 + r['dev.off']() 8.44 + <span># calculamos el Gini</span> 8.45 + gini = r.ineq(grafico, type = r.c("Gini"))[0] 8.46 + </pre> 8.47 + </div> 8.48 + <div id="footer"><p>prototipo realizado por Ing. Aníbal Pacheco con el único fin de probar la interoperabilidad entre python-rpy2 y django</p></div> 8.49 + </body> 8.50 +</html> 8.51 +
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/urls.py Sat Aug 22 00:50:47 2009 -0300 9.3 @@ -0,0 +1,29 @@ 9.4 +from django.conf import settings 9.5 +from django.conf.urls.defaults import * 9.6 + 9.7 +# Uncomment the next two lines to enable the admin: 9.8 +# from django.contrib import admin 9.9 +# admin.autodiscover() 9.10 + 9.11 +from prueba.views import prueba, gini 9.12 + 9.13 +urlpatterns = patterns('', 9.14 + # Example: 9.15 + # (r'^opp/', include('opp.foo.urls')), 9.16 + 9.17 + # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 9.18 + # to INSTALLED_APPS to enable admin documentation: 9.19 + # (r'^admin/doc/', include('django.contrib.admindocs.urls')), 9.20 + 9.21 + # Uncomment the next line to enable the admin: 9.22 + # (r'^admin/', include(admin.site.urls)), 9.23 + 9.24 + (r'^prueba/$', prueba), 9.25 + (r'^gini/$', gini), 9.26 + 9.27 +) 9.28 + 9.29 +if settings.SERVE_MEDIA_FILES: 9.30 + urlpatterns += patterns('', 9.31 + (r'^opp/media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), 9.32 + )
