Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Creating the Fibonacci Sequence: Writing, Testi...
Search
onouyek
December 10, 2021
Programming
520
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Creating the Fibonacci Sequence: Writing, Testing, and Benchmarking Algorithms
【第4回】ゼロから始めるゲノム解析(Python編)
onouyek
December 10, 2021
More Decks by onouyek
See All by onouyek
Finding Open Reading Frames
onouyek
0
400
Inferring mRNA from Protein: Products and Reductions of Lists
onouyek
0
390
Finding the Longest Shared Subsequence: Finding K-mers, Writing Functions, and Using Binary Search
onouyek
0
310
Find a Motif in DNA: Exploring Sequence Similarity
onouyek
0
360
Finding the Hamming Distance: Counting Point Mutations
onouyek
0
470
Transcribing DNA into mRNA: Mutating Strings, Reading and Writing Files
onouyek
0
570
DNA methylation analysis using bisulfite sequencing data
onouyek
0
930
Operations on Genomic Intervals and Genome Arithmetic
onouyek
0
370
Exploratory Data Analysis with Unsupervised Machine Learning
onouyek
0
330
Other Decks in Programming
See All in Programming
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
430
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
300
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
210
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.2k
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.6k
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
The Invisible Side of Design
smashingmag
301
52k
Color Theory Basics | Prateek | Gurzu
gurzu
1
490
Git: the NoSQL Database
bkeepers
PRO
433
67k
Leo the Paperboy
mayatellez
10
2.4k
How to build a perfect <img>
jonoalderson
1
6.1k
WCS-LA-2024
lcolladotor
0
890
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
We Are The Robots
honzajavorek
0
400
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
540
The Limits of Empathy - UXLibs8
cassininazir
2
720
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
Transcript
【第4回】ゼロから始めるゲノム解析 (Python編) Creating the Fibonacci Sequence: Writing, Testing, and Benchmarking
Algorithms @onouyek
本勉強会の概要・目的 書籍名 対象者/目的 Mastering Python for Bioinformatics Python・バイオインフォ知識ほぼゼロの人 を対象に、正しいPythonのコーディング手 法について学ぶ
頻度 毎週〜隔週開催予定 登壇者 募集中!
Rosalindとは • 問題解決を通じてバイオインフォマティク ス、プログラミング、およびアルゴリズムを 学習するためのプラットフォーム • 大学やハッカソン、就職の面接にも 600回 以上の採用実績あり 参考:https://qiita.com/_kimoton/items/d534d0fa9b83dd7dc412
概要
環境構築 - 必要パッケージ群のインストール # 公開されているレポジトリからファイル群を取得 $ git clone https://github.com/kyclark/biofx_python $
cd biofx_python # requirements.txt に記載のパッケージをインストール $ pip3 install -r requirements.txt # pylintの設定ファイルをホームディレクトリに移動 $ cp pylintrc ~/.pylintrc # mypyの設定ファイルをホームディレクトリに移動 $ cp mypy.ini ~/.mypy.ini
本日のお題 http://rosalind.info/problems/fib/ 正の整数n(<=40)、k(<=5)が与えられる。1ペアから始めて、各世代において繁殖年齢のペアのウサギが kペ アのウサギの子孫を残すとしたとき、 nか月後に存在するウサギのペアの総数を求めよ。
本日学ぶこと • 引数の評価とエラーの出し方 • listのスタックとしての使い方 • ジェネレーター関数の書き方 • 再帰関数の書き方 •
メモ化を用いた高速化 • デコレーターの使い方
argparseの型チェック argparseのtypeにintを指定しておけば異なる型で入力されるとエラーになる $ ./fib.py 5 3.2 usage: fib.py [-h] generations
litter fib.py: error: argument litter: invalid int value: '3.2' $ ./fib.py -3 2 usage: fib.py [-h] generations litter fib.py: error: generations "-3" must be between 1 and 40 今回の問題の条件である 1<=n<=40、1<=k<=5を満たさない 場合にエラーを上げたい場合はどう すればよいか?
引数のバリデーション ①args.genはint型なので条件比較する ことができる ②条件に合わない場合は parse.error() でエラーを上げてプログラムを終了でき る ① ②
Solution 1: listをスタックとして用いる ①n=0、n=1のときのペア数0,1をスタックにいれておく ②n=2以降の計算結果(F n = F n-2 *litter
+ F n-1 )をスタックに追加 ③スタックに最後に入れた数が nヶ月後に存在するペア数 ① ② ③ スタック:LIFO (Last-In-First-Out) 1 3 2 1 3 2 Push Pop
Solution 2: ジェネレーター関数を使う ① ② ③ yieldを使うと関数の呼 び出しごとに結果が返 されるジェネレーターに なる
①x,yを0,1で初期化 ②1世代前の値を返す ③2世代前のxを1世代 前yのk倍に置き換え、1 世代前yを直近2世代の 和に置き換える x=0 y=1 yield x 0 0 x=0 y=1 yield y x=y*k=2 y=x+y=1 1 1 x=2 y=1 yield y x=y*k=2 y=x+y=3 1 2 x=2 y=3 yield y x=y*k=6 y=x+y=5 3 3 x=6 y=5 yield y x=y*k=10 y=x+y=11 5 4 x=10 y=11 yield y x=y*k=22 y=x+y=21 11 5 k=2の例
(参考)itertools.islice() for文の代わりにislice()を使えば、指定した 要素まで返すイテレーターが作られる islice(イテラブル, 要素数) リストにして最後の要素を取得
Solution 3-1: 再帰関数を使う ①終了条件:nが1または2になったら1を返す ②それ以外は2世代前と1世代前のfib()が呼ばれる 下図のようなスタックコールが作られていき計算される ① ② fib(5) fib(3)
fib(4) fib(2) fib(3) fib(1) fib(2) fib(1) fib(2)
Solution 3-2: メモ化による高速化 ① ② 毎回計算するのは効率が悪い ①空の辞書を作る ②辞書にない場合だけ計算する fib(5) fib(3)
fib(4) fib(2) fib(3) fib(1) fib(2) fib(1) fib(2)
Solution 3-3: メモ化(デコレーター) fib関数の上に@memoizeとすることでメモ化機 能を上書くことができる
Solution 3-4: functools.lru_cache() memoize関数を作らなくてもfunctoolsライブラリにlru_cache関数が使える
ベンチマークツール:hyperfine ①複数回実行した平均値がわかる ②サマリーで速度比較の結果がわかる demo ① ② https://github.com/sharkdp/hyperfine
randomを使ってランダム値でテストする ①random.choice()で与えられたシーケンスか らランダムに要素を返す ②random.randint(a, b)でa<=N<=bからラン ダムな整数を返す ① ② ① ②
makeコマンドからテストを実行する Makefileにテストコマンドを記述しておけばmake testでテストが実行できる
すべてのSolutionをまとめてテストする ①re.match()で正規表現にマッチする ファイルを取得 ②取得したファイルに対してテストを実行 ① ②
本日学んだこと • 引数のバリデーションとparser.error()によるエラーの上げ方 • listのスタックとしての使い方 • yieldを使ったジェネレーターの作成 • 再帰関数の書き方とメモ化による高速化 •
デコレーターの使い方 • hyperfineによるベンチマーキング • randomモジュールによる値のランダム生成