Python Forum
dictionary construction help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary construction help
#1
I'm trying to assign my values to my keys. The issue is the 2D array, it is only reading in by rows, and I need it to by columns. I want to be able to use the special methods class i created as well.

All help is appreciated. I'm still new to coding, only my second week of it.

import numpy as np
import collections.abc
#import matplotlib.pyplot as plt


class KeyRead:
    '''
    This class reads in the (rcfg) file type and then converts everything to
    strings. These strings are then filter into a list to be used for keys.
    '''
    key_filename = "R033BDAS.rcfg"
    
    def key_data(self):
        '''
        This method reads in the binary file and places the key strings in a list.
        '''
        with open(self.key_filename, 'r') as file:
            for header_row in range(4): file.readline() # Skips header lines
            my_list = []
            for line in file:
                tokens = line.strip().split('\t') # Removes white spaces and splits into tokens at tabs
                my_list.append(tokens[0])
            return my_list
            
    
class DataRead:
    '''
    This class reads in the engineering unit data (eud) files, and places the data
    into a numpy array. The array will be sliced and indexed into the values
    for the dictionary keys.
    '''
    data_filename = "R033P010BDAS.eud"
        
    def read_data(self):
        '''
        This method reads in the data binary file and stores values in a 
        numpy array.
        '''
        return np.fromfile(self.data_filename, dtype = np.float32, count = -1, sep = "")
    
    def data_2d(self):
        '''
        The method creates a two dimensional array from the imported single array
        The data is in the form of 32768 rows and 1233 columns. The data will
        be sliced into columns and placed in their corresponding key values.
        '''
        return self.read_data().reshape(32768,1233)
    
class Dictionary(collections.abc.Mapping, DataRead):
    '''
    This class inherits from DataRead. The purpose of the class is
    to set up the iteration through the two dimensional data array which will
    be stored into the dictionary as key's values. 
    '''  
    def __getitem__(self, key):
        '''
        This method will expect the array of values, and a corresponding key to
        return values associated with the key.
        '''
        return self[key]
    
    def __len__(self):
        '''
        This method returns the length of the array columns.
        '''
        return 32768 # Number of rows 
    
    def __iter__(self):
        '''
        This method allows the array's columns to be iterated through.
        '''
        return (self[0:,i] for i in range(self.__len__()))
        
        
               
if __name__ == '__main__':
    
    fname1 = KeyRead()
    fname2 = DataRead()
    
    index_key = fname1.key_data()
    data_array_2d = fname2.data_2d()   
    dictionary = {k:v for k,v in zip(index_key, data_array_2d.__iter__())}
    
    print(dictionary) # Prints out dictionary, currently the values are reading wrong. (row instead of column)
Reply
#2
Please provide some sample (not the whole file) data
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  construction of Neural Network for solving Differential equations arshad 0 1,615 Jun-04-2020, 09:20 AM
Last Post: arshad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020