Skip to content

Instantly share code, notes, and snippets.

View mrkn's full-sized avatar
:octocat:

Kenta Murata mrkn

:octocat:
View GitHub Profile
[2] pry(main)> Pandas = PyCall.import_module('pandas')
/Users/mrkn/src/github.com/mrkn/pycall-next/lib/pycall/init.rb:20: warning: instance variable @handle not initialized
/Users/mrkn/src/github.com/mrkn/pycall-next/lib/pycall/init.rb:26: warning: loading in progress, circular require considered harmful - /Users/mrkn/src/github.com/mrkn/pycall-next/lib/pycall.bundle
from bin/console:10:in `<main>'
from /Users/mrkn/.rbenv/versions/2.4.1-o0/lib/ruby/gems/2.4.0/gems/pry-byebug-3.4.2/lib/pry-byebug/pry_ext.rb:11:in `start_with_pry_byebug'
from /Users/mrkn/.rbenv/versions/2.4.1-o0/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_class.rb:169:in `start'
from /Users/mrkn/.rbenv/versions/2.4.1-o0/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/repl.rb:15:in `start'
from /Users/mrkn/.rbenv/versions/2.4.1-o0/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/repl.rb:38:in `start'
from /Users/mrkn/.rbenv/versions/2.4.1-o0/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/input_lock.rb
require 'pycall/import'
include PyCall::Import
pyimport 'matplotlib.pyplot', as: 'plt'
plt[:plot].([1, 2, 3, 4, 5], 5.times.map { rand })
plt[:show].()
@mrkn
mrkn / bitmap.rb
Last active December 28, 2016 06:39
class Vocabulary
def initialize
@words = []
@index = {}
end
def <<(word)
unless @index[word]
@index[word] = @words.length
@words << word
require 'benchmark/ips'
Benchmark.ips do |x|
rat = 123456789123456789/987654321r
flt = 123456789.123456789
fix = 1234567890
big = 12345678901234567890
x.report('rat + fix') { rat + fix }
x.report('rat + big') { rat + big }
require 'daru'
require 'tempfile'
require 'open-uri'
def read_as_dataframe(url)
Tempfile.open('hash_bench') do |tmpfile|
tmpfile.puts "category\tx\ty"
open(url) do |io|
io.each_line do |line|
next unless line =~ /^TABLE/
@mrkn
mrkn / ex_01.rb
Last active August 20, 2016 06:59
m = 0
n = 5
10_000_000.times do
xs = Array.new(n) { 1.0 + 1e-6 * (0.5 - rand) }
sq_mean = xs.map {|x| x**2 }.sum / n
mean_sq = (xs.sum / n)**2
var = sq_mean - mean_sq
p [m += 1, var] if var.negative?
end
$ ./ruby -v -rbenchmark -e 'Benchmark.bmbm() {|x| x.report("even?") { 100_000_000.times { 1.even? } } }
'
ruby 2.4.0dev (2016-03-18 trunk 54163) [x86_64-darwin15]
Rehearsal -----------------------------------------
even? 5.470000 0.020000 5.490000 ( 5.525505)
-------------------------------- total: 5.490000sec
user system total real
even? 5.590000 0.020000 5.610000 ( 5.668936)
@mrkn
mrkn / zundoko.rb
Last active March 15, 2016 11:27
128バイトのズンドコキヨシ
z='ズン';d='ドコ';loop.lazy.map{[z,d].sample.tap{|x|$><<x}}.each_cons(5){|x|break if x.join==z*4+d};puts'キ・ヨ・シ!'
@mrkn
mrkn / rgb2lab.py
Last active March 19, 2018 20:03
color space conversion from RGB to CIELAB in PIL/Pillow (Japanese article) ref: http://qiita.com/mrkn/items/670e1622d41d2dcd26b4
from PIL import Image, ImageCms
im = Image.open(image_path)
if im.mode != "RGB":
im = im.convert("RGB")
srgb_profile = ImageCms.createProfile("sRGB")
lab_profile = ImageCms.createProfile("LAB")
rgb2lab_transform = ImageCms.buildTransformFromOpenProfiles(srgb_profile, lab_profile, "RGB", "LAB")
/*
* example.c
*
* This file illustrates how to use the IJG code as a subroutine library
* to read or write JPEG image files. You should look at this code in
* conjunction with the documentation file libjpeg.txt.
*
* This code will not do anything useful as-is, but it may be helpful as a
* skeleton for constructing routines that call the JPEG library.
*