Skip to content

Commit 1a195a2

Browse files
authored
Merge pull request #204 from kostrykin/dev/curve_fitting
Update `ip_curve_fitting`: Relax rigidity of required input + Migrate to standard output format
2 parents 9ef338b + 9485ed4 commit 1a195a2

10 files changed

Lines changed: 182 additions & 38 deletions

File tree

tools/curve_fitting/creators.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../macros/creators.xml

tools/curve_fitting/curve_fitting.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99

1010
import argparse
11+
import os
1112

1213
import numpy as np
1314
import pandas as pd
@@ -51,21 +52,23 @@ def curve_fitting(seq, degree=2, penalty='abs'):
5152
return compute_curve(xx, result.x)
5253

5354

54-
def curve_fitting_io(fn_in, fn_out, degree=2, penalty='abs', alpha=0.01):
55-
# read all sheets
56-
xl = pd.ExcelFile(fn_in)
57-
nSpots = len(xl.sheet_names)
58-
data_all = []
55+
def curve_fitting_io(input_list, output, degree=2, penalty='abs', alpha=0.01):
56+
57+
# read all inputs
58+
nSpots = len(input_list)
59+
df_all, data_all = [], []
5960
for i in range(nSpots):
60-
df = pd.read_excel(xl, xl.sheet_names[i])
61+
df = pd.read_csv(input_list[i], delimiter='\t')
62+
df.columns = df.columns.str.strip() # remove whitespaces from header names
63+
df_all.append(df)
6164
data_all.append(np.array(df))
6265
col_names = df.columns.tolist()
6366
ncols_ori = len(col_names)
6467

6568
# curve_fitting
6669
diff = np.array([], dtype=('float64'))
6770
for i in range(nSpots):
68-
seq = data_all[i][:, -1]
71+
seq = data_all[i][:, col_names.index('intensity')]
6972
seq_fit = seq.copy()
7073
idx_valid = ~np.isnan(seq)
7174
seq_fit[idx_valid] = curve_fitting(seq[idx_valid], degree=degree, penalty=penalty)
@@ -82,25 +85,21 @@ def curve_fitting_io(fn_in, fn_out, degree=2, penalty='abs', alpha=0.01):
8285
seq_assist = data_all[i][:, -1] + sig3
8386
data_all[i] = np.concatenate((data_all[i], seq_assist.reshape(-1, 1)), axis=1)
8487

85-
# write to file
86-
with pd.ExcelWriter(fn_out) as writer:
87-
for i in range(nSpots):
88-
df = pd.DataFrame()
89-
for c in range(ncols_ori):
90-
df[col_names[c]] = data_all[i][:, c]
91-
df['CURVE'] = data_all[i][:, ncols_ori]
92-
if alpha > 0:
93-
df['CURVE_A'] = data_all[i][:, ncols_ori + 1]
94-
df.to_excel(writer, sheet_name=xl.sheet_names[i], index=False, float_format='%.2f')
95-
writer.save()
88+
# write to files
89+
for i in range(nSpots):
90+
df = df_all[i]
91+
df['curve'] = data_all[i][:, ncols_ori]
92+
if alpha > 0:
93+
df['curve_a'] = data_all[i][:, ncols_ori + 1]
94+
df.to_csv(os.path.join(output, f'curve{i + 1}.tsv'), sep='\t', lineterminator='\n', index=False)
9695

9796

9897
if __name__ == "__main__":
9998
parser = argparse.ArgumentParser(description="Fit (1st- or 2nd-degree) polynomial curves to data points")
100-
parser.add_argument("fn_in", help="File name of input data points (xlsx)")
101-
parser.add_argument("fn_out", help="File name of output fitted curves (xlsx)")
99+
parser.add_argument("--input", help="File name of input data points (tsv)", action='append', type=str, required=True)
100+
parser.add_argument("output", help="Name of output directory")
102101
parser.add_argument("degree", type=int, help="Degree of the polynomial function")
103102
parser.add_argument("penalty", help="Optimization objective for fitting")
104103
parser.add_argument("alpha", type=float, help="Significance level for generating assistive curves")
105104
args = parser.parse_args()
106-
curve_fitting_io(args.fn_in, args.fn_out, args.degree, args.penalty, args.alpha)
105+
curve_fitting_io(args.input, args.output, args.degree, args.penalty, args.alpha)

tools/curve_fitting/curve_fitting.xml

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
<tool id="ip_curve_fitting" name="Perform curve fitting" version="0.0.3-2" profile="20.05">
1+
<tool id="ip_curve_fitting" name="Perform curve fitting" version="0.0.4" profile="20.05">
22
<description></description>
3+
<macros>
4+
<import>creators.xml</import>
5+
</macros>
6+
<creator>
7+
<expand macro="creators/bmcv"/>
8+
</creator>
39
<edam_operations>
410
<edam_operation>operation_3443</edam_operation>
511
</edam_operations>
612
<xrefs>
713
<xref type="bio.tools">galaxy_image_analysis</xref>
814
</xrefs>
915
<requirements>
10-
<requirement type="package" version="1.20.2">numpy</requirement>
11-
<requirement type="package" version="3.0.7">openpyxl</requirement>
12-
<requirement type="package" version="1.2.4">pandas</requirement>
13-
<requirement type="package" version="1.6.2">scipy</requirement>
16+
<requirement type="package" version="2.3.5">numpy</requirement>
17+
<requirement type="package" version="2.2.2">pandas</requirement>
18+
<requirement type="package" version="1.16.3">scipy</requirement>
1419
</requirements>
1520
<command>
1621
<![CDATA[
17-
python '$__tool_directory__/curve_fitting.py'
18-
'$fn_in'
19-
./output.xlsx
20-
$degree
21-
$penalty
22-
$alpha
22+
23+
mkdir ./output &&
24+
python '$__tool_directory__/curve_fitting.py'
25+
26+
#for $input in $input_list
27+
#if $input
28+
--input '$input'
29+
#end if
30+
#end for
31+
32+
./output
33+
$degree
34+
$penalty
35+
$alpha
36+
2337
]]>
2438
</command>
2539
<inputs>
26-
<param name="fn_in" type="data" format="xlsx" label="File name of input data points (xlsx)" />
40+
<param name="input_list" type="data" format="tsv,tabular" multiple="true" label="Lists of intensity values"/>
2741
<param name="degree" type="select" label="Degree of the polynomial function">
2842
<option value="2" selected="True">2nd degree</option>
2943
<option value="1">1st degree</option>
@@ -36,21 +50,39 @@ python '$__tool_directory__/curve_fitting.py'
3650
<param name="alpha" type="float" value="0.01" label="Significance level for generating assistive curves" />
3751
</inputs>
3852
<outputs>
39-
<data format="xlsx" name="fn_out" from_work_dir="output.xlsx"/>
53+
<collection type="list" name="output" label="Curve fitting on ${on_string}">
54+
<discover_datasets directory="output" pattern="__name__" format="tsv"/>
55+
</collection>
4056
</outputs>
4157
<tests>
4258
<test>
43-
<param name="fn_in" value="spots_linked.xlsx"/>
59+
<param name="input_list" value="input1.tsv"/>
60+
<param name="degree" value="2"/>
61+
<param name="penalty" value="abs"/>
62+
<param name="alpha" value="0.01"/>
63+
<output_collection name="output" type="list" count="1">
64+
<element name="curve1.tsv" value="output1/curve1.tsv" ftype="tsv" compare="diff"/>
65+
</output_collection>
66+
</test>
67+
<test>
68+
<param name="input_list" value="input1.tsv,input2.tsv"/>
4469
<param name="degree" value="2"/>
4570
<param name="penalty" value="abs"/>
4671
<param name="alpha" value="0.01"/>
47-
<output name="fn_out" value="curves_fitted.xlsx" ftype="xlsx" compare="sim_size"/>
72+
<output_collection name="output" type="list" count="2">
73+
<element name="curve1.tsv" value="output2/curve1.tsv" ftype="tsv" compare="diff"/>
74+
<element name="curve2.tsv" value="output2/curve2.tsv" ftype="tsv" compare="diff"/>
75+
</output_collection>
4876
</test>
4977
</tests>
5078
<help>
51-
**What it does**
5279

53-
This tool fits (1st- or 2nd-degree) polynomial curves to data points. Optional: Given a significance level, assistive curves will also be generated.
80+
**Perform curve fitting for lists of intensity values.**
81+
82+
This tool fits 1st- or 2nd-order polynomial curves to 1-D data points (tabular list of values).
83+
84+
Optionally, given a significance level, assistive curves are generated.
85+
5486
</help>
5587
<citations>
5688
<citation type="doi">10.1097/j.pain.0000000000002642</citation>
-194 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
frame pos_x pos_y intensity
2+
1 199 265 48533.56
3+
2 199 265 55939.36
4+
3 199 265 59098.11
5+
4 199 265 54234.74
6+
5 198 265 52396.98
7+
6 199 265 55937.6
8+
7 199 265 54341.73
9+
8 199 265 51470.92
10+
9 199 265 52983.33
11+
10 199 265 53354.71
12+
11 199 265 53435.04
13+
12 199 266 54853.97
14+
13 199 266 51723.82
15+
14 199 268 55421.12
16+
15 199 268 52665.4
17+
16 199 267 52337.47
18+
17 199 268 55327.1
19+
18 199 269 48798.69
20+
19 199 267 52500.61
21+
20 199 267 53049.55
22+
21 199 267 53916.84
23+
22 199 266 50521.86
24+
23 199 267 48872.79
25+
24 199 267 49995.33
26+
25 199 267 51935.29
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
frame pos_x pos_y intensity
2+
3 199 268 53383.42
3+
4 199 269 56658.14
4+
5 199 269 54199.83
5+
6 199 269 54090.9
6+
7 200 268 54382.22
7+
8 200 269 54957.7
8+
9 200 270 51870.16
9+
10 199 269 46656.38
10+
11 200 269 50855.08
11+
12 202 273 39033.58
12+
13 204 273 32915.16
13+
14 203 272 30327.51
14+
15 204 272 34846.82
15+
16 204 272 36178.4
16+
17 204 273 31739.84
17+
18 204 273 31831.22
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
frame pos_x pos_y intensity curve curve_a
2+
1 199 265 48533.56 54364.28694031549 60195.013880630984
3+
2 199 265 55939.36 54335.31702432827 60166.043964643766
4+
3 199 265 59098.11 54292.13495728906 60122.861897604555
5+
4 199 265 54234.74 54234.74073919788 60065.46767951337
6+
5 198 265 52396.98 54163.13437005471 59993.8613103702
7+
6 199 265 55937.6 54077.31584985955 59908.042790175045
8+
7 199 265 54341.73 53977.285178612416 59808.01211892791
9+
8 199 265 51470.92 53863.04235631329 59693.76929662879
10+
9 199 265 52983.33 53734.587382962185 59565.31432327768
11+
10 199 265 53354.71 53591.9202585591 59422.64719887459
12+
11 199 265 53435.04 53435.04098310402 59265.76792341952
13+
12 199 266 54853.97 53263.94955659697 59094.67649691246
14+
13 199 266 51723.82 53078.64597903793 58909.372919353424
15+
14 199 268 55421.12 52879.130250426904 58709.8571907424
16+
15 199 268 52665.4 52665.40237076389 58496.129311079385
17+
16 199 267 52337.47 52437.4623400489 58268.18928036439
18+
17 199 268 55327.1 52195.31015828192 58026.037098597415
19+
18 199 269 48798.69 51938.945825462964 57769.67276577846
20+
19 199 267 52500.61 51668.36934159202 57499.096281907514
21+
20 199 267 53049.55 51383.58070666909 57214.307646984584
22+
21 199 267 53916.84 51084.579920694174 56915.30686100967
23+
22 199 266 50521.86 50771.36698366728 56602.09392398277
24+
23 199 267 48872.79 50443.9418955884 56274.668835903896
25+
24 199 267 49995.33 50102.30465645754 55933.031596773035
26+
25 199 267 51935.29 49746.455266274694 55577.18220659019
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
frame pos_x pos_y intensity curve curve_a
2+
1 199 265 48533.56 54364.28694031549 63253.283100868255
3+
2 199 265 55939.36 54335.31702432827 63224.313184881044
4+
3 199 265 59098.11 54292.13495728906 63181.131117841825
5+
4 199 265 54234.74 54234.74073919788 63123.73689975064
6+
5 198 265 52396.98 54163.13437005471 63052.13053060748
7+
6 199 265 55937.6 54077.31584985955 62966.31201041232
8+
7 199 265 54341.73 53977.285178612416 62866.28133916519
9+
8 199 265 51470.92 53863.04235631329 62752.03851686606
10+
9 199 265 52983.33 53734.587382962185 62623.58354351495
11+
10 199 265 53354.71 53591.9202585591 62480.91641911186
12+
11 199 265 53435.04 53435.04098310402 62324.037143656795
13+
12 199 266 54853.97 53263.94955659697 62152.945717149734
14+
13 199 266 51723.82 53078.64597903793 61967.642139590695
15+
14 199 268 55421.12 52879.130250426904 61768.126410979676
16+
15 199 268 52665.4 52665.40237076389 61554.39853131666
17+
16 199 267 52337.47 52437.4623400489 61326.45850060167
18+
17 199 268 55327.1 52195.31015828192 61084.306318834686
19+
18 199 269 48798.69 51938.945825462964 60827.941986015736
20+
19 199 267 52500.61 51668.36934159202 60557.36550214479
21+
20 199 267 53049.55 51383.58070666909 60272.576867221855
22+
21 199 267 53916.84 51084.579920694174 59973.57608124694
23+
22 199 266 50521.86 50771.36698366728 59660.36314422004
24+
23 199 267 48872.79 50443.9418955884 59332.93805614117
25+
24 199 267 49995.33 50102.30465645754 58991.30081701031
26+
25 199 267 51935.29 49746.455266274694 58635.451426827465
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
frame pos_x pos_y intensity curve curve_a
2+
3 199 268 53383.42 57769.32742699415 66658.32358754691
3+
4 199 269 56658.14 56658.1393275885 65547.13548814127
4+
5 199 269 54199.83 55431.86482503778 64320.86098559055
5+
6 199 269 54090.9 54090.50391934199 62979.50007989476
6+
7 200 268 54382.22 52634.05661050111 61523.05277105387
7+
8 200 269 54957.7 51062.52289851515 59951.51905906791
8+
9 200 270 51870.16 49375.90278338412 58264.89894393689
9+
10 199 269 46656.38 47574.196265108 56463.192425660774
10+
11 200 269 50855.08 45657.403343686805 54546.39950423957
11+
12 202 273 39033.58 43625.524019120545 52514.520179673316
12+
13 204 273 32915.16 41478.558291409194 50367.55445196196
13+
14 203 272 30327.51 39216.506160552766 48105.50232110554
14+
15 204 272 34846.82 36839.36762655126 45728.36378710403
15+
16 204 272 36178.4 34347.142689404674 43236.13884995744
16+
17 204 273 31739.84 31739.831349113017 40628.82750966579
17+
18 204 273 31831.22 29017.433605676277 37906.42976622905
-149 KB
Binary file not shown.

0 commit comments

Comments
 (0)