summaryrefslogtreecommitdiff
path: root/python-nanoforce.spec
blob: b6bc6fa531134a06f4d046c8e261234bb0099eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
%global _empty_manifest_terminate_build 0
Name:		python-nanoforce
Version:	0.3.32
Release:	1
Summary:	Package to import and analyse AFM force curves produced using Nanoscope 6
License:	MIT License
URL:		https://github.com/crj341/nanoforce
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/14/ab/2097657128a98f1ba01ce4aa911dd95b84f1e646ae88e86f3b80444e9864/nanoforce-0.3.32.tar.gz
BuildArch:	noarch

Requires:	python3-numpy
Requires:	python3-plotly
Requires:	python3-scipy
Requires:	python3-easygui
Requires:	python3-NSFopen
Requires:	python3-sklearn

%description
# Nanoforce

Package to import and analyse AFM force curves produced using Nanoscope 5 & 6 and Nanosurf .nid files

# Python AFM Analysis Tutorial
This tutorial demonstrates the basics of the AFM python tools for data analysis. Currently the scripts are available as an offline file or can be installed from PyPI using:
```
pip install nanoforce
```
Following the steps below will show you how to carry out simple analysis and plotting of AFM data produced with Nanoscope and explain how to carryout your own more specific analysis. For each snippet of code - try running it to see what it outputs. You can download the tutorial as a Jupyter notebook from:
https://github.com/crj341/nanoforce/blob/master/AFM_tutorial.ipynb

## Prerequisites
To use the tools you will need a few python packages (numpy, plotly, scipy and easygui). To install these type the following commands into a terminal window (unless you have installed the nanoforce package from PyPI (i.e. using pip), in which case they should have installed automatically):
```
pip install numpy
pip install plotly
pip install scipy
pip install easygui
```
## Getting started
Start off by creating a folder containing your script (and the afm.py file if you have not installed the package from PyPI). For beginers it may be best to use a jupyter notebook here, as has been used for this tutorial. Open your script file and type (note this is case sensetive):
```
from  nanoforce import  AFM
```
To carry out basic analysis you can use the inbuilt 'run' command.
(Check the window hasn't opened behind python if this is running slowly.)
```
expt = AFM()
expt.run()
```
This will open a window, prompting you to select the AFM data you wish to analyse. Navigate to the folder and select the file with the '.000' extension. The tools will automatically align the data with thebaseline and set the surface height to zero. A plot will open with the processed force curves, use the slider to move through each experiment. The code will also aclculate the adhesion and modules. The mean and standard deviation will appera in the output.

It is likley you will wish to carry out more detailled analysis than provided by this function. The following sections will guide you through all the individual functions available.

## Naming variables
If you have carried out multiple experiments and would like to compare the results, you should assign an instance of the AFM class for each. Name a variable for each experiment and call the class:
```
expt_1 = AFM()
expt_2 = AFM()
expt_3 = AFM()
```
## Importing data - Nanoscope files (.000)
To import a nanoscope dataset use the 'input_files' command for each experiment:

Note - the GUI for selecting a file will only run on a local version of python (not in the Colab tutorial). This will prompt an error in Colab - use manual file_name entry below.
```
expt_1.input_files()
expt_2.input_files()
expt_3.input_files()
```
This will open a window prompting you to select the '.000' file for each experiment. The code will then automatcally import all files with the same name.

If you would prefer to manually set the file name (useful if you are running the script multiple time), you can name the 'file_name' variable and set the 'gui_on' flag in 'import_files' to 'False':
```
expt_1.file_name = '/insert/your/file/path/here.000'
expt_1.input_files(gui_on = False)
```
## Importing data - Nanoscope files (.000)
To import a Nanosurf .nid dataset first set the file name:
```
expt_1.file_name = r'C:\file\path\goes\here.nid
```
Then call the following function to import all force curve data from the file@
```
epxt_1.nanosurf_import
```
## Naming experiments
It may be useful to name each experiment, for plotting etc. To do so, use the following command:
```
expt_1.set_run_name('Sample 1')
```
## Finding parameters - Nanoscope files only

The next step is to import the relevant parameters from the file (such as deflection sensetivity and spring constant). To do so, due the 'nanoscope_params' function as follows:
```
expt_1.nanoscope_params()
expt_2.nanoscope_params()
expt_3.nanoscope_params()
```
By default this will search for parameters in the Nanoscope 6 format. To use a Nanoscope 5 file, change the 'nanoscope_version' input as follows (if a value other than 5 or 6 is given, or the script detects a file in the wrong format, python will exit):
```
expt_1.nanoscope_params(nanoscope_version=5)
```
If you would like to manually set the deflaction sensetivity, use the following function:
```
expt_1.set_def_sens(100)
```
If you would like to manually set the spring constant, use the following function:
```
expt_2.set_spr_const(0.32)
```
## Reading force curve data - Nanoscope files only
Now we have the parameters, we need the actual data. To import this, use the 'nanoscope_read' command:
```
expt_1.nanoscope_read()
expt_2.nanoscope_read()
expt_3.nanoscope_read()
```
## Plot raw data

To view the raw force curve data before any processing use the command below. The number in the brackets specifies which curve to plot. Remember, when using python, the numbers start at 0 (i.e. for 100 force curves the numbers are 0 to 99).
```
expt_1.plot_raw(1)
```
## Baseline
To adjust the baseline for each curve use the 'baseline' function. This works by taking the mean value over a specified area and setting this to zero. There are default values included to get you started:
```
expt_1.baseline()
expt_2.baseline()
expt_3.baseline()
```
If you need to change the region used for baselining (for example, if there is noise on part of the approach) use the 'start_pos' and 'end_pos' inputs. Note these should be a fraction of the distance from the contact section of the curve towards the baseline. The default values are 'start_pos = 0.45' and 'end_pos = 0.8'. Change them using as follows:
```
expt_1.baseline(start_pos = 0.5, end_pos = 0.7)
```
You can also choose to exculed curves with a noisy baseline. The code calculates teh standard deviation over the specified region. If this is above a specified value the curve will be deleted. You can do this spearatley for the approach and retract curves by setting 'max_approach_noise' and 'max_retract_noise'. The default value for both is 1000  (i.e. all curves retained) and this can be changed as follows:
```
expt_1.baseline(max_approach_noise = 2, max_retract_noise = 4)
```
To change the baseline region and maximum noise together:
```
expt_1.baseline(
    start_pos = 0.5,
    end_pos = 0.7,
    max_approach_noise = 2,
    max_retract_noise = 4
)
```
## Contact point
To set surface height to zero, use the 'contact' function. Note this currently interpolates between the first point below zero and the previous point, so may have trouble for noisy data:
```
expt_1.contact()
expt_2.contact()
expt_3.contact()
```
By default, the length position used will be the piezo position, rather than the actual tip position. If a cantilever is likley to experience large deflection when in contact with the surface, the tip position can be calculated using the measured force and deflection sensitivity by calling the 'adjust_tip_position' function.
```
expt_1.adjust_tip_position()
```
## Plot adjusted curves

To view individual curves once they have been aligned, use the 'plot_adjusted' command, typing the number of the curve to plot in the brackets:
```
expt_1.plot_adjusted(10)
```
You can also plot all of the curves and use a slider to cycle through them, using 'plot_curves':
```
expt_3.plot_curves()
```
## Delete bad curves

If a curve is noisy and needs to be removed, use the 'delete_curve' function. Put the numbers for all the curves to delete in the brackets:
```
expt_1.delete_curve(8,59,87)
expt_2.delete_curve(17)
```
## Calcluate adhesion
To calculate the adhesion for each curve, use 'calc_adhesion'. The mean and standard deviation for the adhesion calculated for each experiment will be printed in the output.
```
expt_1.calc_adhesion()
expt_2.calc_adhesion()
expt_3.calc_adhesion()
```
It is possible to plot a histogram of the calculated adhesion values by setting 'plot_hist' to 'True':
```
expt_2.calc_adhesion(plot_hist = True)
```
## Calculate modulus

To caclculate the elastic modulus, use 'calc_modulus':
```
expt_1.calc_modulus()
expt_2.calc_modulus()
expt_3.calc_modulus()
```
The method used currently follows the Hertz model for a spherical indenter:

![](https://camo.githubusercontent.com/e4f11366d77786e0d6a533f4ee99ff57343515ba/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46253230253344253230253543667261632537423425374425374233253744253230452535452a25323052253545253742302e3525374425323064253545253742312e35253744)

![](https://camo.githubusercontent.com/14cef2c5e957138704ec2f3fe2d0b75c4c1dc1b7/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436672616325374231253744253742452535452a25374425323025334425323025354366726163253742312d2535436e75253545325f31253744253742455f312537442532302b25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

Where:

![](https://camo.githubusercontent.com/8bc1794caf55a8564fe3d7d97864264621a42d42/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46) is Applied Force (nN)

![](https://camo.githubusercontent.com/ff544c24651cf98efd2ff0cc886a660c98396c2d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f452535452a) is Reduced Modulus (MPa)

![](https://camo.githubusercontent.com/c7cdc1b9a6698dd223b77822a075aa1246ce3ee1/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f52) is Sphere Radius (nm)

![](https://camo.githubusercontent.com/a9441c067b8389cfff3cccce033e3251a198017c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f64) is Total Deformation (i.e. indentation depth) (nm)

![](https://camo.githubusercontent.com/f518919e4b9dd3fffc951cd5f7a0f4d59a47dd89/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f31) is Indenter elastic modulus (MPa)

![](https://camo.githubusercontent.com/a0497118c2b189cb64abec7a2cac19dd405de497/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f32) is Sample elastic modulus (MPa)

![](https://camo.githubusercontent.com/c69ec83ba78d57af69c54499f1b81ab1fce8a871/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f31) is Indenter Poisson Ratio

![](https://camo.githubusercontent.com/452114f60b359b5dd1a5e252aee4a9d39f1bf10c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f32) is Sample Poisson Ratio

The method assumes a hard indenter is used, i.e. ![](https://camo.githubusercontent.com/e3b520ba355ebf2f1b19e5a5f67c721ce07f917d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f25354366726163253742312d2535436e75253545325f31253744253742455f312537442532302535436c6c25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

If a soft indenter, such as a colloidal probe, is used the 'soft_indenter' input should be set to 'True' and the Poisson Ratio and Elastic Modulus (MPa) of the indenter set using the         'indenter_poisson_ratio' and 'indenter_modulus' inputs respectivley.
```
expt_1.calc_modulus(poisson_ratio = 0.5, indenter_radius = 10 * 1e-9, soft_indenter = True, indenter_poisson_ratio = 0.5, indenter_modulus = 300)
```

The default method uses as surface poisson ratio of 0.5 and indenter radius of 10 nm (input required in m, i.e. 10E-09). To set these manually use the 'poisson_ratio' and 'indenter_radius' inputs:
```
expt_1.calc_modulus(poisson_ratio = 0.4, indenter_radius = 20e-9)
```
As with adhesion, it is possible to plot a histogram of the results:
```
expt_2.calc_modulus(plot_hist = True)
```
It is also possible to use the Sneddon model:
```
expt_1.calc_modulus_sneddon(poisson_ratio = 0.5, opening_angle = 35)
```
Or the four sided pyramid model:
```
expt_1.calc_modulus_pyramid(poisson_ratio = 0.5, face_angle = 35)
```

## Plot adhesion vs modulus

To view a scatter plot of adhesion vs modulus use the following command:
```
expt_1.plot_adhesion_modulus()
```
## Saving and reloading results
To save the calculated results, including force curves and parameters, in a format which can be re imported by the 'nanoforce' package later on use 'save_data'. This will open a window prompting you to select a folder and file name for saving. Leave the file extension blank.
```
expt_1.save_data()
```
You can also specify the filename and folder in the brackets:
```
expt_1.save_data('filename')
expt_2.save_data(r'\path\to\file')
```
To load saved data, create a new variable with the AFM class and call the 'load_data' function:
```
from  afm   import  AFM
loaded_data = AFM()
loaded_data.load_data()
```
It is also possible to save individual variables for use in other softwares. This can be done using the numpy 'savetxt' function, as follows for adhesion (a full list of available variables can be found in the 'Custom plotting and analysis' section below):
```
import numpy as np
np.savetxt('file_name_here.csv', expt_1.adheision)
```
## Built in comparison plots

There are a number of functions built in for plotting comparisons of the adhesion and modulus data for separate experiments. These have been left as basic functions. For more specific plots, folow the custom plotting section below.
To use the comparison plots, call the functions listed below directly from the AFM class with input variables for each experiment as an input.
#### Histogram comparison of adhesion
```
AFM.overlay_adhesion_hist(expt_1,expt_2,expt_3)
```
#### Histogram comparison of modulus
```
AFM.overlay_modulus_hist(expt_1,expt_2,expt_3)
```
#### Box plot comparison of adhesion
```
AFM.overlay_adhesion_box(expt_1,expt_2,expt_3)
```
#### Box plot comparison of modulus
```
AFM.overlay_modulus_box(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of adhesion
```
AFM.overlay_adhesion_bar(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of modulus
```
AFM.overlay_modulus_bar(expt_1,expt_2,expt_3)
```
## Force mapping
Force mapping can be carried out by aquiring a fine grid of force curves on a sample surface. The scan area and grid size will depend on the  uniformity of the surface and size of features, balanced with the length of time required for the scan. The force curves can be analyses as described above ant the force mapping function used to plot the reuslts. By default this function assumes a square grid of force curves across a 10 micron scan area and plots an adhesion map for this data.
```
expt_1.force_map()
```
If the grid is not square, the x_cells and y_cells inputs can be used to specify the grid size in the x and y directions. The auto_cell input must be set to False to manualy set the grid size.

```
expt_1.force_map(x_cells = 10, y_cells = 10, auto_cell = False)
```
The scan size can be specified using the x_scale and y_scale inputs.
```
expt_1.force_map(x_scale = 10, y_scale = 10)
```
The data to plot can be set using the plot input. Available plots are adhesion, modulus, topography (based on tip contact height), indent (indentation depth), max_force and snap_in.
```
expt_1.force_map(plot = 'modulus')
```
Finally, if using .nid files a 3d topographical representation of the surface, coloured with the chosen variable can be plotted by setting topography_3d to True.
```
expt_1.force_map(topography_3d = True)
```

## Editing and saving figures
All built in figures are produced using Plotly. To make changes to the formatting of these figures, the figure must be returned by the plotting function. This is achevied by setting the return_fig input to True.
```
fig_1 = force_map(return_fig = True)
```
This can then be edited using as described in the plotly documentation for each plot type. The returned figure can also be saved, again following the steps in the plotly documentation.

## Custom plotting and analysis
# 
The calculated variables are stored within each 'experiment' variable. These can be called when writing custom plotting or analysis scripts specific to your application. The variables available for each experiment are as follows:
```
expt_1.approach        # All approach curve data (nN)
expt_1.retract         # All retract curve data (nN)
expt_1.z_position      # All tip position data (nm)

expt_1.approach[:,i]   # Adhesion curve for experiment i
expt_1.retract[:,i]    # Adhesion curve for experiment i
expt_1.z_position[:,i] # Adhesion curve for experiment i

expt_1.def_sens        # Deflection sensetivity (nm/V)
expt_1.spr_const       # Spring constant (N/m)

expt_1.adheison        # Adhesion data (nN)
expt_1.modulus         # Modulus data (MPa)

expt_1.run_name        # Experiment name
```
Below is an example custom plotting script to overlay retract curves for multips experiments. This example uses plotly. There are a number of plotting tools in this package, and more detail (including example scripts) is available from https://plotly.com/python/
```
import  plotly.graph_objects  as      go
from    afm                   import  AFM

expt_1 = AFM()
expt_2 = AFM()

expt_1.run()
expt_2.run()

fig = go.Figure()

curve_number = 12

fig.add_trace(go.Scatter(y = expt_1.retract[:,curve_number],
                         x = expt_1.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 1'))

fig.add_trace(go.Scatter(y = expt_2.retract[:,curve_number],
                         x = expt_2.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 2'))

fig.update_layout(
    yaxis_title_text='Force (nN)',
    xaxis_title_text='Tip separation (nm)')

fig.show()
```
## Example analysis script
An example analysis script is available from Git Hub:
https://github.com/crj341/nanoforce/blob/master/afm_analysis.py

A script is also available here to apply a clustering algorithm to identify groups of corresponding adhesion and modulus values, while removing noise:
https://github.com/crj341/nanoforce/blob/master/afm_clustering.py

## Release history:
0.0.1 to 0.0.2 - Development

0.0.3 - Initial release

0.0.4 - Added dependenices to setup (numpy, scipy, easygui, plotly)

0.0.5 to 0.0.7 - Updated README and added class/function descriptions

0.0.8 - Fixed issue extracting spring constants with no decimal point

0.0.9 - Fixed packaging error

0.0.10 - Added support for Nanoscope version 5

0.0.11 to 0.1.0 - Bug fixes

0.1.1 - Full release

0.1.2 to 0.1.5 - Updated 'delete_curves' numbering system, added warning for bad curves in 'contact'

0.1.6 to 0.1.7 - Added support for files not starting at 000

0.1.8 - Added mac support

0.1.9 - Added violin plots for adhesion and modulus

0.1.10 - Fixed units issue for modulus calculaiton

0.1.11 - Added option to calculate actual tip position in 'nanoscope read'. Added option to include indenter Poisson Ratio and Moudlus in calc_modulus.

0.1.12 to - 0.1.24 - Fixed bugs

0.2.0 - New features now stable. Updated README.

0.2.1 to 0.2.19 - Initial support for Nanosurf AFM .nid files.

0.2.20 to 0.2.21 - Added Sneddon model and Four sided pyramid for modulus calculation.

0.2.22 - Fixed bug determining points per force curve.

0.2.23 - Fixed bug where adhesion was calculated as nan.

0.2.24 - Updated Readme for Nanosurf .nid files.

0.3.0 - New features now stable. Updated README.

0.3.1 - Added adhesion map function.

0.3.2 - Added error message to baseline function.

0.3.3 - Snap in force, max indent force, contact height and indent depth calculated.

0.3.4 to 0.3.6 - Additional force map plotting options.

0.3.7 - All figures now returnable. Updated README.

0.3.8 - Bug fixed which prevented force_map being called more than once per dataset.

0.3.9 - Bug fixed where indentation depth calculated incorrectly when using 'adjust_tip_position'.

0.3.10 to 0.3.15 - Updated Sneddon model modulus function to use line fit of indentation.

0.3.16 to 0.3.21 - Update to Hertz model.

0.3.22 to 0.3.28 - Added function to determine snap in distance.

0.3.29 t0 0.3.32 - Added function to calculate work of adhesion.



%package -n python3-nanoforce
Summary:	Package to import and analyse AFM force curves produced using Nanoscope 6
Provides:	python-nanoforce
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-nanoforce
# Nanoforce

Package to import and analyse AFM force curves produced using Nanoscope 5 & 6 and Nanosurf .nid files

# Python AFM Analysis Tutorial
This tutorial demonstrates the basics of the AFM python tools for data analysis. Currently the scripts are available as an offline file or can be installed from PyPI using:
```
pip install nanoforce
```
Following the steps below will show you how to carry out simple analysis and plotting of AFM data produced with Nanoscope and explain how to carryout your own more specific analysis. For each snippet of code - try running it to see what it outputs. You can download the tutorial as a Jupyter notebook from:
https://github.com/crj341/nanoforce/blob/master/AFM_tutorial.ipynb

## Prerequisites
To use the tools you will need a few python packages (numpy, plotly, scipy and easygui). To install these type the following commands into a terminal window (unless you have installed the nanoforce package from PyPI (i.e. using pip), in which case they should have installed automatically):
```
pip install numpy
pip install plotly
pip install scipy
pip install easygui
```
## Getting started
Start off by creating a folder containing your script (and the afm.py file if you have not installed the package from PyPI). For beginers it may be best to use a jupyter notebook here, as has been used for this tutorial. Open your script file and type (note this is case sensetive):
```
from  nanoforce import  AFM
```
To carry out basic analysis you can use the inbuilt 'run' command.
(Check the window hasn't opened behind python if this is running slowly.)
```
expt = AFM()
expt.run()
```
This will open a window, prompting you to select the AFM data you wish to analyse. Navigate to the folder and select the file with the '.000' extension. The tools will automatically align the data with thebaseline and set the surface height to zero. A plot will open with the processed force curves, use the slider to move through each experiment. The code will also aclculate the adhesion and modules. The mean and standard deviation will appera in the output.

It is likley you will wish to carry out more detailled analysis than provided by this function. The following sections will guide you through all the individual functions available.

## Naming variables
If you have carried out multiple experiments and would like to compare the results, you should assign an instance of the AFM class for each. Name a variable for each experiment and call the class:
```
expt_1 = AFM()
expt_2 = AFM()
expt_3 = AFM()
```
## Importing data - Nanoscope files (.000)
To import a nanoscope dataset use the 'input_files' command for each experiment:

Note - the GUI for selecting a file will only run on a local version of python (not in the Colab tutorial). This will prompt an error in Colab - use manual file_name entry below.
```
expt_1.input_files()
expt_2.input_files()
expt_3.input_files()
```
This will open a window prompting you to select the '.000' file for each experiment. The code will then automatcally import all files with the same name.

If you would prefer to manually set the file name (useful if you are running the script multiple time), you can name the 'file_name' variable and set the 'gui_on' flag in 'import_files' to 'False':
```
expt_1.file_name = '/insert/your/file/path/here.000'
expt_1.input_files(gui_on = False)
```
## Importing data - Nanoscope files (.000)
To import a Nanosurf .nid dataset first set the file name:
```
expt_1.file_name = r'C:\file\path\goes\here.nid
```
Then call the following function to import all force curve data from the file@
```
epxt_1.nanosurf_import
```
## Naming experiments
It may be useful to name each experiment, for plotting etc. To do so, use the following command:
```
expt_1.set_run_name('Sample 1')
```
## Finding parameters - Nanoscope files only

The next step is to import the relevant parameters from the file (such as deflection sensetivity and spring constant). To do so, due the 'nanoscope_params' function as follows:
```
expt_1.nanoscope_params()
expt_2.nanoscope_params()
expt_3.nanoscope_params()
```
By default this will search for parameters in the Nanoscope 6 format. To use a Nanoscope 5 file, change the 'nanoscope_version' input as follows (if a value other than 5 or 6 is given, or the script detects a file in the wrong format, python will exit):
```
expt_1.nanoscope_params(nanoscope_version=5)
```
If you would like to manually set the deflaction sensetivity, use the following function:
```
expt_1.set_def_sens(100)
```
If you would like to manually set the spring constant, use the following function:
```
expt_2.set_spr_const(0.32)
```
## Reading force curve data - Nanoscope files only
Now we have the parameters, we need the actual data. To import this, use the 'nanoscope_read' command:
```
expt_1.nanoscope_read()
expt_2.nanoscope_read()
expt_3.nanoscope_read()
```
## Plot raw data

To view the raw force curve data before any processing use the command below. The number in the brackets specifies which curve to plot. Remember, when using python, the numbers start at 0 (i.e. for 100 force curves the numbers are 0 to 99).
```
expt_1.plot_raw(1)
```
## Baseline
To adjust the baseline for each curve use the 'baseline' function. This works by taking the mean value over a specified area and setting this to zero. There are default values included to get you started:
```
expt_1.baseline()
expt_2.baseline()
expt_3.baseline()
```
If you need to change the region used for baselining (for example, if there is noise on part of the approach) use the 'start_pos' and 'end_pos' inputs. Note these should be a fraction of the distance from the contact section of the curve towards the baseline. The default values are 'start_pos = 0.45' and 'end_pos = 0.8'. Change them using as follows:
```
expt_1.baseline(start_pos = 0.5, end_pos = 0.7)
```
You can also choose to exculed curves with a noisy baseline. The code calculates teh standard deviation over the specified region. If this is above a specified value the curve will be deleted. You can do this spearatley for the approach and retract curves by setting 'max_approach_noise' and 'max_retract_noise'. The default value for both is 1000  (i.e. all curves retained) and this can be changed as follows:
```
expt_1.baseline(max_approach_noise = 2, max_retract_noise = 4)
```
To change the baseline region and maximum noise together:
```
expt_1.baseline(
    start_pos = 0.5,
    end_pos = 0.7,
    max_approach_noise = 2,
    max_retract_noise = 4
)
```
## Contact point
To set surface height to zero, use the 'contact' function. Note this currently interpolates between the first point below zero and the previous point, so may have trouble for noisy data:
```
expt_1.contact()
expt_2.contact()
expt_3.contact()
```
By default, the length position used will be the piezo position, rather than the actual tip position. If a cantilever is likley to experience large deflection when in contact with the surface, the tip position can be calculated using the measured force and deflection sensitivity by calling the 'adjust_tip_position' function.
```
expt_1.adjust_tip_position()
```
## Plot adjusted curves

To view individual curves once they have been aligned, use the 'plot_adjusted' command, typing the number of the curve to plot in the brackets:
```
expt_1.plot_adjusted(10)
```
You can also plot all of the curves and use a slider to cycle through them, using 'plot_curves':
```
expt_3.plot_curves()
```
## Delete bad curves

If a curve is noisy and needs to be removed, use the 'delete_curve' function. Put the numbers for all the curves to delete in the brackets:
```
expt_1.delete_curve(8,59,87)
expt_2.delete_curve(17)
```
## Calcluate adhesion
To calculate the adhesion for each curve, use 'calc_adhesion'. The mean and standard deviation for the adhesion calculated for each experiment will be printed in the output.
```
expt_1.calc_adhesion()
expt_2.calc_adhesion()
expt_3.calc_adhesion()
```
It is possible to plot a histogram of the calculated adhesion values by setting 'plot_hist' to 'True':
```
expt_2.calc_adhesion(plot_hist = True)
```
## Calculate modulus

To caclculate the elastic modulus, use 'calc_modulus':
```
expt_1.calc_modulus()
expt_2.calc_modulus()
expt_3.calc_modulus()
```
The method used currently follows the Hertz model for a spherical indenter:

![](https://camo.githubusercontent.com/e4f11366d77786e0d6a533f4ee99ff57343515ba/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46253230253344253230253543667261632537423425374425374233253744253230452535452a25323052253545253742302e3525374425323064253545253742312e35253744)

![](https://camo.githubusercontent.com/14cef2c5e957138704ec2f3fe2d0b75c4c1dc1b7/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436672616325374231253744253742452535452a25374425323025334425323025354366726163253742312d2535436e75253545325f31253744253742455f312537442532302b25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

Where:

![](https://camo.githubusercontent.com/8bc1794caf55a8564fe3d7d97864264621a42d42/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46) is Applied Force (nN)

![](https://camo.githubusercontent.com/ff544c24651cf98efd2ff0cc886a660c98396c2d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f452535452a) is Reduced Modulus (MPa)

![](https://camo.githubusercontent.com/c7cdc1b9a6698dd223b77822a075aa1246ce3ee1/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f52) is Sphere Radius (nm)

![](https://camo.githubusercontent.com/a9441c067b8389cfff3cccce033e3251a198017c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f64) is Total Deformation (i.e. indentation depth) (nm)

![](https://camo.githubusercontent.com/f518919e4b9dd3fffc951cd5f7a0f4d59a47dd89/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f31) is Indenter elastic modulus (MPa)

![](https://camo.githubusercontent.com/a0497118c2b189cb64abec7a2cac19dd405de497/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f32) is Sample elastic modulus (MPa)

![](https://camo.githubusercontent.com/c69ec83ba78d57af69c54499f1b81ab1fce8a871/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f31) is Indenter Poisson Ratio

![](https://camo.githubusercontent.com/452114f60b359b5dd1a5e252aee4a9d39f1bf10c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f32) is Sample Poisson Ratio

The method assumes a hard indenter is used, i.e. ![](https://camo.githubusercontent.com/e3b520ba355ebf2f1b19e5a5f67c721ce07f917d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f25354366726163253742312d2535436e75253545325f31253744253742455f312537442532302535436c6c25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

If a soft indenter, such as a colloidal probe, is used the 'soft_indenter' input should be set to 'True' and the Poisson Ratio and Elastic Modulus (MPa) of the indenter set using the         'indenter_poisson_ratio' and 'indenter_modulus' inputs respectivley.
```
expt_1.calc_modulus(poisson_ratio = 0.5, indenter_radius = 10 * 1e-9, soft_indenter = True, indenter_poisson_ratio = 0.5, indenter_modulus = 300)
```

The default method uses as surface poisson ratio of 0.5 and indenter radius of 10 nm (input required in m, i.e. 10E-09). To set these manually use the 'poisson_ratio' and 'indenter_radius' inputs:
```
expt_1.calc_modulus(poisson_ratio = 0.4, indenter_radius = 20e-9)
```
As with adhesion, it is possible to plot a histogram of the results:
```
expt_2.calc_modulus(plot_hist = True)
```
It is also possible to use the Sneddon model:
```
expt_1.calc_modulus_sneddon(poisson_ratio = 0.5, opening_angle = 35)
```
Or the four sided pyramid model:
```
expt_1.calc_modulus_pyramid(poisson_ratio = 0.5, face_angle = 35)
```

## Plot adhesion vs modulus

To view a scatter plot of adhesion vs modulus use the following command:
```
expt_1.plot_adhesion_modulus()
```
## Saving and reloading results
To save the calculated results, including force curves and parameters, in a format which can be re imported by the 'nanoforce' package later on use 'save_data'. This will open a window prompting you to select a folder and file name for saving. Leave the file extension blank.
```
expt_1.save_data()
```
You can also specify the filename and folder in the brackets:
```
expt_1.save_data('filename')
expt_2.save_data(r'\path\to\file')
```
To load saved data, create a new variable with the AFM class and call the 'load_data' function:
```
from  afm   import  AFM
loaded_data = AFM()
loaded_data.load_data()
```
It is also possible to save individual variables for use in other softwares. This can be done using the numpy 'savetxt' function, as follows for adhesion (a full list of available variables can be found in the 'Custom plotting and analysis' section below):
```
import numpy as np
np.savetxt('file_name_here.csv', expt_1.adheision)
```
## Built in comparison plots

There are a number of functions built in for plotting comparisons of the adhesion and modulus data for separate experiments. These have been left as basic functions. For more specific plots, folow the custom plotting section below.
To use the comparison plots, call the functions listed below directly from the AFM class with input variables for each experiment as an input.
#### Histogram comparison of adhesion
```
AFM.overlay_adhesion_hist(expt_1,expt_2,expt_3)
```
#### Histogram comparison of modulus
```
AFM.overlay_modulus_hist(expt_1,expt_2,expt_3)
```
#### Box plot comparison of adhesion
```
AFM.overlay_adhesion_box(expt_1,expt_2,expt_3)
```
#### Box plot comparison of modulus
```
AFM.overlay_modulus_box(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of adhesion
```
AFM.overlay_adhesion_bar(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of modulus
```
AFM.overlay_modulus_bar(expt_1,expt_2,expt_3)
```
## Force mapping
Force mapping can be carried out by aquiring a fine grid of force curves on a sample surface. The scan area and grid size will depend on the  uniformity of the surface and size of features, balanced with the length of time required for the scan. The force curves can be analyses as described above ant the force mapping function used to plot the reuslts. By default this function assumes a square grid of force curves across a 10 micron scan area and plots an adhesion map for this data.
```
expt_1.force_map()
```
If the grid is not square, the x_cells and y_cells inputs can be used to specify the grid size in the x and y directions. The auto_cell input must be set to False to manualy set the grid size.

```
expt_1.force_map(x_cells = 10, y_cells = 10, auto_cell = False)
```
The scan size can be specified using the x_scale and y_scale inputs.
```
expt_1.force_map(x_scale = 10, y_scale = 10)
```
The data to plot can be set using the plot input. Available plots are adhesion, modulus, topography (based on tip contact height), indent (indentation depth), max_force and snap_in.
```
expt_1.force_map(plot = 'modulus')
```
Finally, if using .nid files a 3d topographical representation of the surface, coloured with the chosen variable can be plotted by setting topography_3d to True.
```
expt_1.force_map(topography_3d = True)
```

## Editing and saving figures
All built in figures are produced using Plotly. To make changes to the formatting of these figures, the figure must be returned by the plotting function. This is achevied by setting the return_fig input to True.
```
fig_1 = force_map(return_fig = True)
```
This can then be edited using as described in the plotly documentation for each plot type. The returned figure can also be saved, again following the steps in the plotly documentation.

## Custom plotting and analysis
# 
The calculated variables are stored within each 'experiment' variable. These can be called when writing custom plotting or analysis scripts specific to your application. The variables available for each experiment are as follows:
```
expt_1.approach        # All approach curve data (nN)
expt_1.retract         # All retract curve data (nN)
expt_1.z_position      # All tip position data (nm)

expt_1.approach[:,i]   # Adhesion curve for experiment i
expt_1.retract[:,i]    # Adhesion curve for experiment i
expt_1.z_position[:,i] # Adhesion curve for experiment i

expt_1.def_sens        # Deflection sensetivity (nm/V)
expt_1.spr_const       # Spring constant (N/m)

expt_1.adheison        # Adhesion data (nN)
expt_1.modulus         # Modulus data (MPa)

expt_1.run_name        # Experiment name
```
Below is an example custom plotting script to overlay retract curves for multips experiments. This example uses plotly. There are a number of plotting tools in this package, and more detail (including example scripts) is available from https://plotly.com/python/
```
import  plotly.graph_objects  as      go
from    afm                   import  AFM

expt_1 = AFM()
expt_2 = AFM()

expt_1.run()
expt_2.run()

fig = go.Figure()

curve_number = 12

fig.add_trace(go.Scatter(y = expt_1.retract[:,curve_number],
                         x = expt_1.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 1'))

fig.add_trace(go.Scatter(y = expt_2.retract[:,curve_number],
                         x = expt_2.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 2'))

fig.update_layout(
    yaxis_title_text='Force (nN)',
    xaxis_title_text='Tip separation (nm)')

fig.show()
```
## Example analysis script
An example analysis script is available from Git Hub:
https://github.com/crj341/nanoforce/blob/master/afm_analysis.py

A script is also available here to apply a clustering algorithm to identify groups of corresponding adhesion and modulus values, while removing noise:
https://github.com/crj341/nanoforce/blob/master/afm_clustering.py

## Release history:
0.0.1 to 0.0.2 - Development

0.0.3 - Initial release

0.0.4 - Added dependenices to setup (numpy, scipy, easygui, plotly)

0.0.5 to 0.0.7 - Updated README and added class/function descriptions

0.0.8 - Fixed issue extracting spring constants with no decimal point

0.0.9 - Fixed packaging error

0.0.10 - Added support for Nanoscope version 5

0.0.11 to 0.1.0 - Bug fixes

0.1.1 - Full release

0.1.2 to 0.1.5 - Updated 'delete_curves' numbering system, added warning for bad curves in 'contact'

0.1.6 to 0.1.7 - Added support for files not starting at 000

0.1.8 - Added mac support

0.1.9 - Added violin plots for adhesion and modulus

0.1.10 - Fixed units issue for modulus calculaiton

0.1.11 - Added option to calculate actual tip position in 'nanoscope read'. Added option to include indenter Poisson Ratio and Moudlus in calc_modulus.

0.1.12 to - 0.1.24 - Fixed bugs

0.2.0 - New features now stable. Updated README.

0.2.1 to 0.2.19 - Initial support for Nanosurf AFM .nid files.

0.2.20 to 0.2.21 - Added Sneddon model and Four sided pyramid for modulus calculation.

0.2.22 - Fixed bug determining points per force curve.

0.2.23 - Fixed bug where adhesion was calculated as nan.

0.2.24 - Updated Readme for Nanosurf .nid files.

0.3.0 - New features now stable. Updated README.

0.3.1 - Added adhesion map function.

0.3.2 - Added error message to baseline function.

0.3.3 - Snap in force, max indent force, contact height and indent depth calculated.

0.3.4 to 0.3.6 - Additional force map plotting options.

0.3.7 - All figures now returnable. Updated README.

0.3.8 - Bug fixed which prevented force_map being called more than once per dataset.

0.3.9 - Bug fixed where indentation depth calculated incorrectly when using 'adjust_tip_position'.

0.3.10 to 0.3.15 - Updated Sneddon model modulus function to use line fit of indentation.

0.3.16 to 0.3.21 - Update to Hertz model.

0.3.22 to 0.3.28 - Added function to determine snap in distance.

0.3.29 t0 0.3.32 - Added function to calculate work of adhesion.



%package help
Summary:	Development documents and examples for nanoforce
Provides:	python3-nanoforce-doc
%description help
# Nanoforce

Package to import and analyse AFM force curves produced using Nanoscope 5 & 6 and Nanosurf .nid files

# Python AFM Analysis Tutorial
This tutorial demonstrates the basics of the AFM python tools for data analysis. Currently the scripts are available as an offline file or can be installed from PyPI using:
```
pip install nanoforce
```
Following the steps below will show you how to carry out simple analysis and plotting of AFM data produced with Nanoscope and explain how to carryout your own more specific analysis. For each snippet of code - try running it to see what it outputs. You can download the tutorial as a Jupyter notebook from:
https://github.com/crj341/nanoforce/blob/master/AFM_tutorial.ipynb

## Prerequisites
To use the tools you will need a few python packages (numpy, plotly, scipy and easygui). To install these type the following commands into a terminal window (unless you have installed the nanoforce package from PyPI (i.e. using pip), in which case they should have installed automatically):
```
pip install numpy
pip install plotly
pip install scipy
pip install easygui
```
## Getting started
Start off by creating a folder containing your script (and the afm.py file if you have not installed the package from PyPI). For beginers it may be best to use a jupyter notebook here, as has been used for this tutorial. Open your script file and type (note this is case sensetive):
```
from  nanoforce import  AFM
```
To carry out basic analysis you can use the inbuilt 'run' command.
(Check the window hasn't opened behind python if this is running slowly.)
```
expt = AFM()
expt.run()
```
This will open a window, prompting you to select the AFM data you wish to analyse. Navigate to the folder and select the file with the '.000' extension. The tools will automatically align the data with thebaseline and set the surface height to zero. A plot will open with the processed force curves, use the slider to move through each experiment. The code will also aclculate the adhesion and modules. The mean and standard deviation will appera in the output.

It is likley you will wish to carry out more detailled analysis than provided by this function. The following sections will guide you through all the individual functions available.

## Naming variables
If you have carried out multiple experiments and would like to compare the results, you should assign an instance of the AFM class for each. Name a variable for each experiment and call the class:
```
expt_1 = AFM()
expt_2 = AFM()
expt_3 = AFM()
```
## Importing data - Nanoscope files (.000)
To import a nanoscope dataset use the 'input_files' command for each experiment:

Note - the GUI for selecting a file will only run on a local version of python (not in the Colab tutorial). This will prompt an error in Colab - use manual file_name entry below.
```
expt_1.input_files()
expt_2.input_files()
expt_3.input_files()
```
This will open a window prompting you to select the '.000' file for each experiment. The code will then automatcally import all files with the same name.

If you would prefer to manually set the file name (useful if you are running the script multiple time), you can name the 'file_name' variable and set the 'gui_on' flag in 'import_files' to 'False':
```
expt_1.file_name = '/insert/your/file/path/here.000'
expt_1.input_files(gui_on = False)
```
## Importing data - Nanoscope files (.000)
To import a Nanosurf .nid dataset first set the file name:
```
expt_1.file_name = r'C:\file\path\goes\here.nid
```
Then call the following function to import all force curve data from the file@
```
epxt_1.nanosurf_import
```
## Naming experiments
It may be useful to name each experiment, for plotting etc. To do so, use the following command:
```
expt_1.set_run_name('Sample 1')
```
## Finding parameters - Nanoscope files only

The next step is to import the relevant parameters from the file (such as deflection sensetivity and spring constant). To do so, due the 'nanoscope_params' function as follows:
```
expt_1.nanoscope_params()
expt_2.nanoscope_params()
expt_3.nanoscope_params()
```
By default this will search for parameters in the Nanoscope 6 format. To use a Nanoscope 5 file, change the 'nanoscope_version' input as follows (if a value other than 5 or 6 is given, or the script detects a file in the wrong format, python will exit):
```
expt_1.nanoscope_params(nanoscope_version=5)
```
If you would like to manually set the deflaction sensetivity, use the following function:
```
expt_1.set_def_sens(100)
```
If you would like to manually set the spring constant, use the following function:
```
expt_2.set_spr_const(0.32)
```
## Reading force curve data - Nanoscope files only
Now we have the parameters, we need the actual data. To import this, use the 'nanoscope_read' command:
```
expt_1.nanoscope_read()
expt_2.nanoscope_read()
expt_3.nanoscope_read()
```
## Plot raw data

To view the raw force curve data before any processing use the command below. The number in the brackets specifies which curve to plot. Remember, when using python, the numbers start at 0 (i.e. for 100 force curves the numbers are 0 to 99).
```
expt_1.plot_raw(1)
```
## Baseline
To adjust the baseline for each curve use the 'baseline' function. This works by taking the mean value over a specified area and setting this to zero. There are default values included to get you started:
```
expt_1.baseline()
expt_2.baseline()
expt_3.baseline()
```
If you need to change the region used for baselining (for example, if there is noise on part of the approach) use the 'start_pos' and 'end_pos' inputs. Note these should be a fraction of the distance from the contact section of the curve towards the baseline. The default values are 'start_pos = 0.45' and 'end_pos = 0.8'. Change them using as follows:
```
expt_1.baseline(start_pos = 0.5, end_pos = 0.7)
```
You can also choose to exculed curves with a noisy baseline. The code calculates teh standard deviation over the specified region. If this is above a specified value the curve will be deleted. You can do this spearatley for the approach and retract curves by setting 'max_approach_noise' and 'max_retract_noise'. The default value for both is 1000  (i.e. all curves retained) and this can be changed as follows:
```
expt_1.baseline(max_approach_noise = 2, max_retract_noise = 4)
```
To change the baseline region and maximum noise together:
```
expt_1.baseline(
    start_pos = 0.5,
    end_pos = 0.7,
    max_approach_noise = 2,
    max_retract_noise = 4
)
```
## Contact point
To set surface height to zero, use the 'contact' function. Note this currently interpolates between the first point below zero and the previous point, so may have trouble for noisy data:
```
expt_1.contact()
expt_2.contact()
expt_3.contact()
```
By default, the length position used will be the piezo position, rather than the actual tip position. If a cantilever is likley to experience large deflection when in contact with the surface, the tip position can be calculated using the measured force and deflection sensitivity by calling the 'adjust_tip_position' function.
```
expt_1.adjust_tip_position()
```
## Plot adjusted curves

To view individual curves once they have been aligned, use the 'plot_adjusted' command, typing the number of the curve to plot in the brackets:
```
expt_1.plot_adjusted(10)
```
You can also plot all of the curves and use a slider to cycle through them, using 'plot_curves':
```
expt_3.plot_curves()
```
## Delete bad curves

If a curve is noisy and needs to be removed, use the 'delete_curve' function. Put the numbers for all the curves to delete in the brackets:
```
expt_1.delete_curve(8,59,87)
expt_2.delete_curve(17)
```
## Calcluate adhesion
To calculate the adhesion for each curve, use 'calc_adhesion'. The mean and standard deviation for the adhesion calculated for each experiment will be printed in the output.
```
expt_1.calc_adhesion()
expt_2.calc_adhesion()
expt_3.calc_adhesion()
```
It is possible to plot a histogram of the calculated adhesion values by setting 'plot_hist' to 'True':
```
expt_2.calc_adhesion(plot_hist = True)
```
## Calculate modulus

To caclculate the elastic modulus, use 'calc_modulus':
```
expt_1.calc_modulus()
expt_2.calc_modulus()
expt_3.calc_modulus()
```
The method used currently follows the Hertz model for a spherical indenter:

![](https://camo.githubusercontent.com/e4f11366d77786e0d6a533f4ee99ff57343515ba/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46253230253344253230253543667261632537423425374425374233253744253230452535452a25323052253545253742302e3525374425323064253545253742312e35253744)

![](https://camo.githubusercontent.com/14cef2c5e957138704ec2f3fe2d0b75c4c1dc1b7/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436672616325374231253744253742452535452a25374425323025334425323025354366726163253742312d2535436e75253545325f31253744253742455f312537442532302b25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

Where:

![](https://camo.githubusercontent.com/8bc1794caf55a8564fe3d7d97864264621a42d42/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f46) is Applied Force (nN)

![](https://camo.githubusercontent.com/ff544c24651cf98efd2ff0cc886a660c98396c2d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f452535452a) is Reduced Modulus (MPa)

![](https://camo.githubusercontent.com/c7cdc1b9a6698dd223b77822a075aa1246ce3ee1/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f52) is Sphere Radius (nm)

![](https://camo.githubusercontent.com/a9441c067b8389cfff3cccce033e3251a198017c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f64) is Total Deformation (i.e. indentation depth) (nm)

![](https://camo.githubusercontent.com/f518919e4b9dd3fffc951cd5f7a0f4d59a47dd89/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f31) is Indenter elastic modulus (MPa)

![](https://camo.githubusercontent.com/a0497118c2b189cb64abec7a2cac19dd405de497/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f455f32) is Sample elastic modulus (MPa)

![](https://camo.githubusercontent.com/c69ec83ba78d57af69c54499f1b81ab1fce8a871/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f31) is Indenter Poisson Ratio

![](https://camo.githubusercontent.com/452114f60b359b5dd1a5e252aee4a9d39f1bf10c/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f2535436e755f32) is Sample Poisson Ratio

The method assumes a hard indenter is used, i.e. ![](https://camo.githubusercontent.com/e3b520ba355ebf2f1b19e5a5f67c721ce07f917d/68747470733a2f2f6c617465782e636f6465636f67732e636f6d2f6769662e6c617465783f25354366726163253742312d2535436e75253545325f31253744253742455f312537442532302535436c6c25323025354366726163253742312d2535436e75253545325f32253744253742455f32253744)

If a soft indenter, such as a colloidal probe, is used the 'soft_indenter' input should be set to 'True' and the Poisson Ratio and Elastic Modulus (MPa) of the indenter set using the         'indenter_poisson_ratio' and 'indenter_modulus' inputs respectivley.
```
expt_1.calc_modulus(poisson_ratio = 0.5, indenter_radius = 10 * 1e-9, soft_indenter = True, indenter_poisson_ratio = 0.5, indenter_modulus = 300)
```

The default method uses as surface poisson ratio of 0.5 and indenter radius of 10 nm (input required in m, i.e. 10E-09). To set these manually use the 'poisson_ratio' and 'indenter_radius' inputs:
```
expt_1.calc_modulus(poisson_ratio = 0.4, indenter_radius = 20e-9)
```
As with adhesion, it is possible to plot a histogram of the results:
```
expt_2.calc_modulus(plot_hist = True)
```
It is also possible to use the Sneddon model:
```
expt_1.calc_modulus_sneddon(poisson_ratio = 0.5, opening_angle = 35)
```
Or the four sided pyramid model:
```
expt_1.calc_modulus_pyramid(poisson_ratio = 0.5, face_angle = 35)
```

## Plot adhesion vs modulus

To view a scatter plot of adhesion vs modulus use the following command:
```
expt_1.plot_adhesion_modulus()
```
## Saving and reloading results
To save the calculated results, including force curves and parameters, in a format which can be re imported by the 'nanoforce' package later on use 'save_data'. This will open a window prompting you to select a folder and file name for saving. Leave the file extension blank.
```
expt_1.save_data()
```
You can also specify the filename and folder in the brackets:
```
expt_1.save_data('filename')
expt_2.save_data(r'\path\to\file')
```
To load saved data, create a new variable with the AFM class and call the 'load_data' function:
```
from  afm   import  AFM
loaded_data = AFM()
loaded_data.load_data()
```
It is also possible to save individual variables for use in other softwares. This can be done using the numpy 'savetxt' function, as follows for adhesion (a full list of available variables can be found in the 'Custom plotting and analysis' section below):
```
import numpy as np
np.savetxt('file_name_here.csv', expt_1.adheision)
```
## Built in comparison plots

There are a number of functions built in for plotting comparisons of the adhesion and modulus data for separate experiments. These have been left as basic functions. For more specific plots, folow the custom plotting section below.
To use the comparison plots, call the functions listed below directly from the AFM class with input variables for each experiment as an input.
#### Histogram comparison of adhesion
```
AFM.overlay_adhesion_hist(expt_1,expt_2,expt_3)
```
#### Histogram comparison of modulus
```
AFM.overlay_modulus_hist(expt_1,expt_2,expt_3)
```
#### Box plot comparison of adhesion
```
AFM.overlay_adhesion_box(expt_1,expt_2,expt_3)
```
#### Box plot comparison of modulus
```
AFM.overlay_modulus_box(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of adhesion
```
AFM.overlay_adhesion_bar(expt_1,expt_2,expt_3)
```
#### Bar chart (with error bars) comparison of modulus
```
AFM.overlay_modulus_bar(expt_1,expt_2,expt_3)
```
## Force mapping
Force mapping can be carried out by aquiring a fine grid of force curves on a sample surface. The scan area and grid size will depend on the  uniformity of the surface and size of features, balanced with the length of time required for the scan. The force curves can be analyses as described above ant the force mapping function used to plot the reuslts. By default this function assumes a square grid of force curves across a 10 micron scan area and plots an adhesion map for this data.
```
expt_1.force_map()
```
If the grid is not square, the x_cells and y_cells inputs can be used to specify the grid size in the x and y directions. The auto_cell input must be set to False to manualy set the grid size.

```
expt_1.force_map(x_cells = 10, y_cells = 10, auto_cell = False)
```
The scan size can be specified using the x_scale and y_scale inputs.
```
expt_1.force_map(x_scale = 10, y_scale = 10)
```
The data to plot can be set using the plot input. Available plots are adhesion, modulus, topography (based on tip contact height), indent (indentation depth), max_force and snap_in.
```
expt_1.force_map(plot = 'modulus')
```
Finally, if using .nid files a 3d topographical representation of the surface, coloured with the chosen variable can be plotted by setting topography_3d to True.
```
expt_1.force_map(topography_3d = True)
```

## Editing and saving figures
All built in figures are produced using Plotly. To make changes to the formatting of these figures, the figure must be returned by the plotting function. This is achevied by setting the return_fig input to True.
```
fig_1 = force_map(return_fig = True)
```
This can then be edited using as described in the plotly documentation for each plot type. The returned figure can also be saved, again following the steps in the plotly documentation.

## Custom plotting and analysis
# 
The calculated variables are stored within each 'experiment' variable. These can be called when writing custom plotting or analysis scripts specific to your application. The variables available for each experiment are as follows:
```
expt_1.approach        # All approach curve data (nN)
expt_1.retract         # All retract curve data (nN)
expt_1.z_position      # All tip position data (nm)

expt_1.approach[:,i]   # Adhesion curve for experiment i
expt_1.retract[:,i]    # Adhesion curve for experiment i
expt_1.z_position[:,i] # Adhesion curve for experiment i

expt_1.def_sens        # Deflection sensetivity (nm/V)
expt_1.spr_const       # Spring constant (N/m)

expt_1.adheison        # Adhesion data (nN)
expt_1.modulus         # Modulus data (MPa)

expt_1.run_name        # Experiment name
```
Below is an example custom plotting script to overlay retract curves for multips experiments. This example uses plotly. There are a number of plotting tools in this package, and more detail (including example scripts) is available from https://plotly.com/python/
```
import  plotly.graph_objects  as      go
from    afm                   import  AFM

expt_1 = AFM()
expt_2 = AFM()

expt_1.run()
expt_2.run()

fig = go.Figure()

curve_number = 12

fig.add_trace(go.Scatter(y = expt_1.retract[:,curve_number],
                         x = expt_1.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 1'))

fig.add_trace(go.Scatter(y = expt_2.retract[:,curve_number],
                         x = expt_2.z_position[:,curve_number],
                         mode='lines',
                         name = 'Sample 2'))

fig.update_layout(
    yaxis_title_text='Force (nN)',
    xaxis_title_text='Tip separation (nm)')

fig.show()
```
## Example analysis script
An example analysis script is available from Git Hub:
https://github.com/crj341/nanoforce/blob/master/afm_analysis.py

A script is also available here to apply a clustering algorithm to identify groups of corresponding adhesion and modulus values, while removing noise:
https://github.com/crj341/nanoforce/blob/master/afm_clustering.py

## Release history:
0.0.1 to 0.0.2 - Development

0.0.3 - Initial release

0.0.4 - Added dependenices to setup (numpy, scipy, easygui, plotly)

0.0.5 to 0.0.7 - Updated README and added class/function descriptions

0.0.8 - Fixed issue extracting spring constants with no decimal point

0.0.9 - Fixed packaging error

0.0.10 - Added support for Nanoscope version 5

0.0.11 to 0.1.0 - Bug fixes

0.1.1 - Full release

0.1.2 to 0.1.5 - Updated 'delete_curves' numbering system, added warning for bad curves in 'contact'

0.1.6 to 0.1.7 - Added support for files not starting at 000

0.1.8 - Added mac support

0.1.9 - Added violin plots for adhesion and modulus

0.1.10 - Fixed units issue for modulus calculaiton

0.1.11 - Added option to calculate actual tip position in 'nanoscope read'. Added option to include indenter Poisson Ratio and Moudlus in calc_modulus.

0.1.12 to - 0.1.24 - Fixed bugs

0.2.0 - New features now stable. Updated README.

0.2.1 to 0.2.19 - Initial support for Nanosurf AFM .nid files.

0.2.20 to 0.2.21 - Added Sneddon model and Four sided pyramid for modulus calculation.

0.2.22 - Fixed bug determining points per force curve.

0.2.23 - Fixed bug where adhesion was calculated as nan.

0.2.24 - Updated Readme for Nanosurf .nid files.

0.3.0 - New features now stable. Updated README.

0.3.1 - Added adhesion map function.

0.3.2 - Added error message to baseline function.

0.3.3 - Snap in force, max indent force, contact height and indent depth calculated.

0.3.4 to 0.3.6 - Additional force map plotting options.

0.3.7 - All figures now returnable. Updated README.

0.3.8 - Bug fixed which prevented force_map being called more than once per dataset.

0.3.9 - Bug fixed where indentation depth calculated incorrectly when using 'adjust_tip_position'.

0.3.10 to 0.3.15 - Updated Sneddon model modulus function to use line fit of indentation.

0.3.16 to 0.3.21 - Update to Hertz model.

0.3.22 to 0.3.28 - Added function to determine snap in distance.

0.3.29 t0 0.3.32 - Added function to calculate work of adhesion.



%prep
%autosetup -n nanoforce-0.3.32

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-nanoforce -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Wed May 10 2023 Python_Bot <Python_Bot@openeuler.org> - 0.3.32-1
- Package Spec generated