Python > makeMarkDownTable_Qxxx_180224.py > v0.1 > 計算結果の数値から計算をして、Markdownテーブル表記で出力する

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
```

### 処理概要

以下のような計算結果がある。ある値との誤差を計算したうえでMarkdownテーブル表記で出力したい。

```
30 4.94326 -11.7412598164 blue
60 4.536754 -15.8224580835 blue
100 4.58954 -20.7454559716 blue
300 4.444392 -22.6421296771 blue
500 4.349006 -23.5499560129 blue
1000 4.377727 -23.6671679161 blue
1500 4.382704 -23.5305418755 blue
2000 4.390335 -23.5956253908 blue
12 2.329375 -32.4526965388 red
42 5.006174 -20.9038079779 red
162 4.328343 -22.0221687607 red
642 4.382297 -23.5953656267 red
2562 4.383335 -23.5452229866 red
31 4.104669 -27.128399391 green
61 4.270889 -26.230112747 green
101 4.351807 -24.5505602615 green
301 4.392717 -23.5750447844 green
501 4.396591 -23.4863556788 green
1001 4.398315 -23.4478203585 green
1501 4.398605 -23.4405453547 green
2001 4.398712 -23.4379518368 green
```

### code v0.1

```makeMarkDownTable_Qxxx_180224.py
'''
v0.1 Feb. 24, 2018
  - print in the markdown format
  - add calc_error()
  - add ROW_START, ROW_STOP
  - add HN_pos[], IN_pos[], FN_pos[]
  - add Noris[], Qexts[], Qabss[]
  - add [STR_RES]
'''

# on Python 3.5.2
# conding rule: PEP8

# Output from [showNoriQxxx_180217.ipynb] v0.1
#   Nori, Qext, Qabs, name_color
#   ROW 0-7: Hammersley Nodes
#   ROW 8-12: Icosahedral Nodes
#   ROW 13-20: Fibonacci Nodes
STR_RES = '''30 4.94326 -11.7412598164 blue
60 4.536754 -15.8224580835 blue
100 4.58954 -20.7454559716 blue
300 4.444392 -22.6421296771 blue
500 4.349006 -23.5499560129 blue
1000 4.377727 -23.6671679161 blue
1500 4.382704 -23.5305418755 blue
2000 4.390335 -23.5956253908 blue
12 2.329375 -32.4526965388 red
42 5.006174 -20.9038079779 red
162 4.328343 -22.0221687607 red
642 4.382297 -23.5953656267 red
2562 4.383335 -23.5452229866 red
31 4.104669 -27.128399391 green
61 4.270889 -26.230112747 green
101 4.351807 -24.5505602615 green
301 4.392717 -23.5750447844 green
501 4.396591 -23.4863556788 green
1001 4.398315 -23.4478203585 green
1501 4.398605 -23.4405453547 green
2001 4.398712 -23.4379518368 green
'''

wrk = STR_RES.replace('\n', ' ').split(' ')
Noris = [wrk[i] for i in range(0, len(wrk), 4)]
Qexts = [float(wrk[i]) for i in range(1, len(wrk), 4)]
Qabss = [float(wrk[i]) for i in range(2, len(wrk), 4)]

# --- Configurations ---
# start, stop for different point sets
HN_pos = [0, 7]
IN_pos = [8, 12]
FN_pos = [13, 20]
# change following for each run (e.g. HN_...)
ROW_START = HN_pos[0]
ROW_STOP = HN_pos[1] + 1
# set answer to compare
ANS = Qexts[IN_pos[1]]
# set array to be compared (Qexts, Qabss)
CLM_A = Noris[ROW_START:ROW_STOP]
CLM_B = Qexts[ROW_START:ROW_STOP]
# ---


def calc_error(elemA, elemB):
    return abs(elemA - elemB) / abs(elemB) * 100

MD_TITLE = '''| 1 | 2 | 3 |
|:-:|:-:|:-:|'''

print(MD_TITLE)

for elemA, elemB in zip(CLM_A, CLM_B):
    err = calc_error(elemB, ANS)
    print("| %s | %s | %5.2f |" % (elemA, elemB, err))

```

### 実行例

```bash
$ python3 makeMarkDownTable_Qxxx_180224.py 
| 1 | 2 | 3 |
|:-:|:-:|:-:|
| 30 | 4.94326 | 12.77 |
| 60 | 4.536754 |  3.50 |
| 100 | 4.58954 |  4.70 |
| 300 | 4.444392 |  1.39 |
| 500 | 4.349006 |  0.78 |
| 1000 | 4.377727 |  0.13 |
| 1500 | 4.382704 |  0.01 |
| 2000 | 4.390335 |  0.16 |
```

Markdownとして見ると以下となる。

| 1 | 2 | 3 |
|:-:|:-:|:-:|
| 30 | 4.94326 | 12.77 |
| 60 | 4.536754 |  3.50 |
| 100 | 4.58954 |  4.70 |
| 300 | 4.444392 |  1.39 |
| 500 | 4.349006 |  0.78 |
| 1000 | 4.377727 |  0.13 |
| 1500 | 4.382704 |  0.01 |
| 2000 | 4.390335 |  0.16 |

あとは項目表記などを編集すれば出来上がり。


### 設定項目

用途に合わせてConfigurationsの項目を変更して使う。

HN_pos[1]の1はマジックナンバーだが、すぐ上を見れば分かるので、これはこのままとする。

### 派生コード

```makeMarkDownTable_S11Pol_180224.py
'''
v0.2 Feb. 24, 2018
  - alter comment for [STR_RES]
  - rename [Qabss] to [Pols]
  - rename [Qexts] to [S11s]
  - branched from [makeMarkDownTable_Qxxx_180224.py]
v0.1 Feb. 24, 2018
  - print in the markdown format
  - add calc_error()
  - add ROW_START, ROW_STOP
  - add HN_pos[], IN_pos[], FN_pos[]
  - add Noris[], Qexts[], Qabss[]
  - add [STR_RES]
'''

# on Python 3.5.2
# conding rule: PEP8

# Output from [showNoriMueller_180203.ipynb] v0.1
#   Nori, S11, Pol, name_color
#   ROW 0-7: Hammersley Nodes
#   ROW 8-12: Icosahedral Nodes
#   ROW 13-20: Fibonacci Nodes
STR_RES = '''30 4.94326 -11.7412598164 blue
60 4.536754 -15.8224580835 blue
100 4.58954 -20.7454559716 blue
300 4.444392 -22.6421296771 blue
500 4.349006 -23.5499560129 blue
1000 4.377727 -23.6671679161 blue
1500 4.382704 -23.5305418755 blue
2000 4.390335 -23.5956253908 blue
12 2.329375 -32.4526965388 red
42 5.006174 -20.9038079779 red
162 4.328343 -22.0221687607 red
642 4.382297 -23.5953656267 red
2562 4.383335 -23.5452229866 red
31 4.104669 -27.128399391 green
61 4.270889 -26.230112747 green
101 4.351807 -24.5505602615 green
301 4.392717 -23.5750447844 green
501 4.396591 -23.4863556788 green
1001 4.398315 -23.4478203585 green
1501 4.398605 -23.4405453547 green
2001 4.398712 -23.4379518368 green
'''

wrk = STR_RES.replace('\n', ' ').split(' ')
Noris = [wrk[i] for i in range(0, len(wrk), 4)]
S11s = [float(wrk[i]) for i in range(1, len(wrk), 4)]
Pols = [float(wrk[i]) for i in range(2, len(wrk), 4)]

# --- Configurations ---
# start, stop for different point sets
HN_pos = [0, 7]
IN_pos = [8, 12]
FN_pos = [13, 20]
# change following for each run (e.g. HN_...)
ROW_START = HN_pos[0]
ROW_STOP = HN_pos[1] + 1
# set answer to compare
ANS = Pols[IN_pos[1]]
# set array to be compared (S11s, Pols)
CLM_A = Noris[ROW_START:ROW_STOP]
CLM_B = Pols[ROW_START:ROW_STOP]
# ---


def calc_error(elemA, elemB):
    return abs(elemA - elemB) / abs(elemB) * 100

MD_TITLE = '''| 1 | 2 | 3 |
|:-:|:-:|:-:|'''

print(MD_TITLE)

for elemA, elemB in zip(CLM_A, CLM_B):
    err = calc_error(elemB, ANS)
    print("| %s | %s | %5.4f |" % (elemA, elemB, err))

```