1

I am trying to write a code that takes a list flow_rate, changes it into a segmented list list_segmented of length segment_len. Then with that segmented list, I take each index and make it a list of data_segment.

I am getting stuck trying to figure out how to make each list_segmented[i] = data_segment. The last part of the code calls another function for data_segment in which I have previously written and can import it.

Appreciate your help.

def flow_rate_to_disorder_status(flow_rate,segment_len,interval,threshold):
    
    inlist = flow_rate[:]
    list_segmented = []
    disorder_status = []
    
    while inlist:
        list_segmented.append(inlist[0 : segment_len])
        inlist[0 : segment_len] = []
    for i in range(0, len(list_segmented)):
        data_segment = list_segmented[i]
            
    condition = sym.has_symptom(data_segment, interval, threshold)

    disorder_status.append(condition)

Initial function:


def has_symptom(data_segment,interval,threshold): 
    
    max_ratio = 1           # maximum ratio allowed when dividing 
                            # data points in interval by len data_segment
                            # for our example it is 1
                            # NOTE: max_ratio can NOT be less than threshold 
    
    # to define the range of the given interval: 
    min_interval = interval[0]
    max_interval = interval[1]
    
    # create an empty list to add to data points that fall in the interval 
    symptom_yes = []
    
    # create a loop function to read every point in data_segment 
    # and compare wether or not it falls in the interval 
    for i in range(0, len(data_segment)):
        if min_interval <= data_segment[i] <= max_interval:
            
            # if the data falls in interval, add it to list symptom_yes
            symptom_yes.append(data_segment[i])
            
    # to get the fraction ration between interval points and total data points
    fraction_ratio = len(symptom_yes) / len(data_segment)
            
    # if the ratio of data points that fall in interval to total points in 
    # data segments is more than or equal to threshold and less than or equal 
    # to max_ratio (1 in our case) then apply condition
    
    if threshold <= fraction_ratio <= max_ratio:
      
        condition = True        # entire segment has the symptom
        
    else:
     
        condition = False       # entire segment does NOT have the symptom
       
            
    return condition

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Pete
  • 11
  • 4

2 Answers2

0

You nearly did it:

for i in range(0, len(data_segment)):   # <-- looping thru data_segment
    # data_segment = list_segmented[i]   <--  this was back to front.
    list_segmented[i] = data_segment   # <--  this will work

note: there are cleaner ways of doing this in python (like list comprehension).

Anyway, good question. Hope that helps.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • This will error because `data_segment` is undefined. I think OP was trying to use this loop to define it. – Anonymous12358 Jul 10 '22 at 09:04
  • @Anonymous12358: you are correct on the `undefined` part, but the OP has written this in the question: "I am getting stuck trying to figure out how to make each list_segmented[i] = data_segment". Which is what I answer. Maybe the OP can clarify. – D.L Jul 10 '22 at 10:24
0

It looks like the lines

condition = sym.has_symptom(data_segment, interval, threshold)
disorder_status.append(condition)

should each be indented by one more level to be inside the for loop, so that they are executed for each data segment.

You presumably also want to return disorder_status at the end of the function.

Anonymous12358
  • 436
  • 1
  • 10