Monday, June 15, 2015

Two-Vector Dictionary in R using "match"


Something common in R is to have two vectors representing a key-value dictionary, for example:


  • Car name
  • Car price

Let's see an example:

We created two vectors using the dataset "car.test.frame" included in the package rpart. The first vector contains the name of the cars (key) and the second vector the list of prices (value). The function "match" is used to look for a couple of cars in the vector of names. If there is a match we get the index in the car_name vector (in our case, 37 and 43). Now we can use the indexes to work with the vector car_price or with the original data frame as a key-value dictionary.

Here is the code:

library(rpart)
str(car.test.frame)

# Get the name of the cars
car_name = row.names(car.test.frame)
car_price = car.test.frame$Price

# List of car's names
cars = c("Volvo 240 4", "Ford Taurus V6")
match(cars, car_name)

# Get the price of the car
car.test.frame[match(cars, car_name),]$Price

# or using the value-vector
car_price[match(cars, car_name)]

Tuesday, June 9, 2015

Problems printing graphical contents / canvas in lastest version of Chrome.


In the last release of Chrome something was wrong printing canvas contents. To see the problem visit the website:

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

  1. Do you see the circles on the map?
  2. Print the page
  3. Observe the differents with the printing version. Do you see the circles? 
If the answer is not, this post solves your problem.


We can solve temporarily the problem until Google launch a new version of Chrome or an update, following the next steps:

  1. Go to: chrome://flags/   (writing "chome://flags/" in your navigation bar)
  2. Deactivate display list 2D canvas.
  3. Restart Chrome (clicking on "Restart" at the bottom of the flags page).
Sorry my Chrome is in German. chrome://flags


Try again printing the previous page and observe the map. Now you should see the circles on the map.






Wednesday, February 11, 2015

How to speed up your site with htaccess


Loading speed is a common problem considering that your website is downloading again and  again the same files (css, js, png,...).

Let's load a couple of modules:

  • mod_expires
  • mod_header

The idea is to use the max-age header parameter which lets us says "this file expires 1 week from today", which is simpler than setting an explicit date. The max-age is measured in seconds, ie. 1 day = 86400, 1 week=604800, and so on.

In uuntu we can write:
sudo a2enmod headers
sudo a2enmod expires
sudo service apache2 restart
Then edit your .htaccess and insert the folowing lines:
#
# Configure mod_expires
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
#

    ExpiresActive On
    ExpiresDefault A3600
    ExpiresByType image/x-icon A2592000
    ExpiresByType application/x-javascript A86400
    ExpiresByType application/javascript A86400
    ExpiresByType text/javascript A86400
    ExpiresByType text/css A86400
    ExpiresByType image/gif A604800
    ExpiresByType image/png A604800
    ExpiresByType image/jpeg A604800
    ExpiresByType application/font-woff A604800
    ExpiresByType application/octet-stream A604800

#
# Configure mod_headers
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
#

   
        Header set Cache-Control "max-age=86400, public"
   

   
        Header set Cache-Control "max-age=600, private, must-revalidate"
   

    Header unset ETag
    Header unset Last-Modified

Done!. You can try now and check if your page is using the cache.

Hope help



Monday, October 27, 2014

Port tunneling on Mac with SSH


Imagine you need to connect from your browser to another computer which has access to an specific service, por example a private control panel. How to do it using SSH on a mac?

Well, lets describe the problem:


I want to access to the Service 192.168.300.12:11801 which can be accesed only from the Server SSH 192.168.200.1. To increase the complexity of the problem, we should use HTTPS to access the Service, ie, the direct URL from Server SSH looks like: https://192.168.300.12:11801/index.html

Well, first we open a terminal and execute the command:

$ sudo ssh -L 443:192.168.300.12:11801 -p 22 -l username -N 192.168.200.1

The command opens a tunnel between our machine on port 443 to machine 192.168.300.12 using the port 11801 through 192.168.200.1. Just what we want. "-p  20" is only the SSH port, "-l username" is clear and "-N" indicates that we will not execute remove commands.

Now, we can simply set up a SOCKS-Proxy going to Mac > Installation > Network > More Options. Select the tab "Proxies" and enable the protocol SOCKS-Proxy. You need only to insert:
  • SOCKS-Proxy-Server: 127.0.0.1
  • Port: 443
Now, open your favorite browser and write the URL:
https://localhost/index.html
to access to the Service.

Hope help



How normalization between two numbers works


A friend asked me today about how to normalize a range into another one. Well, given a range from A to B we want to convert it to a scale of C to D, where A maps to C and b maps to D, and we want to do it with a linear function. For example, if C=1 and D=5, ie, interval [1,5], that means that A maps 1 and B maps 5.

The following linear equation does exactly what we need

f(x) = 1 + (x-A)*(5-1)/(B-A) 

because
f(A)=1 + (A-A)*(5-1)/(B-A) = 1+0 = 1 
and
f(B)=1 + (B-A)/(B-A)*(5-1) = 1 + 1*(5-1) = 5.

In general, the linear ecuation looks like:

f(x) = C + (x-A)*(D-C)/(B-A)
Easy peasy, isn't it?

Hope help!

Friday, October 24, 2014

NACE PHP Array - Statistical Classification of Economic Activities


The Statistical Classification of Economic Activities in the European Community commonly referred to as NACE, is a European industry standard classification system consisting of a 6 digit code.

Here I leave a PHP array with the categories of the first level. The European comunity give us a plain text file available hier.

A - Agriculture, forestry and fishing  
A1 - Crop and animal production, hunting and related service activities  
A1.1 - Growing of non-perennial crops  
A1.2 - Growing of perennial crops  
A1.3 - Plant propagation  
A1.4 - Animal production  
...


The PHP array version looks like:

public $NACE = array(
"A" => "Agriculture, forestry and fishing",
"A1" => "Crop and animal production, hunting and related service activities",
"A1.1" => "Growing of non-perennial crops",
"A1.2" => "Growing of perennial crops",
"A1.3" => "Plant propagation",
"A1.4" => "Animal production",
"A1.5" => "Mixed farming",
"A1.6" => "Support activities to agriculture and post-harvest crop activities",
"A1.7" => "Hunting, trapping and related service activities",
"A2" => "Forestry and logging",
"A3" => "Fishing and aquaculture",
"B" => "Mining and quarrying",
"B5" => "Mining of coal and lignite",
"B6" => "Extraction of crude petroleum and natural gas",
"B7" => "Mining of metal ores",
"B8" => "Other mining and quarrying",
"B9" => "Mining support service activities",
"C" => "Manufacturing",
"C10" => "Manufacture of food products",
"C11" => "Manufacture of beverages",
"C12" => "Manufacture of tobacco products",
"C13" => "Manufacture of textiles",
"C14" => "Manufacture of wearing apparel",
"C15" => "Manufacture of leather and related products",
"C16" => "Manufacture of wood and of products of wood and cork, except furniture; manufacture of articles of straw and plaiting materials",
"C17" => "Manufacture of paper and paper products",
"C18" => "Printing and reproduction of recorded media",
"C19" => "Manufacture of coke and refined petroleum products",
"C20" => "Manufacture of chemicals and chemical products",
"C21" => "Manufacture of basic pharmaceutical products and pharmaceutical preparations",
"C22" => "Manufacture of rubber and plastic products",
"C23" => "Manufacture of other non-metallic mineral products",
"C24" => "Manufacture of basic metals",
"C25" => "Manufacture of fabricated metal products, except machinery and equipment",
"C26" => "Manufacture of computer, electronic and optical products",
"C27" => "Manufacture of electrical equipment",
"C28" => "Manufacture of machinery and equipment n.e.c.",
"C29" => "Manufacture of motor vehicles, trailers and semi-trailers",
"C30" => "Manufacture of other transport equipment",
"C31" => "Manufacture of furniture",
"C32" => "Other manufacturing",
"C33" => "Repair and installation of machinery and equipment",
"D" => "Electricity, gas, steam and air conditioning supply",
"D35" => "Electricity, gas, steam and air conditioning supply",
"E" => "Water supply; sewerage; waste managment and remediation activities",
"E36" => "Water collection, treatment and supply",
"E37" => "Sewerage",
"E38" => "Waste collection, treatment and disposal activities; materials recovery",
"E39" => "Remediation activities and other waste management services",
"F" => "Construction",
"F41" => "Construction of buildings",
"F42" => "Civil engineering",
"F43" => "Specialised construction activities",
"G" => "Wholesale and retail trade; repair of motor vehicles and motorcycles",
"G45" => "Wholesale and retail trade and repair of motor vehicles and motorcycles",
"G46" => "Wholesale trade, except of motor vehicles and motorcycles",
"G47" => "Retail trade, except of motor vehicles and motorcycles",
"H" => "Transporting and storage",
"H49" => "Land transport and transport via pipelines",
"H50" => "Water transport",
"H51" => "Air transport",
"H52" => "Warehousing and support activities for transportation",
"H53" => "Postal and courier activities",
"I" => "Accommodation and food service activities",
"I55" => "Accommodation",
"I56" => "Food and beverage service activities",
"J" => "Information and communication",
"J58" => "Publishing activities",
"J59" => "Motion picture, video and television programme production, sound recording and music publishing activities",
"J60" => "Programming and broadcasting activities",
"J61" => "Telecommunications",
"J62" => "Computer programming, consultancy and related activities",
"J63" => "Information service activities",
"K" => "Financial and insurance activities",
"K64" => "Financial service activities, except insurance and pension funding",
"K65" => "Insurance, reinsurance and pension funding, except compulsory social security",
"K66" => "Activities auxiliary to financial services and insurance activities",
"L" => "Real estate activities",
"L68" => "Real estate activities",
"M" => "Professional, scientific and technical activities",
"M69" => "Legal and accounting activities",
"M70" => "Activities of head offices; management consultancy activities",
"M71" => "Architectural and engineering activities; technical testing and analysis",
"M72" => "Scientific research and development",
"M73" => "Advertising and market research",
"M74" => "Other professional, scientific and technical activities",
"M75" => "Veterinary activities",
"N" => "Administrative and support service activities",
"N77" => "Rental and leasing activities",
"N78" => "Employment activities",
"N79" => "Travel agency, tour operator and other reservation service and related activities",
"N80" => "Security and investigation activities",
"N81" => "Services to buildings and landscape activities",
"N82" => "Office administrative, office support and other business support activities",
"O" => "Public administration and defence; compulsory social security",
"O84" => "Public administration and defence; compulsory social security",
"P" => "Education",
"P85" => "Education",
"Q" => "Human health and social work activities",
"Q86" => "Human health activities",
"Q87" => "Residential care activities",
"Q88" => "Social work activities without accommodation",
"R" => "Arts, entertainment and recreation",
"R90" => "Creative, arts and entertainment activities",
"R91" => "Libraries, archives, museums and other cultural activities",
"R92" => "Gambling and betting activities",
"R93" => "Sports activities and amusement and recreation activities",
"S" => "Other services activities",
"S94" => "Activities of membership organisations",
"S95" => "Repair of computers and personal and household goods",
"S96" => "Other personal service activities",
"T" => "Activities of households as employers; undifferentiated goods and services producing activities of households for own use",
"T97" => "Activities of households as employers of domestic personnel",
"T98" => "Undifferentiated goods- and services-producing activities of private households for own use",
"U" => "Activities of extraterritorial organisations and bodies",

"U99" => "Activities of extraterritorial organisations and bodies");


Hope help!









Friday, July 11, 2014

MAMP. Access MySQL with PHP via CRON or Command line.


Again problems with MAMP.

I have a project in PHP which should access via CRON to MySQL running on MAMP.  With the default MAMP's configuration that is not possible. What to do?

OPTION A. Setting PATHs


1) Edit th .profile file (vi ~/.profile) and change the line

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

to

export PATH=/Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php5/bin/:/opt/local/bin:/opt/local/sbin:$PATH

2) Reload the .profile file

$ . ~/.profile

3) (Optional) If you have problems connecting to the database tray to create a symbolic link to /tmp/mysql.sock

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Or more professional, add the next lines to your /etc/my.cnf file:

[mysqld]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

[client]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

Note: You can use also /etc/profile file if you want to do it for the rest of users.

OPTION B. Configuring a php.ini


1)  Check if your php exec has a defined config file

$ php --ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File:         (none)
Scan for additional .ini files in: /opt/local/var/db/php5
Additional .ini files parsed:      /opt/local/var/db/php5/gd.ini,
/opt/local/var/db/php5/mbstring.ini,
/opt/local/var/db/php5/mcrypt.ini,
/opt/local/var/db/php5/mysql.ini,
/opt/local/var/db/php5/openssl.ini,
/opt/local/var/db/php5/zip.ini



2) Create/copy a default php.ini file in the config folder of your current PHP version. For example in my case: /opt/local/etc/php5/php.ini

Hope help!