Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute can load (Sourcery refactored) #14

Open
wants to merge 1 commit into
base: compute_can_load
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions can.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@


class Can:
def convert_string(string: str) -> str:
if not isinstance(string, str):
def convert_string(self) -> str:
if not isinstance(self, str):
raise TypeError("`string` needs to be an integer type!")

string = string.upper()
string = string.replace(' ', '_')
string = normalize('NFKD', string)
string = string.encode('ASCII', 'ignore')
string = string.decode('ASCII')
string = re.sub('[^A-Z0-9_]+', '', string)
self = self.upper()
self = self.replace(' ', '_')
self = normalize('NFKD', self)
self = self.encode('ASCII', 'ignore')
self = self.decode('ASCII')
self = re.sub('[^A-Z0-9_]+', '', self)

return string
return self
Comment on lines -18 to +29
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.convert_string refactored with the following changes:


def validate_byte(byte: int) -> bool:
if (byte < 0) | (byte > 8):
def validate_byte(self) -> bool:
if (self < 0) | (self > 8):
Comment on lines -31 to +32
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.validate_byte refactored with the following changes:

print("Byte number MUST be between 0 and 7 to be described.")
return False
return True

def validate_bit(bit: int) -> bool:
if (bit < 0) | (bit > 8):
def validate_bit(self) -> bool:
if (self < 0) | (self > 8):
Comment on lines -37 to +38
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.validate_bit refactored with the following changes:

print("Bit number MUST be between 0 and 7 to be described.")
return False
return True
Expand Down Expand Up @@ -188,10 +188,7 @@ def get(self) -> dict:
}

def get_total_load(self, bitrate):
load = 0
for topic in self.topics:
load += topic.get_load(bitrate)
return load
return sum(topic.get_load(bitrate) for topic in self.topics)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.module.get_total_load refactored with the following changes:


def __str__(self) -> str:
return json.dumps(self.get(), indent=4)
Expand Down Expand Up @@ -253,7 +250,7 @@ def export_c_library(self, filename: str = "can_ids.h"):

def export_csv(self, filename: str):
import pandas as pd

Comment on lines -256 to +253
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the following improvement in Function Can.export_csv:

modules = []
signature = []
ids = []
Expand Down Expand Up @@ -284,15 +281,15 @@ def export_csv(self, filename: str):
"frame_length": frame_length,
"description": description,
})

df.to_csv(filename)


def get_can_load_by_topic(self):
load = {}
for module in self.modules:
for topic in module["topics"]:
if not topic["id"] in load.keys():
if topic["id"] not in load:
Comment on lines -295 to +292
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.get_can_load_by_topic refactored with the following changes:

load[topic["id"]] = []
load[topic["id"]].append(self.get_topic_load(topic))
return load
Expand Down Expand Up @@ -323,19 +320,14 @@ def plot_load(self):
ids = {}
for module in self.modules:
for topic in module["topics"]:
if not topic["id"] in ids.keys():
if topic["id"] not in ids:
ids[topic["id"]] = 0
ids[topic["id"]] += self.get_topic_load(topic)

id = list(ids.keys())
id.sort()

load = []
for i in id:
load.append(ids[i])

axes[0][0].bar(range(0, len(id)), load, align='center', color='royalblue')
axes[0][0].set_xticks(range(0, len(id)))
id = sorted(ids.keys())
load = [ids[i] for i in id]
axes[0][0].bar(range(len(id)), load, align='center', color='royalblue')
axes[0][0].set_xticks(range(len(id)))
Comment on lines -326 to +330
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Can.plot_load refactored with the following changes:

axes[0][0].set_xticklabels(list(id), fontsize = 9, rotation = 90)
axes[0][0].set_title("Load x Id")
axes[0][0].set_ylabel("Load [%]")
Expand All @@ -356,14 +348,14 @@ def plot_load(self):
modules = {}
for module in self.modules:
for topic in module["topics"]:
if not module["name"] in modules.keys():
if module["name"] not in modules:
modules[module["name"]] = 0
modules[module["name"]] += self.get_topic_load(topic)
load = list(modules.values())
modules = list(modules.keys())
axes[1][0].set_title("Load x Module")
axes[1][0].bar(range(0, len(modules)), load)
axes[1][0].set_xticks(range(0,len(modules)))
axes[1][0].bar(range(len(modules)), load)
axes[1][0].set_xticks(range(len(modules)))
axes[1][0].set_xticklabels(modules, fontsize = 9, rotation=90)
axes[1][0].set_ylabel("Load [%]")

Expand Down
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
can.export_c_library()
can.export_csv("can_ids.csv")
loads = can.get_can_load_by_topic()
ids = list(loads.keys())
ids.sort()
ids = sorted(loads.keys())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 11-12 refactored with the following changes:

for id in ids:
print("Id: ", id, "load:", loads[id])
print("Total load:", round(can.get_can_load(),3), "%")
Expand Down