Skip to content

Commit

Permalink
fix: continue data fix
Browse files Browse the repository at this point in the history
  • Loading branch information
terryyz committed May 2, 2024
1 parent 237cd18 commit 0d684e5
Show file tree
Hide file tree
Showing 23 changed files with 833 additions and 817 deletions.
70 changes: 32 additions & 38 deletions data/clean/f_412_jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
def f_412(data):
"""
Calculate statistical measurements (mean and standard deviation) of the values associated with
each key in a list of dictionaries and visualize them with bar charts.
each key in a list of dictionaries, and visualize mean and standard deviation with bar charts.
Parameters:
data (list): The list of dictionaries. Must not be empty. Each dictionary must have numeric values.
The function raises ValueError if the input list is empty and TypeError if the input is not a
list of dictionaries or contains non-numeric values.
Returns:
tuple:
Expand All @@ -23,6 +21,10 @@ def f_412(data):
- matplotlib.pyplot
- collections.defaultdict
Raises:
- ValueError: If the input data is empty.
- TypeError: If the input is not a list of dictionaries or if any value in the dictionaries is not numeric.
Example:
>>> stats, axes = f_412([{'cat': 1, 'dog': 3}, {'cat' : 2, 'dog': 5}, {'cat' : 3, 'dog': 7}])
>>> stats
Expand Down Expand Up @@ -65,13 +67,12 @@ def test_case_1(self):
# Test basic case
data = [{"cat": 1, "dog": 3}, {"cat": 2, "dog": 5}, {"cat": 3, "dog": 7}]
stats, axes = f_412(data)
self.assertEqual(
stats,
{
"cat": {"mean": 2.0, "std": 0.816496580927726},
"dog": {"mean": 5.0, "std": 1.632993161855452},
},
)

self.assertAlmostEqual(stats["cat"]["mean"], 2.0)
self.assertAlmostEqual(stats["cat"]["std"], 0.816496580927726)
self.assertAlmostEqual(stats["dog"]["mean"], 5.0)
self.assertAlmostEqual(stats["dog"]["std"], 1.632993161855452)

self.assertEqual(axes[0].get_title(), "Statistics of cat")
self.assertEqual(axes[1].get_title(), "Statistics of dog")
for ax, key in zip(axes, stats):
Expand All @@ -82,13 +83,11 @@ def test_case_2(self):
# Test other keys (animals)
data = [{"bird": 5, "fish": 10}, {"bird": 6, "fish": 8}, {"bird": 7, "fish": 9}]
stats, axes = f_412(data)
self.assertEqual(
stats,
{
"bird": {"mean": 6.0, "std": 0.816496580927726},
"fish": {"mean": 9.0, "std": 0.816496580927726},
},
)

self.assertAlmostEqual(stats["bird"]["mean"], 6.0)
self.assertAlmostEqual(stats["bird"]["std"], 0.816496580927726)
self.assertAlmostEqual(stats["fish"]["mean"], 9.0)
self.assertAlmostEqual(stats["fish"]["std"], 0.816496580927726)
self.assertEqual(axes[0].get_title(), "Statistics of bird")
self.assertEqual(axes[1].get_title(), "Statistics of fish")
for ax, key in zip(axes, stats):
Expand All @@ -99,13 +98,12 @@ def test_case_3(self):
# Test handling negatives
data = [{"cat": -1, "dog": -3}, {"cat": -2, "dog": -5}, {"cat": -3, "dog": -7}]
stats, axes = f_412(data)
self.assertEqual(
stats,
{
"cat": {"mean": -2.0, "std": 0.816496580927726},
"dog": {"mean": -5.0, "std": 1.632993161855452},
},
)

self.assertAlmostEqual(stats["cat"]["mean"], -2.0)
self.assertAlmostEqual(stats["cat"]["std"], 0.816496580927726)
self.assertAlmostEqual(stats["dog"]["mean"], -5.0)
self.assertAlmostEqual(stats["dog"]["std"], 1.632993161855452)

self.assertEqual(axes[0].get_title(), "Statistics of cat")
self.assertEqual(axes[1].get_title(), "Statistics of dog")
for ax, key in zip(axes, stats):
Expand Down Expand Up @@ -157,25 +155,21 @@ def test_case_8(self):
{"apple": -6, "banana": 8},
]
stats, _ = f_412(data)
self.assertEqual(
stats,
{
"apple": {"mean": -4.0, "std": 1.632993161855452},
"banana": {"mean": 6.0, "std": 1.632993161855452},
},
)

self.assertAlmostEqual(stats["apple"]["mean"], -4.0)
self.assertAlmostEqual(stats["apple"]["std"], 1.632993161855452)
self.assertAlmostEqual(stats["banana"]["mean"], 6.0)
self.assertAlmostEqual(stats["banana"]["std"], 1.632993161855452)

def test_case_9(self):
# Test with floating point numbers
data = [{"x": 0.5, "y": 1.5}, {"x": 2.5, "y": 3.5}, {"x": 4.5, "y": 5.5}]
stats, _ = f_412(data)
self.assertEqual(
stats,
{
"x": {"mean": 2.5, "std": 1.632993161855452},
"y": {"mean": 3.5, "std": 1.632993161855452},
},
)

self.assertAlmostEqual(stats["x"]["mean"], 2.5)
self.assertAlmostEqual(stats["x"]["std"], 1.632993161855452)
self.assertAlmostEqual(stats["y"]["mean"], 3.5)
self.assertAlmostEqual(stats["y"]["std"], 1.632993161855452)

def tearDown(self):
plt.close("all")
Expand Down
3 changes: 3 additions & 0 deletions data/clean/f_418_jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def f_418(df: pd.DataFrame) -> (Counter, plt.Axes):
- seaborn
- matplotlib.pyplot
Raises:
- ValueError: If the DataFrame is empty or if age is negative.
Example:
>>> df = pd.DataFrame({'name': ['Alice', 'Bob', 'Alice'], 'age': [25, 26, 25]})
>>> duplicates_counter, ax = f_418(df)
Expand Down
3 changes: 3 additions & 0 deletions data/clean/f_421_jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def f_421(db_path, table_name, num_entries, random_seed=None):
Returns:
int: The number of rows inserted.
Raises:
ValueError: If num_entries is negative.
Requirements:
- sqlite3
- numpy
Expand Down
5 changes: 4 additions & 1 deletion data/clean/f_424_jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def f_424(db_name, table_name):
"""
Plot the relationship between the first and second numerical columns of an SQLite3 table.
Plot the relationship between the first and second numerical columns of an SQLite3 table, after excluding 'id' column.
Parameters:
- db_name (str): The absolute path to the SQLite3 database.
Expand All @@ -13,6 +13,9 @@ def f_424(db_name, table_name):
Returns:
- matplotlib.axes._axes.Axes: Scatterplot with column name labeled on their respective axes.
Raises:
- ValueError: If the table has less than two numerical columns.
Requirements:
- sqlite3
- pandas
Expand Down
3 changes: 3 additions & 0 deletions data/clean/f_425_jenny.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def f_425(db_name, table_name, num_entries, random_seed=None):
Returns:
str: The absolute path of the SQLite3 database file.
Raises:
ValueError: If num_entries is negative.
Requirements:
- sqlite3
- random.choice
Expand Down
2 changes: 1 addition & 1 deletion data/clean/f_427_ming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def f_427(hex_keys=KEYS, seed=42):
seed (int, optional): A seed for the random number generator to ensure deterministic behavior.
Returns:
str: The MD5 hash of the randomly selected hexadecimal string converted to a float.
str: The MD5 hash of the floating-point number derived from the randomly selected hexadecimal string.
Raises:
ValueError: If contains invalid hexadecimal strings.
Expand Down
39 changes: 20 additions & 19 deletions data/clean/f_457_ming.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def f_457(hours, output_dir = output_dir):
with open(FILE_PATH, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)

if not os.path.exists(BACKUP_PATH):
os.makedirs(BACKUP_PATH)
shutil.copy(FILE_PATH, BACKUP_PATH)
Expand Down Expand Up @@ -100,24 +100,25 @@ def test_f_457_copies_to_backup_directory(self, mock_copy, mock_getcwd):
self.assertEqual(expected_backup_dir, actual_backup_dir,
"The backup directory path does not match the expected directory path.")

@patch('os.getcwd', return_value=output_dir)
@patch('os.makedirs')
@patch('os.path.exists', side_effect=lambda path: path in [FILE_PATH, BACKUP_PATH])
@patch('builtins.open', new_callable=mock_open, read_data="Time,Condition\n")
def test_f_457_writes_correct_header(self, mock_file_open, mock_exists, mock_makedirs, mock_getcwd):
"""Ensure f_457 writes the correct header to weather_data.csv."""
expected_header = "Time,Condition\n"
f_457(1)

# Check all calls to write to ensure the expected header was written
# Check all calls to write to ensure key components of the expected header were written
header_components = ["Time", "Condition"]
header_written = any(
all(component in call_args.args[0] for component in header_components)
for call_args in mock_file_open().write.call_args_list
)

self.assertTrue(header_written, "The expected header components were not written to the file.")
# @patch('os.makedirs')
# @patch('os.path.exists')
# @patch('builtins.open', new_callable=mock_open, read_data="Time,Condition\n")
# @patch('os.getcwd', return_value=output_dir)
# def test_f_457_writes_correct_header(self, mock_file_open, mock_exists, mock_makedirs, mock_getcwd):
# """Ensure f_457 writes the correct header to weather_data.csv."""
# # create backup directory
# expected_header = "Time,Condition\n"
# f_457(1)

# # Check all calls to write to ensure the expected header was written
# # Check all calls to write to ensure key components of the expected header were written
# header_components = ["Time", "Condition"]
# header_written = any(
# all(component in call_args.args[0] for component in header_components)
# for call_args in mock_file_open().write.call_args_list
# )

# self.assertTrue(header_written, "The expected header components were not written to the file.")


def test_backup_file_creation(self):
Expand Down
Loading

0 comments on commit 0d684e5

Please sign in to comment.