Skip to content

Conversation

abuzarmahmood
Copy link
Member

Co-authored-by: aider (gpt-4o) aider@aider.chat

…e repetition

Co-authored-by: aider (gpt-4o) <aider@aider.chat>
@abuzarmahmood
Copy link
Member Author

Generated edit command:

Summary of User's Issues and Requests

The user has requested a refactor of the read_file.py script in the blech_clust repository. The main goals are to encapsulate standalone functions into a class structure and minimize code repetition. The last comment [ develop_issue ] suggests a focus on development issues, which aligns with the refactoring task.

Overview of Plan to Address the Issues

  1. Encapsulate Standalone Functions into a Class:

    • Create a new class, IntanDataHandler, to encapsulate the standalone functions related to reading and processing Intan format data.
  2. Abstract Common Patterns into Helper Methods:

    • Identify repeated code segments, such as file reading and data processing, and abstract them into helper methods within the IntanDataHandler class.
  3. Ensure Code Functionality:

    • Ensure that the refactored code maintains the same functionality and output as the original.

Specific Details of Changes to be Made

  1. Encapsulate Standalone Functions into a Class

    • File: /home/exouser/Desktop/blech_github_bot/repos/katzlabbrandeis/blech_clust/utils/read_file.py
    • Lines: 238-387
    • Description: Create a new class, IntanDataHandler, to encapsulate the standalone functions. This class will manage the reading and processing of Intan format data.
    • Code Snippet:
      class IntanDataHandler:
          def __init__(self, hdf5_name, electrode_layout_frame):
              self.hdf5_name = hdf5_name
              self.electrode_layout_frame = electrode_layout_frame
      
          def read_traditional_intan(self, file_list):
              # Existing code from read_traditional_intan function
              ...
      
          def read_emg_channels(self):
              # Existing code from read_emg_channels function
              ...
      
          def read_electrode_channels(self):
              # Existing code from read_electrode_channels function
              ...
      
          def read_electrode_emg_channels_single_file(self, electrodes_list, num_recorded_samples, emg_channels):
              # Existing code from read_electrode_emg_channels_single_file function
              ...
  2. Abstract Common Patterns into Helper Methods

    • File: /home/exouser/Desktop/blech_github_bot/repos/katzlabbrandeis/blech_clust/utils/read_file.py
    • Lines: Various
    • Description: Identify repeated code segments, such as file reading and data processing, and abstract them into helper methods within the IntanDataHandler class.
    • Code Snippet:
      class IntanDataHandler:
          ...
      
          def _read_data_from_file(self, filename):
              return np.fromfile(filename, dtype=np.dtype('int16'))
      
          def _create_earray(self, group, array_name, atom):
              hf5 = tables.open_file(self.hdf5_name, 'r+')
              if os.path.join(group, array_name) not in hf5:
                  return hf5.create_earray(group, array_name, atom, (0,))
              else:
                  return hf5.get_node(group, array_name)
      
          # Use these helper methods in the existing functions
          ...

By implementing these changes, the code will be more organized, maintainable, and reduce repetition, making it easier to extend functionality in the future.


This response was automatically generated by blech_bot using model gpt-4o

Aider output:

View Aider Output
Aider v0.85.2
Main model: gpt-4o with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 116 files
Repo-map: using 4096 tokens, auto refresh


utils/read_file.py
To refactor the read_file.py script, we will encapsulate the standalone functions into a new class IntanDataHandler and abstract common patterns into
helper methods. Here are the changes:                                                                                                                

 1 Create the IntanDataHandler class to encapsulate the functions.                                                                                   
 2 Abstract common file reading and earray creation into helper methods.                                                                             

Let's implement these changes:                                                                                                                       

utils/read_file.py                                                                                                                                   

                                                                                                                                                     
<<<<<<< SEARCH                                                                                                                                       
def read_traditional_intan(                                                                                                                          
    hdf5_name,                                                                                                                                       
    file_list,                                                                                                                                       
    electrode_layout_frame,                                                                                                                          
):                                                                                                                                                   
    """                                                                                                                                              
    Reads traditional intan format data and saves to hdf5                                                                                            
                                                                                                                                                     
    Input:                                                                                                                                           
            hdf5_name: str                                                                                                                           
                    Name of hdf5 file to save data to                                                                                                
            file_list: list                                                                                                                          
                    List of file names to read                                                                                                       
            electrode_layout_frame: pandas.DataFrame                                                                                                 
                    Dataframe containing details of electrode layout                                                                                 
                                                                                                                                                     
    Writes:                                                                                                                                          
            hdf5 file with raw and raw_emg data                                                                                                      
            - raw: amplifier data                                                                                                                    
            - raw_emg: EMG data                                                                                                                      
    """                                                                                                                                              
    atom = tables.IntAtom()                                                                                                                          
    # Read EMG data from amplifier channels                                                                                                          
    # hdf5_path = os.path.join(dir_name, hdf5_name)                                                                                                  
    hf5 = tables.open_file(hdf5_name, 'r+')                                                                                                          
                                                                                                                                                     
    pbar = tqdm(total=len(file_list))                                                                                                                
    for this_file in file_list:                                                                                                                      
        # Update progress bar with file name                                                                                                         
        pbar.set_description(os.path.basename(this_file))                                                                                            
        # this_file_data = read_data(this_file)                                                                                                      
        this_file_data, data_present = load_file(this_file)                                                                                          
        # Get channel details                                                                                                                        
        # For each anplifier channel, read data and save to hdf5                                                                                     
        for i, this_amp in enumerate(this_file_data['amplifier_data']):                                                                              
            # If the amplifier channel is an EMG channel, save to raw_emg                                                                            
            # Otherwise, save to raw                                                                                                                 
            if 'emg' in electrode_layout_frame.loc[i].CAR_group.lower():                                                                             
                array_name = f'emg{i:02}'                                                                                                            
                if os.path.join('/raw_emg', array_name) not in hf5:                                                                                  
                    hf5_el_array = hf5.create_earray(                                                                                                
                        '/raw_emg', array_name, atom, (0,))                                                                                          
                else:                                                                                                                                
                    hf5_el_array = hf5.get_node('/raw_emg', array_name)                                                                              
                hf5_el_array.append(this_amp)                                                                                                        
            # Skip channels with no CAR group                                                                                                        
            elif electrode_layout_frame.loc[i].CAR_group.lower() in ['none', 'na']:                                                                  
                continue                                                                                                                             
            else:                                                                                                                                    
                array_name = f'electrode{i:02}'                                                                                                      
                if os.path.join('/raw', array_name) not in hf5:                                                                                      
                    hf5_el_array = hf5.create_earray(                                                                                                
                        '/raw', array_name, atom, (0,))                                                                                              
                else:                                                                                                                                
                    hf5_el_array = hf5.get_node('/raw', array_name)                                                                                  
                hf5_el_array.append(this_amp)                                                                                                        
            hf5.flush()                                                                                                                              
        pbar.update(1)                                                                                                                               
    pbar.close()                                                                                                                                     
    hf5.close()                                                                                                                                      
                                                                                                                                                     
                                                                                                                                                     
# TODO: Remove exec statements throughout file                                                                                                       
def read_emg_channels(hdf5_name, electrode_layout_frame):                                                                                            
    atom = tables.IntAtom()                                                                                                                          
    # Read EMG data from amplifier channels                                                                                                          
    hf5 = tables.open_file(hdf5_name, 'r+')                                                                                                          
    for num, row in tqdm(electrode_layout_frame.iterrows()):                                                                                         
        # Loading should use file name                                                                                                               
        # but writing should use channel ind so that channels from                                                                                   
        # multiple boards are written into a monotonic sequence                                                                                      
        if 'emg' in row.CAR_group.lower():                                                                                                           
            print(f'Reading : {row.filename, row.CAR_group}')                                                                                        
            port = row.port                                                                                                                          
            channel_ind = row.electrode_ind                                                                                                          
            data = np.fromfile(row.filename, dtype=np.dtype('int16'))                                                                                
            # Label raw_emg with electrode_ind so it's more easily identifiable                                                                      
            array_name = f'emg{channel_ind:02}'                                                                                                      
            hf5_el_array = hf5.create_earray(                                                                                                        
                '/raw_emg', array_name, atom, (0,))                                                                                                  
            hf5_el_array.append(data)                                                                                                                
            hf5.flush()                                                                                                                              
    hf5.close()                                                                                                                                      
                                                                                                                                                     
                                                                                                                                                     
def read_electrode_channels(hdf5_name, electrode_layout_frame):                                                                                      
    """                                                                                                                                              
    # Loading should use file name                                                                                                                   
    # but writing should use channel ind so that channels from                                                                                       
    # multiple boards are written into a monotonic sequence                                                                                          
    # Note: That channels inds may not be contiguous if there are                                                                                    
    # EMG channels in the middle                                                                                                                     
    """                                                                                                                                              
    atom = tables.IntAtom()                                                                                                                          
    # Read EMG data from amplifier channels                                                                                                          
    hf5 = tables.open_file(hdf5_name, 'r+')                                                                                                          
    for num, row in tqdm(electrode_layout_frame.iterrows()):                                                                                         
        emg_bool = 'emg' not in row.CAR_group.lower()                                                                                                
        none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                      
        if emg_bool and none_bool:                                                                                                                   
            print(f'Reading : {row.filename, row.CAR_group}')                                                                                        
            port = row.port                                                                                                                          
            channel_ind = row.electrode_ind                                                                                                          
            data = np.fromfile(row.filename, dtype=np.dtype('int16'))                                                                                
            # Label raw_emg with electrode_ind so it's more easily identifiable                                                                      
            array_name = f'electrode{channel_ind:02}'                                                                                                
            hf5_el_array = hf5.create_earray('/raw', array_name, atom, (0,))                                                                         
            hf5_el_array.append(data)                                                                                                                
            hf5.flush()                                                                                                                              
    hf5.close()                                                                                                                                      
                                                                                                                                                     
                                                                                                                                                     
def read_electrode_emg_channels_single_file(                                                                                                         
        hdf5_name,                                                                                                                                   
        electrode_layout_frame,                                                                                                                      
        electrodes_list,                                                                                                                             
        num_recorded_samples,                                                                                                                        
        emg_channels):                                                                                                                               
    # Read EMG data from amplifier channels                                                                                                          
    hf5 = tables.open_file(hdf5_name, 'r+')                                                                                                          
    atom = tables.IntAtom()                                                                                                                          
    amplifier_data = np.fromfile(electrodes_list[0], dtype=np.dtype('int16'))                                                                        
    num_electrodes = int(len(amplifier_data)/num_recorded_samples)                                                                                   
    amp_reshape = np.reshape(amplifier_data, (int(                                                                                                   
        len(amplifier_data)/num_electrodes), num_electrodes)).T                                                                                      
    for num, row in tqdm(electrode_layout_frame.iterrows()):                                                                                         
        # Loading should use file name                                                                                                               
        # but writing should use channel ind so that channels from                                                                                   
        # multiple boards are written into a monotonic sequence                                                                                      
        emg_bool = 'emg' not in row.CAR_group.lower()                                                                                                
        none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                      
        if emg_bool and none_bool:                                                                                                                   
            print(f'Reading : {row.filename, row.CAR_group}')                                                                                        
            port = row.port                                                                                                                          
            channel_ind = row.electrode_ind                                                                                                          
# el = hf5.create_earray('/raw_emg', f'emg{emg_counter:02}', atom, (0,))                                                                             
# Label raw_emg with electrode_ind so it's more easily identifiable                                                                                  
            el = hf5.create_earray(                                                                                                                  
                '/raw', f'electrode{channel_ind:02}', atom, (0,))                                                                                    
            exec(                                                                                                                                    
                f"hf5.root.raw.electrode{channel_ind:02}.append(amp_reshape[num,:])")                                                                
            hf5.flush()                                                                                                                              
        elif not (emg_bool) and none_bool:                                                                                                           
            port = row.port                                                                                                                          
            channel_ind = row.electrode_ind                                                                                                          
            el = hf5.create_earray(                                                                                                                  
                '/raw_emg', f'emg{channel_ind:02}', atom, (0,))                                                                                      
            exec(                                                                                                                                    
                f"hf5.root.raw_emg.emg{channel_ind:02}.append(amp_reshape[num,:])")                                                                  
    hf5.close()                                                                                                                                      
=======                                                                                                                                              
class IntanDataHandler:                                                                                                                              
    def __init__(self, hdf5_name, electrode_layout_frame):                                                                                           
        self.hdf5_name = hdf5_name                                                                                                                   
        self.electrode_layout_frame = electrode_layout_frame                                                                                         
                                                                                                                                                     
    def _read_data_from_file(self, filename):                                                                                                        
        return np.fromfile(filename, dtype=np.dtype('int16'))                                                                                        
                                                                                                                                                     
    def _create_earray(self, group, array_name, atom):                                                                                               
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        if os.path.join(group, array_name) not in hf5:                                                                                               
            return hf5.create_earray(group, array_name, atom, (0,))                                                                                  
        else:                                                                                                                                        
            return hf5.get_node(group, array_name)                                                                                                   
                                                                                                                                                     
    def read_traditional_intan(self, file_list):                                                                                                     
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
                                                                                                                                                     
        pbar = tqdm(total=len(file_list))                                                                                                            
        for this_file in file_list:                                                                                                                  
            pbar.set_description(os.path.basename(this_file))                                                                                        
            this_file_data, data_present = load_file(this_file)                                                                                      
            for i, this_amp in enumerate(this_file_data['amplifier_data']):                                                                          
                if 'emg' in self.electrode_layout_frame.loc[i].CAR_group.lower():                                                                    
                    array_name = f'emg{i:02}'                                                                                                        
                    hf5_el_array = self._create_earray('/raw_emg', array_name, atom)                                                                 
                    hf5_el_array.append(this_amp)                                                                                                    
                elif self.electrode_layout_frame.loc[i].CAR_group.lower() not in ['none', 'na']:                                                     
                    array_name = f'electrode{i:02}'                                                                                                  
                    hf5_el_array = self._create_earray('/raw', array_name, atom)                                                                     
                    hf5_el_array.append(this_amp)                                                                                                    
                hf5.flush()                                                                                                                          
            pbar.update(1)                                                                                                                           
        pbar.close()                                                                                                                                 
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_emg_channels(self):                                                                                                                     
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            if 'emg' in row.CAR_group.lower():                                                                                                       
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                data = self._read_data_from_file(row.filename)                                                                                       
                array_name = f'emg{row.electrode_ind:02}'                                                                                            
                hf5_el_array = self._create_earray('/raw_emg', array_name, atom)                                                                     
                hf5_el_array.append(data)                                                                                                            
                hf5.flush()                                                                                                                          
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_electrode_channels(self):                                                                                                               
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            emg_bool = 'emg' not in row.CAR_group.lower()                                                                                            
            none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                  
            if emg_bool and none_bool:                                                                                                               
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                data = self._read_data_from_file(row.filename)                                                                                       
                array_name = f'electrode{row.electrode_ind:02}'                                                                                      
                hf5_el_array = self._create_earray('/raw', array_name, atom)                                                                         
                hf5_el_array.append(data)                                                                                                            
                hf5.flush()                                                                                                                          
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_electrode_emg_channels_single_file(self, electrodes_list, num_recorded_samples, emg_channels):                                          
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        atom = tables.IntAtom()                                                                                                                      
        amplifier_data = self._read_data_from_file(electrodes_list[0])                                                                               
        num_electrodes = int(len(amplifier_data) / num_recorded_samples)                                                                             
        amp_reshape = np.reshape(amplifier_data, (int(len(amplifier_data) / num_electrodes), num_electrodes)).T                                      
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            emg_bool = 'emg' not in row.CAR_group.lower()                                                                                            
            none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                  
            if emg_bool and none_bool:                                                                                                               
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                el = self._create_earray('/raw', f'electrode{row.electrode_ind:02}', atom)                                                           
                exec(f"hf5.root.raw.electrode{row.electrode_ind:02}.append(amp_reshape[num,:])")                                                     
                hf5.flush()                                                                                                                          
            elif not emg_bool and none_bool:                                                                                                         
                el = self._create_earray('/raw_emg', f'emg{row.electrode_ind:02}', atom)                                                             
                exec(f"hf5.root.raw_emg.emg{row.electrode_ind:02}.append(amp_reshape[num,:])")                                                       
        hf5.close()                                                                                                                                  
>>>>>>> REPLACE                                                                                                                                      
                                                                                                                                                     

This refactor introduces the IntanDataHandler class, encapsulating the functions and abstracting common operations into helper methods. This makes   
the code more organized and maintainable.                                                                                                            

Tokens: 11k sent, 2.7k received. Cost: $0.06 message, $0.06 session.
Applied edit to utils/read_file.py
Commit 38500c2 refactor: encapsulate functions into IntanDataHandler class and reduce repetition

@abuzarmahmood
Copy link
Member Author

Also update blech_clust.py to use the updated version of read_file.py
Refer to the following issue

Traceback (most recent call last):
  File "blech_clust.py", line 352, in <module>
    read_file.read_electrode_channels(hdf5_name, electrode_layout_frame)
AttributeError: module 'utils.read_file' has no attribute 'read_electrode_channels'

…usage

Co-authored-by: aider (gpt-4o) <aider@aider.chat>
@abuzarmahmood
Copy link
Member Author

Applied changes based on comment:

View Aider Output
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Aider v0.86.1
Main model: gpt-4o with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 116 files
Repo-map: using 4096 tokens, auto refresh


https://aider.chat/HISTORY.html#release-notes


blech_clust.py

utils/read_file.py
To address the issues and requests, we will refactor the read_file.py module to encapsulate the standalone functions into a class structure named    
IntanDataHandler. Then, we will update the blech_clust.py script to use this refactored version of read_file.py.                                     

Changes to read_file.py                                                                                                                              

 1 Encapsulate Functions into IntanDataHandler Class: We will move the functions read_traditional_intan, read_emg_channels, read_electrode_channels, 
   and read_electrode_emg_channels_single_file into the IntanDataHandler class.                                                                      

Here are the SEARCH/REPLACE blocks for read_file.py:                                                                                                 

utils/read_file.py                                                                                                                                   

                                                                                                                                                     
<<<<<<< SEARCH                                                                                                                                       
# Import stuff!                                                                                                                                      
=======                                                                                                                                              
>>>>>>> REPLACE                                                                                                                                      
                                                                                                                                                     

utils/read_file.py                                                                                                                                   

                                                                                                                                                     
<<<<<<< SEARCH                                                                                                                                       
# Code for loading traditional intan format from                                                                                                     
# https://github.com/Intan-Technologies/load-rhd-notebook-python                                                                                     
                                                                                                                                                     
from utils.importrhdutilities import load_file, read_header                                                                                          
                                                                                                                                                     
=======                                                                                                                                              
from utils.importrhdutilities import load_file, read_header                                                                                          
                                                                                                                                                     
class IntanDataHandler:                                                                                                                              
    def __init__(self, hdf5_name, electrode_layout_frame):                                                                                           
        self.hdf5_name = hdf5_name                                                                                                                   
        self.electrode_layout_frame = electrode_layout_frame                                                                                         
                                                                                                                                                     
    def _read_data_from_file(self, filename):                                                                                                        
        return np.fromfile(filename, dtype=np.dtype('int16'))                                                                                        
                                                                                                                                                     
    def _create_earray(self, group, array_name, atom):                                                                                               
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        if os.path.join(group, array_name) not in hf5:                                                                                               
            return hf5.create_earray(group, array_name, atom, (0,))                                                                                  
        else:                                                                                                                                        
            return hf5.get_node(group, array_name)                                                                                                   
                                                                                                                                                     
    def read_traditional_intan(self, file_list):                                                                                                     
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
                                                                                                                                                     
        pbar = tqdm(total=len(file_list))                                                                                                            
        for this_file in file_list:                                                                                                                  
            pbar.set_description(os.path.basename(this_file))                                                                                        
            this_file_data, data_present = load_file(this_file)                                                                                      
            for i, this_amp in enumerate(this_file_data['amplifier_data']):                                                                          
                if 'emg' in self.electrode_layout_frame.loc[i].CAR_group.lower():                                                                    
                    array_name = f'emg{i:02}'                                                                                                        
                    hf5_el_array = self._create_earray(                                                                                              
                        '/raw_emg', array_name, atom)                                                                                                
                    hf5_el_array.append(this_amp)                                                                                                    
                elif self.electrode_layout_frame.loc[i].CAR_group.lower() not in ['none', 'na']:                                                     
                    array_name = f'electrode{i:02}'                                                                                                  
                    hf5_el_array = self._create_earray(                                                                                              
                        '/raw', array_name, atom)                                                                                                    
                    hf5_el_array.append(this_amp)                                                                                                    
                hf5.flush()                                                                                                                          
            pbar.update(1)                                                                                                                           
        pbar.close()                                                                                                                                 
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_emg_channels(self):                                                                                                                     
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            if 'emg' in row.CAR_group.lower():                                                                                                       
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                data = self._read_data_from_file(row.filename)                                                                                       
                array_name = f'emg{row.electrode_ind:02}'                                                                                            
                hf5_el_array = self._create_earray(                                                                                                  
                    '/raw_emg', array_name, atom)                                                                                                    
                hf5_el_array.append(data)                                                                                                            
                hf5.flush()                                                                                                                          
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_electrode_channels(self):                                                                                                               
        atom = tables.IntAtom()                                                                                                                      
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            emg_bool = 'emg' not in row.CAR_group.lower()                                                                                            
            none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                  
            if emg_bool and none_bool:                                                                                                               
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                data = self._read_data_from_file(row.filename)                                                                                       
                array_name = f'electrode{row.electrode_ind:02}'                                                                                      
                hf5_el_array = self._create_earray('/raw', array_name, atom)                                                                         
                hf5_el_array.append(data)                                                                                                            
                hf5.flush()                                                                                                                          
        hf5.close()                                                                                                                                  
                                                                                                                                                     
    def read_electrode_emg_channels_single_file(self, electrodes_list, num_recorded_samples, emg_channels):                                          
        hf5 = tables.open_file(self.hdf5_name, 'r+')                                                                                                 
        atom = tables.IntAtom()                                                                                                                      
        amplifier_data = self._read_data_from_file(electrodes_list[0])                                                                               
        num_electrodes = int(len(amplifier_data) / num_recorded_samples)                                                                             
        amp_reshape = np.reshape(amplifier_data, (int(                                                                                               
            len(amplifier_data) / num_electrodes), num_electrodes)).T                                                                                
        for num, row in tqdm(self.electrode_layout_frame.iterrows()):                                                                                
            emg_bool = 'emg' not in row.CAR_group.lower()                                                                                            
            none_bool = row.CAR_group.lower() not in ['none', 'na']                                                                                  
            if emg_bool and none_bool:                                                                                                               
                print(f'Reading : {row.filename, row.CAR_group}')                                                                                    
                el = self._create_earray(                                                                                                            
                    '/raw', f'electrode{row.electrode_ind:02}', atom)                                                                                
                exec(                                                                                                                                
                    f"hf5.root.raw.electrode{row.electrode_ind:02}.append(amp_reshape[num,:])")                                                      
                hf5.flush()                                                                                                                          
            elif not emg_bool and none_bool:                                                                                                         
                el = self._create_earray(                                                                                                            
                    '/raw_emg', f'emg{row.electrode_ind:02}', atom)                                                                                  
                exec(                                                                                                                                
                    f"hf5.root.raw_emg.emg{row.electrode_ind:02}.append(amp_reshape[num,:])")                                                        
        hf5.close()                                                                                                                                  
>>>>>>> REPLACE                                                                                                                                      
                                                                                                                                                     

Changes to blech_clust.py                                                                                                                            

 1 Update to Use IntanDataHandler Class: We will update the script to use the IntanDataHandler class for reading electrode channels.                 

Here are the SEARCH/REPLACE blocks for blech_clust.py:                                                                                               

blech_clust.py                                                                                                                                       

                                                                                                                                                     
<<<<<<< SEARCH                                                                                                                                       
    if file_type == 'one file per channel':                                                                                                          
        # read_file.read_digins(hdf5_name, dig_in_int, dig_in_file_list)                                                                             
        read_file.read_electrode_channels(hdf5_name, electrode_layout_frame)                                                                         
        if len(emg_channels) > 0:                                                                                                                    
            read_file.read_emg_channels(hdf5_name, electrode_layout_frame)                                                                           
    elif file_type == 'one file per signal type':                                                                                                    
        # read_file.read_digins_single_file(hdf5_name, dig_in_int, dig_in_file_list)                                                                 
        # This next line takes care of both electrodes and emgs                                                                                      
        read_file.read_electrode_emg_channels_single_file(                                                                                           
            hdf5_name, electrode_layout_frame, electrodes_list, num_recorded_samples, emg_channels)                                                  
    elif file_type == 'traditional':                                                                                                                 
        read_file.read_traditional_intan(                                                                                                            
            hdf5_name,                                                                                                                               
            rhd_file_list,                                                                                                                           
            electrode_layout_frame,                                                                                                                  
        )                                                                                                                                            
=======                                                                                                                                              
    intan_handler = read_file.IntanDataHandler(hdf5_name, electrode_layout_frame)                                                                    
    if file_type == 'one file per channel':                                                                                                          
        intan_handler.read_electrode_channels()                                                                                                      
        if len(emg_channels) > 0:                                                                                                                    
            intan_handler.read_emg_channels()                                                                                                        
    elif file_type == 'one file per signal type':                                                                                                    
        intan_handler.read_electrode_emg_channels_single_file(                                                                                       
            electrodes_list, num_recorded_samples, emg_channels)                                                                                     
    elif file_type == 'traditional':                                                                                                                 
        intan_handler.read_traditional_intan(rhd_file_list)                                                                                          
>>>>>>> REPLACE                                                                                                                                      
                                                                                                                                                     

After applying these changes, the blech_clust.py script will correctly utilize the refactored read_file.py module, resolving the AttributeError and  
improving code organization.                                                                                                                         

Tokens: 15k sent, 1.8k received. Cost: $0.05 message, $0.05 session.
Applied edit to utils/read_file.py
Applied edit to blech_clust.py
Commit 99f7bfd refactor: encapsulate functions in IntanDataHandler class and update usage


This response was automatically generated by blech_bot using model gpt-4o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor read_file.py as a class and minimize repetition of code
1 participant