To force the system to free SWAP memory:

swapoff -a
swapon -a


Hence I created a function freeswap_fun at my .basrc with an alias freeswap:

freeswap_fun() {
swapoff -a
swapon -a
}
alias freeswap=freeswap_fun


This last week I used lost of Microsoft Excel files that I need in my R scripts. Hence I discovered the excellent and complete package XLConnect. But, for easy and fast working, its not the best solution.

So I codded a wrapper of XLConnect, calling it loadxls that allows to read and write Excel files in an easy way.

The R package loadxls can be installed from its github repository with:

devtools::install_github("carleshf/loadxls")


It implements only 4 functions: read_all, read_sheet, write_all and write_sheet.

# Functions

## read_all

read_all(filename, environment = parent.frame(), verbose = TRUE)


This functions reads a given Excel file and loads each sheet as a data.frame in the current environment. The created objects will have the name of the sheet.

## read_sheet

read_sheet(filename, sheetname, varname, environment = parent.frame(), verbose = TRUE)


This functions loads, instead of the full content of a file, only a given sheet. If the argument varname is supplied, the object loaded from the given sheet will take its name.

## write_all

write_all(..., filename, verbose = TRUE)


This function writes all the objects given though ... to a new Excel file, saving each objects as a new sheet.

## write_sheet

write_sheet(data, sheetname, filename, replace = FALSE, verbose = TRUE)


This function allows to save a single object to and giving the name of the sheet where it will be write.

I had a file containing ~1M SNPs in their rsid (and their position). I needed to complete the information with their chromosome.

Since the list is really large I use scan combined with a bash command (to know the length of the file).

I found this solution:

library("SNPlocs.Hsapiens.dbSNP.20120608")

## Create connection to big file
inputName <- "input_file.gen"
outputName <- "output_file.gen"
inputCon <- file(description = inputName, open = "r")
outputCon <- file(description = outputName, open = "w")

## We need to know the number of lines of the big file
## This will work for GNU/Linux environments
command <- paste("wc -l ", inputName, " | awk '{ print $1 }'", sep="") nLines <- as.numeric(system(command = command, intern = TRUE)) rm(command) ## Loop over the file connection until end of lines pb <- txtProgressBar(min = 0, max = nLines, style = 3) for(ii in 1:nLines) { readLine <- scan(file = inputCon, nlines = 1, what = "list", quiet = TRUE) ## Get the chr from the SNP x <- tryCatch({ x <- rsidsToGRanges(readLine[[2]]) as.character(x@seqnames[1]) }, error = function(e) { "---" }) ## Update on list and save on output readLine[[1]] <- x writeLines(paste(readLine, collapse = " "), outputCon) setTxtProgressBar(pb, ii) } close(inputCon) close(outputCon)  This solution is really slow but it gets the SNP’s chromosome and fills it as "---" when the SNP is not found in dbSNP. WARNING: The SNP’s rsid must be located into the second column of the file. Take a look at readLine[[2]] in the for. I am open to know different ways of doing this. Using bash and ImageMagick we can crop all the pictures in a folder in a single shoot: fix='crop' it=1 for file in$2/*;
do
if [[ -f $file ]]; then output=$2/${fix}${it}.png
echo "$file -->$output"
convert -crop $1 "$file" "$output" it=$((it+1))
fi
done


The script, called crop.sh is run as:

./crop.sh 525x240+675+150 ~/Pictures/


The first argument is the argument for the ImageMagick’s crop tool and the second argumen is the folder where the pictures to be cropped are.

Aquest post forma part d’una serie, aquí es poden trobar els altres:

Presentació
Dia 1 Dia 2 Dia 3 Dia 4 Dia 5
Dia 6 Dia 7 Dia 8 Dia 9 Dia 10

STEM Barcelona és un projecte educatiu de l’Ajuntament de Barcelona i amb col·laboració amb The New York Academy of Sciences.

STEM és l’acrònim de Science, Technology, Engineering and Maths.

L’objectiu és despertat l’interès per la ciència en els estudiants d’institut i fomentar l’interès per la docència en el personal científic:

1. Promoure els coneixements i les vocacions científiques.
2. Afavorir la igualtat d’oportunitats i contribuir a la reducció de l’abandonament escolar.
3. Millorar les competències socials, comunicatives i pedagògiques dels investigadors joves (estudiants de doctorat universitari), i despertar en ells l’interès per a l’educació.

Per tant, la seva implementació serà realitzada per estudiants voluntaris de doctorat d’universitat que exerciran la figura de mentor.

El contingut d’STEM Barcelona s’organitza amb 7 mòduls:

1. NUTRICIÓ
2. INTRODUCCIÓ A LA ROBÒTICA
3. JOCS MATEMÀTICS
4. QUÍMICA I SOSTENIBILITAT
5. CIÈNCIES DE LA VIDA
6. UNIVERS
7. ARRELS DE CIÈNCIA (per educació primària)

en aquest primer quadrimestres es realitzen els mòduls de l’1 al 5, i es preveu la realització de tots ells durant el segon quadrimestre.

Per tant es pot deduir que el període en que es porta a terme STEM Barcelona es de setembre a gener i inclou 12h de formació per als doctorands i 18h re practica en un centre educatiu (horari extra-lectiu).

Idealment, es treballarà en parella i les classes agruparan un màxim de 20 estudiants er primer cicle d’ESO supervisats per un responsable del centre educatiu i un membre del programa STEM Barcelona.

Tal i com s’ha comentat anteriorment, els mentors no reben compensació econòmica per la seva feina. Rebran crèdit acadèmic pel curs realitzat i se’ls cobreix la quota d’inscripció com a membres de la The New York Academy of Sciences / Science Alliance durant un any.

Pels estudiants és una activitat gratuïta i STEM Barcelona aporta el material experimental per a la realització de les activitats de forma gratuïta.

# Més informació:

• Díptic informatiu: aquí
• Publicació a la web del Consorci d’Educació de Barcelona: aqui
• Publicació a la web de l’Ajuntament de Barcelona: aquí
• Contacte i informació: email. hricomag@bcn.cat – tel. 93 291 84 78

Today I started my first kata at CodeWars as a sensei. The kata is based on an mobile game called Strata (android, iOS).

# The kata in CodeWars

The following sections corresponds to the kata I wrote for CodeWars. Now I’m working on the solution. When all the required stuff get finished I will write another post.

## Introduction

Cross-stitch is a popular form of counted-thread embroidery in which X-shaped stitches in a tiled, raster-like pattern are used to form a picture. Cross-stitch is the oldest form of embroidery and can be found all over the world. Many folk museums show examples of clothing decorated with cross-stitch, especially from continental Europe and Asia.

This kata tries to emulate a certain cross-stitch embroidery. We start from a squared canvas where certain tiles are colored, a pattern. The pattern is composed by colored-tiles and empty-tiles (gray in the following example). We need to embroider the canvas and cover each tile with a vertical strand and an horizontal strand. The tile will be colored with the color of the last strand that covers it. The empty tiles can be embroidered with any color.

Take the next picture as reference:

We start with a 2×2 canvas with a total of 4 tiles (1). We can embroider the first row with a blue strand (2). We can do the same for the second row with a purple strand (3). Embroidering the first column with a blue strand (4) we cover the blue tile in the first row with two strands. Since the strand on the top is blue, this tile is colored as blue. This makes the first tile be colored with the requested color.

Then, if we embroider the second column with a blue strand (5a) we fill the last tile (seconds row and second column) as blue, but the tile tells us that it must be colored as purple. So we not match the pattern. For hence, the strand on the second column needs to be purple (5b).

## Request

What is requested in this kata is to return a solution given a patterned-canvas. For hence, you are requested to implement the function find_solution that has a two arguments:

• canvas: This is an array of arrays representing the corss-stitch canvas (an array of rows).
• colors: This is an array of the colors of the pattern.

I must say that the board will always be squared. An example of canvas and colors is:

canvas = [['b', ' '], [' ', 'p']]
colors = ['b', 'p']


It must be understood as: The canvas has two rows and two columns. And it has two colors, blue (b) and purple (p).

The function find solution must return a valid solution to embroider the canvas and color the given pattern. This solution must follow a specific format detailed here:

1. It must be an array of hashes
2. Each hash can have only three keys: :type, :index and :color
3. The :type key can be 'r' for rows and 'c' for columns (string)
4. The :index key will be the index of the row or column to be colored (integer). It must be between 0 and board.size - 1
5. The :color key will be used to fill the row or column (string)

Warning: The canvas will be colore following the array’s order

An example of a solution for a 2×2 canvas:

[{:type=>"r", :index=>0, :color=>"b"}, {:type=>"r", :index=>1, :color=>"b"}, {:type=>"c", :index=>"0", :color=>"b"}, {:type=>"c", :index=>"1", :color=>"b"}]


Today I was working on a simple kata of CodeWars that required to work with combinations and permutations of elements given in array.

The number of permutations of the n elements in a set taken by groups of k is given by:

$\frac{n!}{(n-k)!}$

In this case, the order within the groups matters. If order does not matter, then we are talking about combinations:

$\frac{n!}{k!(n-k)!} = \binom{n}{k}$

The array class from ruby comes with a method (permutation) to get the permutations of its elements given a value k:

[1,2].permutation(0).to_a
=> [[]]
[1,2].permutation(1).to_a
=> [[1], [2]]
[1,2].permutation(2).to_a
=> [[1, 2], [2, 1]]
[1,2].permutation(3).to_a
=> []


In the same way, the class array comes with a method combination to get the combinations of its elements given a value k:

[1,2].combination(0).to_a
=> [[]]
[1,2].combination(1).to_a
=> [[1], [2]]
[1,2].combination(2).to_a
=> [[1, 2]]
[1,2].combination(3).to_a
=> []


So with these two methods we can get the sets results of permuting and combining the elements of an array but not to calculate the elements of a k-permutation of k-combination.

To do that I propose:

1. To include the method factorial to class Fixnum.
2. To include the methods perm_length and comb_length to class array

These two steps can be done as:

class Fixnum
def factorial
f = 1
(1..self).each{|ii| f *= ii }
return f
end
end

class Array
def perm_length k
return self.length.factorial / (self.length - k).factorial
end
def comb_length k
return self.length.factorial / (k.factorial * (self.length - k).factorial)
end
end


Something more complex would be needed for using this in a real scenario like a control for negative values of k. But for me it’s just enough :-)