Search code examples
pythonpseudocode

Understanding pseudo-code for Python implementation


I was given some pseudo-code written by someone who is no longer around to question. I was told I need to implement it in Python. At this point I am not looking for someone to write the code in Python for me, but I am trying to understand the pseudo-code so that I can write it in Python.

statsOut = open('/shared/succedentsupercedent_stats','w')
supercedents = tuple(open('/path', 'r'))
succedents = tuple(open('ABCD_.raw', 'r'))

for ( cols = 1 to succedents.columns , colg = 1 to supercedents.columns )

  /**** load all users for one succedent/supercedent into an array for future stats
  xy_array = new array ( supercedents.rows , 2 )
  for ( user = 1 to supercedents.rows )
    if succedents[user,cols].isValidDouble and supercedents[user,colg].isValidDouble
      then do
        xy_array [ user , 1 ] = succedents[user,cols].toDouble
        xy_array [ user , 2 ] = supercedents[user,colg].toDouble
      enddo
    endif
  endfor

  /*** do some stats with one succedent:supercedent key for all users
    sumx = sum( of xy_array[1:supercedents.rows][1]
    sumy = sum( of xy_array[1:supercedents.rows][2]

  /*** output succedent,supercedent,sumx,sumy
    append(statsOut,succedent,supercedent,sumx,sumy)
endfor        

If anyone can help shed some light on interpret what this is doing would be hugely helpful to me.


Solution

  • Here is a guess as to what is required by the pseudo-code. It is not entirely clear what is required, and a look at the actual data being read and a sample of what is to be produced would be very helpful. Without sample input and output, it is difficult to figure out what the code is supposed to accomplish.

    def main():
        succedents = load_succedents('ABCD_reddob_cases.raw')
        supercedents = load_supercedents('/shared/voom_e_plink_cases')
        with open('/shared/succedentsupercedent_stats', 'w') as stats_out:
            for cols in range(len(succedents[0])):
                for colg in range(len(supercedents[0])):
                    xy_array = [[0] * 2 for _ in range(len(supercedents))]
                    for user in range(len(supercedents)):
                        s1, s2 = succedents[user][cols], supercedents[user][colg]
                        if is_valid_double(s1) and is_valid_double(s2):
                            xy_array[user] = to_double(s1), to_double(d2)
                    sumx = sum(row[0] for row in xy_array)
                    sumy = sum(row[1] for row in xy_array)
                    append(stats_out, cols, colg, sumx, sumy)
    
    def load_supercedents(path):
        with open(path) as file:
            # perform whatever you need to do for loading the supercedents
            # some sort of data conversion may need to take place here
            supercedents = tuple(file)
        return supercedents
    
    def load_succedents(path):
        with open(path) as file:
            # perform whatever you need to do for loading the succedents
            # some sort of data conversion may need to take place here
            succedents = tuple(file)
        return succedents
    
    def is_valid_double(data):
        # determine if the data is a valid double or not
        return True or False
    
    def to_double(data):
        # convert the data into whatever a double happens to be
        return data
    
    def append(file, s, g, x, y):
        # write the data to your file
        print('cols = {s}, colg = {g}, sumx = {x}, sumy = {y}'.format(**locals()),
              file=file, flush=True)
    
    if __name__ == '__main__':
        main()