Skip to content

Commit

Permalink
Fixed some coding style and clean coding inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKHaliliT committed Aug 23, 2024
1 parent af0871b commit 09760fa
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 46 deletions.
4 changes: 1 addition & 3 deletions MobileViViT/assets/utils/low_resource_training_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def low_resource_training_scheme(model: tf.keras.Model,
# Initializing the total gradients to zero which will reset at the end of each epoch
total_gradients = [tf.zeros_like(var) for var in model.trainable_variables]

if verbose == 0:
pass
elif verbose == 1 or verbose == 2:
if verbose == 1 or verbose == 2:
print(f"Epoch {epoch + 1}:")

# Iterating over the training dataset
Expand Down
65 changes: 25 additions & 40 deletions MobileViViT/assets/utils/move_column_to_the_beginning.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pandas as pd
from move_column_to_the_beginning_assets._by_name import _by_name
from move_column_to_the_beginning_assets._by_index import _by_index


class MoveColumnToTheBeginning:
Expand All @@ -8,6 +10,27 @@ class MoveColumnToTheBeginning:
A class containing functions to move a column to the beginning of a Pandas DataFrame.
"""

def __init__(self) -> None:

"""
Constructor of the MoveColumnToTheBeginning class.
Parameters
----------
None.
Returns
-------
None.
"""

pass


@staticmethod
def move_column_to_the_beginning_by_name(dataframe: pd.DataFrame, column_name: str) -> pd.DataFrame:
Expand Down Expand Up @@ -42,26 +65,7 @@ def move_column_to_the_beginning_by_name(dataframe: pd.DataFrame, column_name: s
raise ValueError("column_name must be a column of the DataFrame")


if column_name == dataframe.columns[0]:

print("The column is already in the beginning, returning the original DataFrame.")


return dataframe

else:

# Get the column
column = dataframe[column_name]

# Remove the column from its current position
dataframe = dataframe.drop(columns=column_name)

# Add the column to the beginning
dataframe.insert(0, column_name, column)


return dataframe
return _by_name(dataframe, column_name)


@staticmethod
Expand Down Expand Up @@ -97,23 +101,4 @@ def move_column_to_the_beginning_by_index(dataframe: pd.DataFrame, column_index:
raise ValueError("column_index is out of range for DataFrame")


if column_index == 0:

print("The column is already in the beginning, returning the original DataFrame.")


return dataframe

else:

# Get the column
column = dataframe.iloc[:, column_index]

# Remove the column from its current position
dataframe = dataframe.drop(dataframe.columns[column_index], axis=1)

# Add the column to the beginning
dataframe.insert(0, dataframe.columns[column_index], column)


return dataframe
return _by_index(dataframe, column_index)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pandas as pd


def _by_index(dataframe: pd.DataFrame, column_index: int) -> pd.DataFrame:

"""
Moves a column to the beginning of a Pandas DataFrame
based on its index (0-based index).
Parameters
----------
dataframe : pandas.DataFrame
DataFrame containing the column to be moved.
column_index : int
Index of the column to be moved (0-based index).
Returns
-------
dataframe : pandas.DataFrame
DataFrame with the column moved to the beginning.
"""

if not isinstance(dataframe, pd.DataFrame):
raise TypeError("dataframe must be a DataFrame")
if not isinstance(column_index, int):
raise TypeError("column_index must be an integer")
if column_index < 0 or column_index >= len(dataframe.columns):
raise ValueError("column_index is out of range for DataFrame")


if column_index == 0:

print("The column is already in the beginning, returning the original DataFrame.")


return dataframe

else:

# Get the column
column = dataframe.iloc[:, column_index]

# Remove the column from its current position
dataframe = dataframe.drop(dataframe.columns[column_index], axis=1)

# Add the column to the beginning
dataframe.insert(0, dataframe.columns[column_index], column)


return dataframe
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pandas as pd


def _by_name(dataframe: pd.DataFrame, column_name: str) -> pd.DataFrame:

"""
Moves a column to the beginning of a Pandas DataFrame
based on its name.
Parameters
----------
dataframe : pandas.DataFrame
DataFrame containing the column to be moved.
column_name : str
Name of the column to be moved.
Returns
-------
dataframe : pandas.DataFrame
DataFrame with the column moved to the beginning.
"""

if not isinstance(dataframe, pd.DataFrame):
raise TypeError("dataframe must be a DataFrame")
if not isinstance(column_name, str):
raise TypeError("column_name must be a string")
if column_name not in dataframe.columns:
raise ValueError("column_name must be a column of the DataFrame")


if column_name == dataframe.columns[0]:

print("The column is already in the beginning, returning the original DataFrame.")


return dataframe

else:

# Get the column
column = dataframe[column_name]

# Remove the column from its current position
dataframe = dataframe.drop(columns=column_name)

# Add the column to the beginning
dataframe.insert(0, column_name, column)


return dataframe
3 changes: 2 additions & 1 deletion MobileViViT/mobilevivit_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(self, num_output_units) -> None:
Parameters
----------
None.
num_output_units : int
Number of output units.
Returns
Expand Down
3 changes: 2 additions & 1 deletion MobileViViT/mobilevivit_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(self, num_output_units) -> None:
Parameters
----------
None.
num_output_units : int
Number of output units.
Returns
Expand Down
3 changes: 2 additions & 1 deletion MobileViViT/mobilevivit_xxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(self, num_output_units) -> None:
Parameters
----------
None.
num_output_units : int
Number of output units.
Returns
Expand Down

0 comments on commit 09760fa

Please sign in to comment.