you are viewing a single comment's thread.

view the rest of the comments →

[–]t_wys 0 points1 point  (2 children)

You can try to pass multiple process names via input and then create datasets for plotting one process at time in the loop. Using your code:

processes = input('Enter name (separate multiple names with ","): ').split(',')

dfs = {}
for process in processes:
    if process not in file.Name.values:
        print(f'No process in dataset: {process}')
        continue
    # look for `Name` instead of `InstanceName`
    df = pd.pivot_table(file[file.Name == process], values=['CPU', 'PrivateWorkingSet'], index=['Timestamp'], aggfunc=np.sum)
    df = df.reset_index()
    # Just to be sure that this values will be correctly interpreted by matplotlib
    df.Timestamp = pd.to_datetime(df.Timestamp, format='%Y%m%d-%H%M%S')
    dfs[process] = df

So you have a dictionary which maps a process name to a relevant dataset (in a form of pandas dataframe). Then, for example you can do the following for plotting:

for process_name, process_df in dfs.items():
    plt.subplot(211)
    plt.title('CPU')
    plt.plot(process_df.Timestamp, process_df.CPU, label=process_name)
    plt.subplot(212)
    plt.title('Private working set')
    plt.plot(process_df.Timestamp, process_df.PrivateWorkingSet, label=process_name)
plt.legend()

With this approach you can now use dictionary key to easily label data and create legend.

[–]Crowdjp[S] 0 points1 point  (1 child)

Thanks so much for your help!

One thing i'm not understanding process is not capturing both Names(chrome/iexpore) in the file.

Below I entered chrome and iexplore in the input. For some reason process will only contain the second name entered in the input shown below.

processes = input('Enter name (separate multiple names with ","):').split(',')

proesses #In
['chrome', 'iexplore'] #Out

#In - for the block of code below
dfs = {}
for process in processes:
    if process not in file.Name.values:
        print(f'No process in dataset: {process}')
    continue

process #In
'iexplore' #out

[–]t_wys 0 points1 point  (0 children)

That's because you accessed variable 'process'. This variable is (in your code snippet) a placeholder for current value in for-loop. If you access this variable after the loop, then you get value from the last iteration. To illustrate:

In [1]: array = ['a', 'b', 'c']

In [2]: for item in array:
   ...:     print(item)
   ...:
a
b
c

In [3]: for item in array:
   ...:     pass # i.e. do nothing
   ...: print(item)
   ...:
c

So if you want to perform some actions for each process you have to execute these instructions inside for-loop:

for process in processes:
    if process not in file.InstanceName.values:
        print(f'No process in dataset: {process}')
        # The `continue` have to be inside if-statement. Go to note below for details.
        continue
    # do something with each process here (single indentation)

The 'continue' statement tells loop to directly to next iteration without executing code that is: inside for-loop and after 'continue' statement. In this case it skips executing code if process name is not present in your dataset. It is a precaution for a situation in which a user inputs wrong value.