measure_store_offsets_DMK3


calculates offsets even when negative.
/**
 * Macro for data evaluation on PhotosynQ.org
 * by: David M. Kramer
 * created: December 18, 2018 1:05 PM
 */     
      
 // Define the output object here
 
var offset_min = -100;
var offset_max = 500;
var output = {};

var start_index = 4;
var end_index = 9;

var number_pulses=json.v_arrays[0][0];
output.number_pulses=number_pulses;
  
var xvals_all=json.v_arrays[1]; //.slice(1,9);
//output.pulse_durations=xvals_all;
 
// Check if the key time exists in json
if (json.time !== undefined){
    // Add key time and value to output
    output.time = json.time;
}   
 
var offset_traces=GetProtocolByLabel( "offset_detector1", json );

var d1_off=ArrayNth( offset_traces.data_raw, 2, 0 );
var d3_off=ArrayNth( offset_traces.data_raw, 2, 1 );

output.det_1_offset_array=d1_off;
output.det_3_offset_array=d3_off;

var det_1_a = [];
var det_3_a = [];
var xvals = [];
for (var i=start_index; i<end_index; i++){
	det_1_a.push(MathMEAN(d1_off.slice(i*number_pulses,(i+1)*number_pulses)));
	det_3_a.push(MathMEAN(d3_off.slice(i*number_pulses,(i+1)*number_pulses)));
    xvals.push(xvals_all[i]);
} 
 
output.det_1_a=det_1_a;
output.det_3_a=det_3_a;
//output.xvals=xvals;

var offset_traces_dark=GetProtocolByLabel( "offset_detector_dark", json );

var d1_off_2=ArrayNth( offset_traces_dark.data_raw, 2, 0 );
var d3_off_2=ArrayNth( offset_traces_dark.data_raw, 2, 1 );


var det_1_b = [];
var det_3_b = [];
for (var i=start_index; i<end_index; i++){
	det_1_b.push(MathMEAN(d1_off_2.slice(i*number_pulses,(i+1)*number_pulses)));
	det_3_b.push(MathMEAN(d3_off_2.slice(i*number_pulses,(i+1)*number_pulses)));
} 

output.det_1_b=det_1_b;
output.det_3_b=det_3_b;

fit_lin_1=MathLINREG(xvals, det_1_a);
 
//output.fit_lin_1_low_b = fit_lin_1.b;
//output.fit_lin_1_low_m = fit_lin_1.m;
output.fit_lin_1_b_cor = MathROUND(fit_lin_1.b + (1.5*fit_lin_1.m),0);

fit_lin_1_b=MathLINREG(xvals, det_1_b);

//output.fit_lin_1_high_b = fit_lin_1_b.b;
//output.fit_lin_1_high_m = fit_lin_1_b.m;

//output.fit_lin_1_high_b = fit_lin_1_b.b;
//output.fit_lin_1_high_m = fit_lin_1_b.m;
output.fit_lin_1_high_b_cor = MathROUND(fit_lin_1_b.b + (0*fit_lin_1_b.m),0);

var det_1_diff = TransformTrace( "subtract", det_1_b, det_1_a);
output.det_1_diff=det_1_diff;

fit_lin_1_diff=MathLINREG(xvals, det_1_diff);

output.det_1_diff_b = MathROUND(fit_lin_1_diff.b,0);
output.det_1_diff_m = MathROUND(fit_lin_1_diff.m,2);

output.toDevice="set_detector_offset+1+";
output.toDevice+=output.det_1_diff_b;
output.toDevice+="+";


/////////
fit_lin_3=MathLINREG(xvals, det_3_a);
 
//output.fit_lin_3_b = fit_lin_3.b;
//output.fit_lin_3_m = fit_lin_3.m;
output.fit_lin_3_b_cor = MathROUND(fit_lin_3.b + (0*fit_lin_3.m),0);

fit_lin_3_b=MathLINREG(xvals, det_3_b);

//output.fit_lin_3_b_b = fit_lin_3_b.b;
//output.fit_lin_3_b_m = fit_lin_3_b.m;
//output.fit_lin_3_b_r2=fit_lin_3_b.r2;
//output.fit_lin_3_b_b = fit_lin_3_b.b;
//output.fit_lin_3_b_m = fit_lin_3_b.m;
output.fit_lin_3_b_b_cor = MathROUND(fit_lin_3_b.b+ (-1.0*fit_lin_3_b.m),0);

var det_1_diff = TransformTrace( "subtract", det_1_b, det_1_a);
output.det_1_diff=det_1_diff;

fit_lin_1_diff=MathLINREG(xvals, det_1_diff);

output.det_1_diff_b = MathROUND(fit_lin_1_diff.b,0);
output.det_1_diff_m = MathROUND(fit_lin_1_diff.m,2);
output.det_1_diff_b_corr = MathROUND(fit_lin_1_diff.b + 1*fit_lin_1_diff.m ,0);


var det_3_diff = TransformTrace( "subtract", det_3_b, det_3_a);
output.det_3_diff=det_3_diff;

fit_lin_3_diff=MathLINREG(xvals, det_3_diff);

output.det_3_diff_b = MathROUND(fit_lin_3_diff.b,0);
output.det_3_diff_m = MathROUND(fit_lin_3_diff.m,2);
output.det_3_diff_b_corr = MathROUND(fit_lin_3_diff.b - 1*fit_lin_3_diff.m,0);


output.toDevice="set_detector_offset+1+";
output.toDevice+=output.fit_lin_1_b_cor;
output.toDevice+="+";


output.toDevice+="set_detector_offset+3+";
output.toDevice+=output.fit_lin_3_b_cor; 
output.toDevice+="+"; 

output.toDevice+="set_detector_offset+2+0+";
output.toDevice+="set_detector_offset+4+0+";

return output;


var ttt=0;
for (var t=240; t<d1_off_2.length; t++){
  ttt+=d1_off_2[t];
}
ttt=ttt/(d1_off_2.length-240);
var di_off_1_2_n=TransformTrace( "divide", d1_off_2, ttt);
output.di_off_1_2_n=di_off_1_2_n;

var ttt=0;
for (var t=240; t<d3_off_2.length; t++){
  ttt+=d3_off_2[t];
}
ttt=ttt/(d3_off_2.length-240);
var di_off_3_2_n=TransformTrace( "divide", d3_off_2, ttt);

//output.ym=ttt/d1_off_2.length;
output.di_off_3_2_n=di_off_3_2_n;

fit_lin_1=MathLINREG(xvals, det_1_a);

output.fit_lin_1_b = fit_lin_1.b;
output.fit_lin_1_m = fit_lin_1.m;

fit_lin_1_2=MathLINREG(xvals, di_off_1_2_n);

output.fit_lin_1_2_b = fit_lin_1_2.b;
output.fit_lin_1_2_m = fit_lin_1_2.m;

fit_lin_3=MathLINREG(xvals, det_3_a);

output.fit_lin_3_b = fit_lin_3.b;
output.fit_lin_3_m = fit_lin_3.m;



return output;

//var d1_off = GetProtocolByLabel( "offset_detector1", json );
//var d3_off = GetProtocolByLabel( "offset_detector3", json );
  

//output["detector 1 offset test "]=d1_off.data_raw;
//output["detector 3 offset test "]=d3_off.data_raw;
   
offset_raw_avs=[];
inv_offset=[];
var xvals=[];
for (var i=start_index; i<end_index; i++){
	offset_raw_avs.push(MathMEAN(d1_off.slice(i*number_pulses,(i+1)*number_pulses)));
	var io = MathMEAN(d1_off.slice(i*number_pulses,(i+1)*number_pulses));
  	inv_offset.push(1/io);
    xvals.push(xvals_all[i]);
} 
  // offset_raw_avs.push(MathMEAN(d1_off.data_raw.slice(10,19)));
// offset_raw_avs.push(MathMEAN(d1_off.data_raw.slice(20,29)));
   
output.offset_raw_avs=offset_raw_avs;


//var values = ArrayZip( xvals, inv_offset );
//mnl = NonLinearRegression(values, {equation: "b + a * e(- x / c)", initial: [30,4000,1000]});
//output.mnla=mnl.parameters[2].value;
 

fit_inv1=MathLINREG(xvals, inv_offset);
output.fit_inv1=1/fit_inv1.b;

fit_offset_det1=MathROUND(1/fit_inv1.b,0);
output.fit_inv1=fit_offset_det1;
//xvals=TransformTrace( "subtract", xvals, 15);

fit_1=MathLINREG(xvals, offset_raw_avs);

output.fit_lin_1=fit_1.b;
output.offset_1=MathROUND(fit_offset_det1);
//output.slope_1=MathROUND(fit_1.m,2); 
output.r2_1=MathROUND(fit_inv1.r2,3); 
 



//var d3_off = GetProtocolByLabel( "offset_detector3", json );
 
//output.d3_off=MathROUND(d3_off.data_raw);


offset_raw_avs3=[];
var inv_offset3=[];
for (var i=start_index; i<end_index; i++){
	offset_raw_avs3.push(MathMEAN(d3_off.slice(i*number_pulses,(i+1)*number_pulses)));
	var io = MathMEAN(d3_off.slice(i*number_pulses,(i+1)*number_pulses));
  	inv_offset3.push(1/io);
}  
  
// offset_raw_avs3.push(MathMEAN(d3_off.data_raw.slice(0,9)));
// offset_raw_avs3.push(MathMEAN(d3_off.data_raw.slice(10,19)));
// offset_raw_avs3.push(MathMEAN(d3_off.data_raw.slice(20,29)));
 

output.offset_raw_avs3=offset_raw_avs3;

//output.xvals=xvals;

//fit_inv1=MathLINREG(xvals, inv_offset);
//output.fit_inv1=1/fit_inv1.b;


fit_inv3=MathLINREG(xvals, inv_offset3);
fit_offset_det3=MathROUND(1/fit_inv3.b,0);
output.fit_inv3=fit_offset_det3;

fit_3=MathLINREG(xvals, offset_raw_avs3);
output.fit_lin_3=fit_3.b;

if ((fit_offset_det3>offset_max) || (fit_offset_det3<offset_min)){ 
  danger("offset for detector 3 is too large. May cause poor resutls",output);
}

if ((fit_offset_det1>offset_max) || (fit_offset_det1<offset_min)){ 
  danger("offset for detector 1 is too large. May cause poor resutls",output);
}

//output.slope_3=MathROUND(fit_3.m); 
//output.r2_3=MathROUND(fit_3.r2,3); 

//output.offset_3=MathROUND(fit_offset_det3);
output.toDevice="set_detector_offset+1+";
output.toDevice+=output.fit_lin_1_b_cor;
output.toDevice+="+";


output.toDevice+="set_detector_offset+3+";
output.toDevice+=output.fit_lin_1_b_cor; 
output.toDevice+="+"; 

output.toDevice+="set_detector_offset+2+0+";
output.toDevice+="set_detector_offset+4+0+";

// Return data
return output;
{
  "time": 1546558205063,
  "device_name": "MultispeQ",
  "device_version": "2",
  "device_id": "63:00:08:08",
  "device_battery": 105,
  "device_firmware": 2.006,
  "sample": [
    [
      {
        "time": 1546558205063,
        "v_arrays": [
          [
            "30",
            "7",
            "300",
            "1",
            "200"
          ],
          [
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8"
          ]
        ],
        "protocol_id": 1,
        "set": [
          {
            "time": 1546558205077,
            "set_detector_offsets": [
              [
                1,
                0
              ],
              [
                3,
                0
              ]
            ],
            "message": [
              "alert",
              "clamp 2X calibration panel 1",
              "ok"
            ],
            "label": "offset_detector1",
            "autogain": [
              [
                10,
                1,
                30,
                589,
                7609
              ],
              [
                4,
                3,
                30,
                280,
                8717
              ]
            ],
            "data_raw": [
              0,
              168,
              0,
              169,
              0,
              169,
              0,
              169,
              0,
              168,
              0,
              169,
              0,
              169,
              0,
              167,
              0,
              168,
              0,
              169,
              0,
              171,
              0,
              169,
              0,
              170,
              0,
              171,
              0,
              169,
              0,
              169,
              0,
              169,
              0,
              169,
              0,
              168,
              0,
              171,
              0,
              170,
              0,
              169,
              0,
              167,
              0,
              167,
              0,
              168,
              0,
              167,
              0,
              168,
              0,
              169,
              0,
              171,
              0,
              168,
              41,
              503,
              39,
              502,
              43,
              504,
              40,
              503,
              41,
              502,
              43,
              501,
              39,
              503,
              45,
              502,
              41,
              504,
              42,
              504,
              40,
              504,
              44,
              502,
              41,
              501,
              39,
              503,
              43,
              501,
              41,
              503,
              42,
              501,
              41,
              502,
              44,
              505,
              41,
              504,
              40,
              502,
              41,
              502,
              42,
              504,
              43,
              502,
              39,
              502,
              43,
              503,
              41,
              503,
              43,
              504,
              42,
              501,
              42,
              503,
              229,
              873,
              231,
              873,
              227,
              874,
              231,
              871,
              229,
              871,
              233,
              873,
              226,
              872,
              225,
              873,
              228,
              873,
              226,
              873,
              231,
              874,
              226,
              872,
              230,
              874,
              228,
              875,
              234,
              873,
              225,
              872,
              227,
              873,
              229,
              873,
              230,
              873,
              225,
              871,
              229,
              874,
              228,
              871,
              227,
              872,
              225,
              873,
              228,
              871,
              231,
              874,
              230,
              874,
              233,
              871,
              230,
              872,
              229,
              871,
              493,
              1241,
              487,
              1240,
              495,
              1243,
              491,
              1241,
              489,
              1242,
              493,
              1241,
              491,
              1242,
              488,
              1239,
              493,
              1241,
              490,
              1242,
              491,
              1238,
              490,
              1241,
              491,
              1243,
              489,
              1241,
              491,
              1241,
              494,
              1239,
              493,
              1242,
              489,
              1241,
              486,
              1241,
              491,
              1241,
              490,
              1241,
              493,
              1241,
              491,
              1241,
              491,
              1239,
              492,
              1241,
              490,
              1239,
              493,
              1241,
              491,
              1240,
              493,
              1240,
              492,
              1241,
              774,
              1606,
              771,
              1605,
              761,
              1606,
              769,
              1607,
              765,
              1605,
              766,
              1608,
              767,
              1605,
              768,
              1607,
              767,
              1605,
              769,
              1606,
              767,
              1607,
              761,
              1606,
              775,
              1606,
              761,
              1604,
              763,
              1602,
              768,
              1605,
              768,
              1606,
              767,
              1606,
              771,
              1606,
              767,
              1606,
              763,
              1604,
              769,
              1607,
              760,
              1605,
              769,
              1607,
              765,
              1605,
              767,
              1605,
              769,
              1605,
              761,
              1603,
              770,
              1605,
              771,
              1607,
              1052,
              1967,
              1045,
              1965,
              1047,
              1967,
              1050,
              1966,
              1051,
              1966,
              1049,
              1965,
              1047,
              1968,
              1055,
              1966,
              1051,
              1965,
              1054,
              1966,
              1048,
              1966,
              1047,
              1965,
              1052,
              1963,
              1053,
              1965,
              1055,
              1965,
              1056,
              1966,
              1049,
              1965,
              1042,
              1965,
              1052,
              1968,
              1045,
              1963,
              1049,
              1967,
              1049,
              1967,
              1045,
              1965,
              1045,
              1969,
              1043,
              1966,
              1043,
              1967,
              1051,
              1966,
              1052,
              1966,
              1050,
              1967,
              1041,
              1967,
              1321,
              2324,
              1329,
              2322,
              1325,
              2321,
              1333,
              2323,
              1329,
              2323,
              1318,
              2322,
              1331,
              2322,
              1331,
              2323,
              1323,
              2322,
              1327,
              2322,
              1332,
              2323,
              1325,
              2323,
              1325,
              2321,
              1320,
              2321,
              1328,
              2322,
              1324,
              2322,
              1323,
              2324,
              1327,
              2322,
              1327,
              2321,
              1327,
              2325,
              1327,
              2323,
              1329,
              2324,
              1328,
              2320,
              1331,
              2322,
              1328,
              2323,
              1325,
              2322,
              1325,
              2323,
              1327,
              2322,
              1323,
              2323,
              1323,
              2319,
              1604,
              2674,
              1605,
              2676,
              1603,
              2678,
              1609,
              2676,
              1607,
              2675,
              1598,
              2677,
              1607,
              2677,
              1603,
              2672,
              1603,
              2675,
              1608,
              2676,
              1607,
              2675,
              1602,
              2675,
              1603,
              2677,
              1612,
              2674,
              1604,
              2675,
              1607,
              2677,
              1604,
              2676,
              1605,
              2675,
              1609,
              2672,
              1610,
              2675,
              1607,
              2677,
              1601,
              2675,
              1607,
              2676,
              1604,
              2674,
              1605,
              2675,
              1605,
              2678,
              1606,
              2675,
              1608,
              2674,
              1608,
              2678,
              1605,
              2677,
              1884,
              3023,
              1879,
              3023,
              1885,
              3024,
              1882,
              3023,
              1883,
              3023,
              1887,
              3023,
              1883,
              3021,
              1889,
              3023,
              1888,
              3023,
              1887,
              3023,
              1882,
              3023,
              1888,
              3022,
              1888,
              3025,
              1889,
              3023,
              1888,
              3024,
              1887,
              3023,
              1890,
              3023,
              1885,
              3023,
              1883,
              3025,
              1884,
              3023,
              1894,
              3024,
              1887,
              3023,
              1887,
              3024,
              1885,
              3023,
              1889,
              3024,
              1887,
              3020,
              1888,
              3023,
              1893,
              3027,
              1887,
              3024,
              1887,
              3024
            ]
          },
          {
            "time": 1546558214628,
            "message": [
              "alert",
              "clamp 1X calibration panel 1",
              "ok"
            ],
            "label": "offset_detector_dark",
            "data_raw": [
              120,
              556,
              122,
              556,
              121,
              553,
              120,
              554,
              123,
              558,
              118,
              555,
              122,
              556,
              121,
              556,
              121,
              555,
              121,
              555,
              127,
              555,
              123,
              557,
              118,
              557,
              122,
              555,
              122,
              559,
              119,
              555,
              121,
              557,
              121,
              554,
              124,
              555,
              119,
              557,
              121,
              553,
              125,
              555,
              121,
              556,
              119,
              558,
              123,
              554,
              119,
              555,
              121,
              556,
              118,
              557,
              118,
              555,
              121,
              557,
              599,
              1899,
              600,
              1895,
              597,
              1897,
              595,
              1895,
              605,
              1895,
              596,
              1897,
              603,
              1899,
              599,
              1895,
              601,
              1896,
              600,
              1895,
              602,
              1899,
              595,
              1896,
              601,
              1893,
              601,
              1896,
              597,
              1897,
              605,
              1895,
              595,
              1895,
              601,
              1896,
              601,
              1899,
              594,
              1896,
              602,
              1894,
              602,
              1894,
              608,
              1893,
              606,
              1895,
              604,
              1898,
              593,
              1899,
              603,
              1898,
              599,
              1897,
              603,
              1897,
              600,
              1895,
              1186,
              3357,
              1190,
              3357,
              1188,
              3359,
              1189,
              3356,
              1184,
              3355,
              1191,
              3355,
              1188,
              3357,
              1191,
              3359,
              1185,
              3357,
              1186,
              3358,
              1185,
              3355,
              1181,
              3358,
              1186,
              3358,
              1182,
              3358,
              1185,
              3357,
              1188,
              3358,
              1191,
              3357,
              1187,
              3361,
              1188,
              3360,
              1184,
              3360,
              1188,
              3359,
              1185,
              3359,
              1189,
              3360,
              1184,
              3359,
              1188,
              3358,
              1189,
              3359,
              1183,
              3360,
              1189,
              3357,
              1188,
              3357,
              1185,
              3357,
              1772,
              4795,
              1775,
              4799,
              1775,
              4798,
              1782,
              4799,
              1777,
              4797,
              1775,
              4799,
              1779,
              4795,
              1773,
              4797,
              1776,
              4802,
              1781,
              4798,
              1774,
              4796,
              1783,
              4797,
              1779,
              4803,
              1778,
              4796,
              1776,
              4799,
              1773,
              4801,
              1779,
              4797,
              1783,
              4803,
              1777,
              4799,
              1775,
              4799,
              1773,
              4804,
              1775,
              4799,
              1783,
              4796,
              1779,
              4801,
              1780,
              4798,
              1787,
              4800,
              1776,
              4801,
              1777,
              4799,
              1781,
              4797,
              1779,
              4796,
              2367,
              6214,
              2368,
              6217,
              2370,
              6219,
              2359,
              6216,
              2362,
              6218,
              2362,
              6222,
              2361,
              6219,
              2367,
              6218,
              2364,
              6219,
              2365,
              6217,
              2370,
              6217,
              2362,
              6219,
              2363,
              6219,
              2368,
              6215,
              2367,
              6219,
              2363,
              6218,
              2364,
              6221,
              2369,
              6219,
              2367,
              6219,
              2370,
              6216,
              2366,
              6217,
              2372,
              6218,
              2362,
              6219,
              2366,
              6216,
              2364,
              6221,
              2365,
              6219,
              2371,
              6217,
              2367,
              6216,
              2367,
              6217,
              2360,
              6216,
              2955,
              7615,
              2951,
              7615,
              2955,
              7615,
              2958,
              7619,
              2949,
              7619,
              2950,
              7613,
              2956,
              7615,
              2954,
              7618,
              2953,
              7615,
              2954,
              7614,
              2954,
              7616,
              2955,
              7617,
              2958,
              7615,
              2952,
              7619,
              2954,
              7615,
              2958,
              7617,
              2958,
              7615,
              2949,
              7615,
              2954,
              7618,
              2951,
              7620,
              2951,
              7616,
              2955,
              7613,
              2957,
              7619,
              2956,
              7618,
              2948,
              7617,
              2955,
              7617,
              2953,
              7615,
              2947,
              7617,
              2956,
              7617,
              2951,
              7617,
              3533,
              8993,
              3542,
              8994,
              3537,
              8994,
              3537,
              8991,
              3538,
              8997,
              3535,
              8997,
              3536,
              8996,
              3537,
              8994,
              3532,
              8995,
              3536,
              8995,
              3533,
              8997,
              3528,
              8995,
              3535,
              9000,
              3539,
              8999,
              3545,
              8995,
              3539,
              8997,
              3535,
              8998,
              3534,
              8993,
              3533,
              8995,
              3538,
              8995,
              3534,
              8994,
              3541,
              8995,
              3538,
              8990,
              3536,
              8995,
              3539,
              8993,
              3540,
              8995,
              3536,
              8991,
              3537,
              8991,
              3534,
              8992,
              3539,
              8994,
              4114,
              10349,
              4114,
              10355,
              4115,
              10351,
              4113,
              10353,
              4120,
              10353,
              4113,
              10358,
              4116,
              10352,
              4121,
              10351,
              4113,
              10353,
              4115,
              10353,
              4120,
              10355,
              4114,
              10353,
              4112,
              10355,
              4117,
              10350,
              4116,
              10355,
              4116,
              10354,
              4117,
              10355,
              4113,
              10354,
              4116,
              10353,
              4115,
              10352,
              4118,
              10354,
              4119,
              10354,
              4123,
              10355,
              4114,
              10353,
              4110,
              10354,
              4119,
              10355,
              4113,
              10353,
              4115,
              10351,
              4117,
              10351,
              4120,
              10353,
              4690,
              11692,
              4693,
              11691,
              4690,
              11689,
              4694,
              11697,
              4686,
              11692,
              4689,
              11693,
              4690,
              11689,
              4687,
              11692,
              4691,
              11693,
              4686,
              11694,
              4685,
              11693,
              4686,
              11695,
              4685,
              11699,
              4685,
              11693,
              4686,
              11693,
              4690,
              11690,
              4690,
              11697,
              4694,
              11690,
              4688,
              11692,
              4693,
              11695,
              4690,
              11697,
              4689,
              11687,
              4689,
              11696,
              4687,
              11696,
              4687,
              11693,
              4689,
              11695,
              4687,
              11695,
              4685,
              11692,
              4690,
              11695,
              4683,
              11695
            ]
          }
        ],
        "data_raw": [

        ]
      }
    ]
  ],
  "app_os": "macOS 17.7.0",
  "app_name": "PhotosynQ",
  "app_version": "1.1.0",
  "app_device": "x64",
  "location": false,
  "time_offset": "America/New_York"
}
Dave in cap img 4615
Created by

David M. Kramer


Protocol connections:
1
Latest Update:
Jan 2019