teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

訂正

2021/12/15 10:52

投稿

Nari9113
Nari9113

スコア3

title CHANGED
File without changes
body CHANGED
@@ -27,140 +27,10 @@
27
27
  x, y = np.loadtxt(datafile, skiprows=29, unpack=True)
28
28
 
29
29
 
30
- def detect_peak():
31
- global x
32
- global y
33
- #signal.find_peaks(x, height=高さ, distance=距離)
34
30
 
35
- # 極大値 orderを変えることでピークの検出が変わる(ピーク検出の閾値)
36
- maxid = signal.argrelmax(y, order=100)
37
- # minid = signal.argrelmin(y, order=1) #極小値
38
-
39
-
40
-
41
- # plt.plot(x[minid],y[minid],'bo',label='ピーク値(最小)')
42
-
43
- return x[maxid], y[maxid]
44
-
45
- def func(x, *params):
46
-
47
- #paramsの長さでフィッティングする関数の数を判別。
48
- num_func = int(len(params)/3)
49
-
50
- #ガウス関数にそれぞれのパラメータを挿入してy_listに追加。
51
- y_list = []
52
- for i in range(num_func):
53
- y = np.zeros_like(x)
54
- param_range = list(range(3*i,3*(i+1),1))
55
- amp = params[int(param_range[0])]
56
- ctr = params[int(param_range[1])]
57
- wid = params[int(param_range[2])]
58
- y = y + amp * np.exp( -((x - ctr)/wid)**2)
59
- y_list.append(y)
60
-
61
- #y_listに入っているすべてのガウス関数を重ね合わせる。
62
- y_sum = np.zeros_like(x)
63
- for i in y_list:
64
- y_sum = y_sum + i
65
-
66
- #最後にバックグラウンドを追加。
67
- y_sum = y_sum + params[-1]
68
-
69
- return y_sum
70
-
71
- #プロットの定義
72
- def fit_plot(x, *params):
73
- num_func = int(len(params)/3)
74
- y_list = []
75
- for i in range(num_func):
76
- y = np.zeros_like(x)
77
- param_range = list(range(3*i,3*(i+1),1))
78
- amp = params[int(param_range[0])]
79
- ctr = params[int(param_range[1])]
80
- wid = params[int(param_range[2])]
81
- y = y + amp * np.exp( -((x - ctr)/wid)**2) + params[-1]
82
- y_list.append(y)
83
- return y_list
84
-
85
- #初期値のリストを作成
86
- #[amp,ctr,wid]
87
-
88
- # x1, y1 = detect_peak() ##
89
- np_x, np_y = detect_peak() ##
90
-
91
- background = 62
92
- # guess = [] ##
93
- guess = [[y1, x1, 0.5] for (x1, y1) in zip(np_x, np_y) if background < y1]
94
- #バックグラウンドの初期値
95
-
96
-
97
- #初期値リストの結合
98
- guess_total = []
99
- for i in guess:
100
- guess_total.extend(i)
101
- guess_total.append(background)
102
-
103
- popt, pcov = curve_fit(func, x, y, p0=guess_total,maxfev=100000)
104
-
105
-
106
-
107
- fit= func(x, *popt)
108
- plt.scatter(x, y, s=20)
109
- plt.plot(x, fit , ls='-', c='black', lw=1)
110
-
111
- y_list = fit_plot(x, *popt)
112
- baseline = np.zeros_like(x) + popt[-1]
113
- for n,i in enumerate(y_list):
114
- plt.fill_between(x, i, baseline, facecolor=cm.rainbow(n/len(y_list)), alpha=0.6)
115
-
116
- sub_list=list((popt))
117
-
118
- l_list=sub_list[1:-1:3]
119
- r=np.array(l_list)
120
- rad=np.radians(r)
121
- sin=np.sin(rad/2)
122
-
123
- Cuk_Alpha=1.5418
124
- last_list=Cuk_Alpha/(2*sin)
125
-
126
-
127
- P_list=sub_list[0:-1]
128
-
129
- n1=3
130
- result1 = [P_list[idx:idx + n1] for idx in range(0,len(P_list),n1)]
131
- pd.set_option('display.unicode.east_asian_width',True)
132
- df1=pd.DataFrame(result1,columns=['ピーク強度','位置','幅'])
133
-
134
-
135
- n2=1
136
- result2 = [last_list[idx:idx + n2] for idx in range(0,len(last_list),n2)]
137
- pd.set_option('display.unicode.east_asian_width',True)
138
- df2=pd.DataFrame(result2,columns=['面間隔'])
139
-
140
- df_h_in = pd.concat([df1, df2], axis=1, join='inner')
141
- print(df_h_in)
142
-
143
- print('バックグラウンドの強度',sub_list[-1])
144
-
145
- csv_datafile=input("保存データ名")
146
- df_h_in.to_csv(csv_datafile, sep=",",index=False)
147
31
  ```
148
32
 
149
33
  ### 試したこと
150
34
  https://ja.stackoverflow.com/questions/45204
151
35
  https://masaeng.hatenablog.com/entry/2020/06/28/233020
152
- などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした
36
+ などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした
153
-
154
- ### 変更追記
155
- ```Python
156
- datafile=input("入力データ")
157
- process(datafile)
158
- def process():
159
- def detect_peak():
160
- x, y = np.loadtxt(datafile, skiprows=29, unpack=True)
161
-
162
- #signal.find_peaks(x, height=高さ, distance=距離)
163
-
164
- # 極大値 orderを変えることでピークの検出が変わる(ピーク検出の閾値)
165
- maxid = signal.argrelmax(y, order=100)
166
- ```

3

訂正

2021/12/15 10:52

投稿

Nari9113
Nari9113

スコア3

title CHANGED
File without changes
body CHANGED
@@ -154,10 +154,10 @@
154
154
  ### 変更追記
155
155
  ```Python
156
156
  datafile=input("入力データ")
157
- process(data_file)
157
+ process(datafile)
158
158
  def process():
159
159
  def detect_peak():
160
- x, y = np.loadtxt(data_file, skiprows=29, unpack=True)
160
+ x, y = np.loadtxt(datafile, skiprows=29, unpack=True)
161
161
 
162
162
  #signal.find_peaks(x, height=高さ, distance=距離)
163
163
 

2

追記

2021/12/15 06:36

投稿

Nari9113
Nari9113

スコア3

title CHANGED
File without changes
body CHANGED
@@ -149,4 +149,18 @@
149
149
  ### 試したこと
150
150
  https://ja.stackoverflow.com/questions/45204
151
151
  https://masaeng.hatenablog.com/entry/2020/06/28/233020
152
- などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした
152
+ などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした
153
+
154
+ ### 変更追記
155
+ ```Python
156
+ datafile=input("入力データ")
157
+ process(data_file)
158
+ def process():
159
+ def detect_peak():
160
+ x, y = np.loadtxt(data_file, skiprows=29, unpack=True)
161
+
162
+ #signal.find_peaks(x, height=高さ, distance=距離)
163
+
164
+ # 極大値 orderを変えることでピークの検出が変わる(ピーク検出の閾値)
165
+ maxid = signal.argrelmax(y, order=100)
166
+ ```

1

文法の修正

2021/12/15 06:35

投稿

Nari9113
Nari9113

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,11 @@
1
1
  ### 前提・実現したいこと
2
- Python使い始めて約一か月の初心者なので力を貸してください
2
+ Python使い始めて約一か月の初心者なので力を貸してください
3
+
3
4
  txtファイル2th-scan_BLT1.txt~2th-scan_BLTn.txt(nは数)を下記コードで1つずつ処理をしたいです。
4
5
  下記コードは「input(入力データ)」の部分に手入力で『2th-scan_BLT1.txt』のように入力すれば処理されます。
5
6
  複数のファイルを与えられたとき一つ一つ入力が大変なので自動処理できるよう変更すべき点を教えて頂きたいです。
6
- 色々調べたのですがtxtだったりskiprowsだったりx,yで必要な値を抜き出しているようなものがなかったので
7
+ 色々調べたのですがtxtだったりskiprowsだったりx,yで必要な値を抜き出しているようなものがなかったので、、
8
+
7
9
  コードを記載していただけると助かります、、
8
10
  必要な情報があればコメント頂ければ記載します!
9
11
 
@@ -147,4 +149,4 @@
147
149
  ### 試したこと
148
150
  https://ja.stackoverflow.com/questions/45204
149
151
  https://masaeng.hatenablog.com/entry/2020/06/28/233020
150
- などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした(´;ω;`)
152
+ などのサイトも確認しましたがx,yの壁が立ちはだかりチンプンカンプンでした