I have such function, that calculate forced vital capacity by checking the last positive number before negative in dataset sequence:
def FVC(self, data):
count = 0
for i in range(len(data)):
# checking if there's enough negative values to tell that inspiration started
if (data[i][1] > 1) and (data[i][2] < 0):
count += 1
elif count > 110:
return data[i-115][1]
else:
count = 0
The piece of sequence that I'm looking for might look like this (the answer-string here is [8340.0, 3.0616, 0.0]
):
[8280.0, 3.0611, 0.01]
[8290.0, 3.0612, 0.01]
[8300.0, 3.0613, 0.01]
[8310.0, 3.0614, 0.01]
[8320.0, 3.0615, 0.01]
[8330.0, 3.0616, 0.01]
[8340.0, 3.0616, 0.0]
[8350.0, 3.0615, -0.01125]
[8360.0, 3.0614, -0.01125]
[8370.0, 3.0613, -0.01125]
[8380.0, 3.0611, -0.01625]
[8390.0, 3.0609, -0.0275]
[8400.0, 3.0605, -0.033125]
[8410.0, 3.0601, -0.044375]
[8420.0, 3.0595, -0.055625]
[8430.0, 3.0589, -0.06625]
[8440.0, 3.0582, -0.06625]
[8450.0, 3.0575, -0.06625]
[8460.0, 3.0568, -0.0775]
[8470.0, 3.056, -0.0775]
[8480.0, 3.0551, -0.08875]
[8490.0, 3.0541, -0.105]
[8500.0, 3.0527, -0.1325]
[8510.0, 3.051, -0.17125]
[8520.0, 3.0489, -0.21]
[8530.0, 3.0465, -0.24375]
[8540.0, 3.0437, -0.2825]
[8550.0, 3.0406, -0.31]
[8560.0, 3.0371, -0.34312]
[8570.0, 3.0334, -0.37062]
[8580.0, 3.0294, -0.39875]
[8590.0, 3.0251, -0.43125]
[8600.0, 3.0204, -0.47625]
[8610.0, 3.0151, -0.52625]
[8620.0, 3.0091, -0.5975]
But also there might be some errors like this:
[6860.0, 3.0468, 0.0]
[6870.0, 3.0467, -0.005625]
[6880.0, 3.0466, -0.01125]
[6890.0, 3.0465, -0.01125]
[6900.0, 3.0464, -0.01125]
[6910.0, 3.0463, -0.01125]
[6920.0, 3.0461, -0.01125]
[6930.0, 3.046, -0.01125]
[6940.0, 3.0459, -0.01125]
[6950.0, 3.0457, -0.021875]
[6960.0, 3.0455, -0.021875]
[6970.0, 3.0452, -0.0275]
[6980.0, 3.0449, -0.033125]
[6990.0, 3.0445, -0.033125]
[7000.0, 3.0443, -0.021875]
[7010.0, 3.0441, -0.021875]
[7020.0, 3.0439, -0.021875]
[7030.0, 3.0437, -0.021875]
[7040.0, 3.0435, -0.021875]
[7050.0, 3.0433, -0.01125]
[7060.0, 3.0433, -0.005625]
[7070.0, 3.0433, 0.0]
[7080.0, 3.0433, 0.0]
[7090.0, 3.0433, 0.0]
[7100.0, 3.0433, 0.0]
[7110.0, 3.0433, 0.0]
[7120.0, 3.0433, 0.0]
[7130.0, 3.0433, 0.0]
[7140.0, 3.0433, 0.0]
[7150.0, 3.0433, 0.0]
[7160.0, 3.0434, 0.01]
[7170.0, 3.0435, 0.01]
As you can see, the only solution for that cases that I found is just to check if there is 100 negative numbers after sought-for value (just because 100 is pretty big number). So my question is how to replace constant 100 with some non-constant value, so my function will be more adaptive?
My recommendation would be to simply supply an additional parameter with a default argument. This allows you to run it as intended, but allows the flexibility to adjust if necessary. Something along the lines of --
def FVC(self, data, max_data_size: int = 100):
count = 0
for i in range(len(data)):
# checking if there's enough negative values to tell that inspiration started
if (data[i][1] > 1) and (data[i][2] < 0):
count += 1
elif count > max_data_size:
return data[i - max_data_size][1]
else:
count = 0