1

So I have two dictionaries that store specific "channels" and their values, these channels update every second. My end goal is to create a tool/GUI that will help us determine how off or misaligned our channels are, they correspond to optical mount DOFs.

I currently have a function that grabs the current time and determines the channels value at that time and puts it in a dictionary and another function that lets you select a time and then does the same.

The part of this project I'm a bit lost on is how to compare these dictionaries.

I want to match the keys (which are my channels) and then find the difference between their two values and store that in a 3rd dictionary with the same channels.

So here's the basic idea in code for what I'm looking to do:

diff_channels_dict = {}
### After my functions that create these dictionaries run, I'll have something like the following, but a lot longer

ref_channels_dict = {

    'IM1_M1_P_OFFSET': 8,
    'IM1_M1_Y_OFFSET': 8,
    'IM2_M1_P_OFFSET': 8}
now_channels_dict = {

    'IM1_M1_P_OFFSET': 2,
    'IM1_M1_Y_OFFSET': 2,
    'IM2_M1_P_OFFSET': 2}


def some_func():

    for key in now_channels_dict and ref_channels_dict:
        diff = now_channels_dict[value] - ref_channels_dict[value]
        diff_channels_dict[key].append(diff)
    return

some_func()

### So then
diff_channels_dict = {

    'IM1_M1_P_OFFSET': -6,
    'IM1_M1_Y_OFFSET': -6,
    'IM2_M1_P_OFFSET': -6}

Can anyone give me some guidance on how to go about doing this? I looked into a few libraries, deepdiff, and Dictdiffer but I do not have access to them with the env that is used on all the machines that will use this tool.

ryancrouch
  • 13
  • 5
  • Welcome to Stack Overflow. It's hard to understand the problem description because, despite the variable names, *there are no `dict`s in your example code*. Anyway, it seems like you just want to subtract corresponding values from two dictionaries? *What exactly is the difficulty*? Do you know how to get a key/value pair from a dictionary? Do you know how to subtract two values? Do you know how to repeat a process for each entry in a dictionary? Do you know how to look at the matching entries from two dictionaries at the same time? If you put these things together, does it solve the problem? – Karl Knechtel Sep 20 '22 at 15:49
  • If there's already an external library that solves the problem, and you need your tool to be able to run in places that don't already have that library installed, that's just a packaging issue, which is generally going to be a lot easier to solve than reimplementing the external library. :) Look at e.g. https://stackoverflow.com/questions/11436777/is-there-a-way-to-embed-dependencies-within-a-python-script or https://docs.python-guide.org/shipping/freezing/#freezing-your-code-ref – Samwise Sep 20 '22 at 15:50

2 Answers2

0

I'm going to assume that the starting dictionaries are like this (written in python format):

ref_channels_dict = {
            'IM1_M1_P_OFFSET': 8,
            'IM1_M1_Y_OFFSET': 8,
            'IM2_M1_P_OFFSET': 8
            }
            
now_channels_dict = {
            'IM1_M1_P_OFFSET': 2,
            'IM1_M1_Y_OFFSET': 2,
            'IM2_M1_P_OFFSET': 2
            }

From this, we can write the function that compares and put the differences into a new dictionary:

def compare_dictionaries(ref_dict, now_dict):
    assert ref_dict.keys() == now_dict.keys() # check if the keys are the same

    diff_dict = {}

    for key in ref_dict.keys(): #iterate over the keys and fill the diff_dict with the differences
        # this can be done because we know that the keys in both dictionaries are the same
        diff_dict[key] = now_dict[key] - ref_dict[key]

    return diff_dict

After that, we can define the whole code as:


def compare_dictionaries(ref_dict, now_dict):
    assert ref_dict.keys() == now_dict.keys() # check if the keys are the same

    diff_dict = {}

    for key in ref_dict.keys(): #iterate over the keys and fill the diff_dict with the differences
        # this can be done because we know that the keys in both dictionaries are the same
        diff_dict[key] = now_dict[key] - ref_dict[key]

    return diff_dict





if __name__ == "__main__":
    ref_channels_dict = {
            'IM1_M1_P_OFFSET': 8,
            'IM1_M1_Y_OFFSET': 8,
            'IM2_M1_P_OFFSET': 8
            }

    now_channels_dict = {
            'IM1_M1_P_OFFSET': 2,
            'IM1_M1_Y_OFFSET': 2,
            'IM2_M1_P_OFFSET': 2
            }

    print(compare_dictionaries(ref_channels_dict, now_channels_dict))

Which outputs:

>>> {'IM1_M1_P_OFFSET': -6, 'IM1_M1_Y_OFFSET': -6, 'IM2_M1_P_OFFSET': -6}
Cuartero
  • 407
  • 1
  • 6
0

with pandas you can do it without looping, what could be quite useful for really large dataset:

import pandas as pd

diff_dict = (pd.Series(now_channels_dict) - pd.Series(ref_channels_dict)).to_dict()

print(diff_dict)  
'''
{'IM1_M1_P_OFFSET': -6, 'IM1_M1_Y_OFFSET': -6, 'IM2_M1_P_OFFSET': -6}
SergFSM
  • 1,419
  • 1
  • 4
  • 7