Tuesday, February 19, 2019

Python Memo パイソンの学習メモ

 Python(パイソン)は、オランダ人のグイド・ヴァンロッサムによって開発されたオープンソースのオブジェクト指向スクリプト言語。イギリスのテレビ局 BBC が製作したコメディ番組『空飛ぶモンティ・パイソン』にちなんで名付けられた。Pythonという英単語は爬虫類のニシキヘビを意味し、Python言語のマスコットやアイコンとして使われることがある(wikipedia)。
Windows版はこちらから直接ダウンロードできます。

 Opencvを使う画像処理については、「【Python/OpenCV】画像処理入門・サンプル集」はよい学習材料です。

(2019年1月更新)
今は、AnacondaやConda、MiniCondaは一番簡単に使える環境です。

Windows環境でのファイル処理では、まずエンコードを正しく指定しないと、すぐエラーになります。例えば、UTF-8 with BOM(UTF-8-BOM)でエンコードされたテキストファイルを開く場合、以下のように指定する必要はあります。
  io.open(filename, "r", encoding="utf_8_sig")

str型(UTF-8)からunicode型に変換には、[1]

  uni_string = unicode(str_string, 'utf_8_sig')

 Python Imaging Library (PIL)と併用すれば、画像処理のプログラムだって簡単に書けます。
以下のスクリプトでは、Fractal(フラクタル)画像を作成する例です。
#!/usr/bin/python
import Image, ImageDraw, math, colorsys
dimensions = (800, 800)
scale = 1.0/(dimensions[0]/3)
center = (2.2, 1.5)       # Use this for Mandelbrot set
#center = (1.5, 1.5)       # Use this for Julia set
iterate_max = 100
colors_max = 50
img = Image.new("RGB", dimensions)
d = ImageDraw.Draw(img)
# Calculate a tolerable palette
palette = [0] * colors_max
for i in xrange(colors_max):
    f = 1-abs((float(i)/colors_max-1)**15)
    r, g, b = colorsys.hsv_to_rgb(.66+f/3, 1-f/2, f)
    palette[i] = (int(r*255), int(g*255), int(b*255))
# Calculate the mandelbrot sequence for the point c with start value z
def iterate_mandelbrot(c, z = 0):
    for n in xrange(iterate_max + 1):
        z = z*z +c
        if abs(z) > 2:
            return n
    return None
# Draw our image
for y in xrange(dimensions[1]):
    for x in xrange(dimensions[0]):
        c = complex(x * scale - center[0], y * scale - center[1])
        n = iterate_mandelbrot(c)            # Use this for Mandelbrot set
        #n = iterate_mandelbrot(complex(0.3, 0.6), c)  # Use this for Julia set
        if n is None:
            v = 1
        else:
            v = n/100.0
        d.point((x, y), fill = palette[int(v * (colors_max-1))])
del d
img.save("result.png")
以下は上のスクリプトで作成されたMandelbrotとJuliaフラクタル画像です。
PythonとPILライブラリで作成したJuliaフラクタル
PythonとPILライブラリで作成したMandelbrotフラクタル
以下は参考リンク[2]の「領域分割で減色」で処理した原画像と結果例です。
写真のアニメ絵化の結果
原画像(This person does not existプログラムで生成)

adaptiveThresholdを使ったエッジ抽出による結果
モルフォロジー変換のMORPH_OPENを使ってエッジを強調した[4]

参考リンク
[1] PythonでUTF-8 with BOMを開く
[2] 【Python/OpenCV】画像処理入門・サンプル集
[3]【Python/OpenCV】写真のアニメ絵化
[4] モルフォロジー変換
[5] python-izm:独学の無料サイト

Tuesday, February 5, 2019

The Saddest and Happiest English Words もっとも悲しいと幸せな英単語

dictionary.comによると、英語からもっとも悲しい単語や表現として以下のものを選びました。

  • Heartbroken
  • Back to school
  • Goodbye
  • If only
  • What might have been
  • Lonely
  • Love
  • Melancholy
  • Terminal
  • What party?
  • Time for bed
  • Alien
  • Almost
  • Forlorn
  • Bad news
  • Too late
  • No time
  • We're from Corporate and we're here to help
  • No more ice cream
  • We're from the Internal Revenue Service

例えば「back to school」は主に学生達に適用するもので、その親達にとってはむしろもっとも幸せなものです。

反対に、もっとも幸せなものは以下の通りです。これは、かなり「欧米」的で、ほぼ肥満に直結なものも結構あります。それに、褒め言葉も多いです。


  • Why don't you knock off early
  • Donuts in the break room
  • No homework this weekend
  • You're getting a raise (or bonus)
  • I appreciate you
  • I made cookies
  • I'm buying
  • Three-day weekend
  • I'm proud of you
  • I like your shirt!

How to detect Overdiagnosis 過剰診断の判断基準

文春より「日本では2割が過剰診断? リスクもある乳がん検診」と言う記事があります。

Wikipediaには、過剰診断の英語訳であるOverdiagnosisしかなく、その定義は「Overdiagnosis is the diagnosis of "disease" that will never cause symptoms or death during a patient's ordinarily expected lifetime」。さらに「 Overdiagnosis is a side effect of screening for early forms of disease.」つまり、生涯発祥しないか、死亡に至らない「病気」を診断することです。それは、病気の早期発見による副作用でもあります。

過剰診断は、無駄な医療(Unnecessary health care)、過剰利用(over utilization)、濃厚診療をもたらします。

theconversationの「Five warning signs of overdiagnosis」と言う記事には、診断に使われたテスト方法について、以下の5つのチェックポイントを挙げました。

  1. is there potential for more diagnoses with the new test?
  2. are more people actually being diagnosed by the new test?
  3. do the additional people diagnosed have milder or harmless forms of the disease?
  4. are more people being treated?
  5. might the harms of being treated outweigh the benefits?

簡単に訳すと、

  1. 既存のテスト方法より有効か
  2. このテスト方法ではほかのより多くの患者を見つけられたか
  3. より多く診断された患者の症状はより軽微か無害か
  4. より多くの患者が治療されているか
  5. 治療によって恩恵より損害の方が大きいか

theconversationの同記事には、甲状腺癌を例に取り上げました。一旦診断されると、その癌が小さくても、甲状腺が切除されるか、生涯甲状腺ホルモンの置き換え治療を受けることになります。しかし、ほっとおけば、害はほとんどありません。しかも、世界希望の調査によると、甲状腺癌の発見数は劇的に増えてきたにもかかわらず、それによる死亡率の向上は認められていません。

大阪大学医学系研究科甲状腺腫瘍研究チームより、「甲状腺がんの過剰診断Q&A」によると、「甲状腺がんの大部分は若年で発生し、若いうちは比較的速く成長ししばしば首のリンパ節にも転移しますが、その後徐々に成長を止めてほとんどが一生気づかれないまま経過します。これらのがんは早期診断・早期治療がふさわしくないがんです。」

preventingoverdiagnosisは過剰診断を阻止するために立ち上げられたサイトです。同サイトでは、喘息や、乳がん、高血圧などについて驚くほどの過剰診断が起こっていると警鐘を鳴らしています。また、過剰診断のきっかけはやはり、早期発見検査(screening programs)か健康診断によるものです(One common way overdiagnosis can happen is when healthy people who attend screening programs or receive tests during check-ups are diagnosed and subsequently treated for the early form of a disease which would never in fact have harmed them.)

H.ギルバート ウェルチ氏による「過剰診断: 健康診断があなたを病気にする」という本は関係書籍の一つです。