From ef032e0b4f6d8a149680c6148c74dfea5b3fa898 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 17:11:45 -0700 Subject: [PATCH 1/8] algo Signed-off-by: Saurabh Misra --- codeflash/after_algo.py | 9 +++++++++ codeflash/before_algo.py | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 codeflash/after_algo.py create mode 100644 codeflash/before_algo.py diff --git a/codeflash/after_algo.py b/codeflash/after_algo.py new file mode 100644 index 00000000..bf506b2c --- /dev/null +++ b/codeflash/after_algo.py @@ -0,0 +1,9 @@ +def postprocess(self, predictions: tuple[np.ndarray, ...], max_detections: int): + bboxes, logits = predictions + batch_size, num_queries, num_classes = logits.shape + logits_sigmoid = self.sigmoid_stable(logits) + for batch_idx in range(batch_size): + logits_flat = logits_sigmoid[batch_idx].reshape(-1) + # Use argpartition for better performance when max_detections is smaller than logits_flat + partition_indices = np.argpartition(-logits_flat, max_detections)[:max_detections] + sorted_indices = partition_indices[np.argsort(-logits_flat[partition_indices])] diff --git a/codeflash/before_algo.py b/codeflash/before_algo.py new file mode 100644 index 00000000..9baf6086 --- /dev/null +++ b/codeflash/before_algo.py @@ -0,0 +1,7 @@ +def postprocess(self, predictions: tuple[np.ndarray, ...], max_detections: int): + bboxes, logits = predictions + batch_size, num_queries, num_classes = logits.shape + logits_sigmoid = self.sigmoid_stable(logits) + for batch_idx in range(batch_size): + logits_flat = logits_sigmoid[batch_idx].reshape(-1) + sorted_indices = np.argsort(-logits_flat)[:max_detections] From cde5c39e2e94deeabc853e0a6a178a1523e516b3 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 17:23:32 -0700 Subject: [PATCH 2/8] web servers Signed-off-by: Saurabh Misra --- codeflash/after_web_server.py | 10 ++++++++++ codeflash/before_web_server.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 codeflash/after_web_server.py create mode 100644 codeflash/before_web_server.py diff --git a/codeflash/after_web_server.py b/codeflash/after_web_server.py new file mode 100644 index 00000000..8b6dea3e --- /dev/null +++ b/codeflash/after_web_server.py @@ -0,0 +1,10 @@ +async def get_endpoint(session, url): + async with session.get(url) as response: + return await response.text() + + +async def some_api_call(urls): + async with aiohttp.ClientSession() as session: + tasks = [get_endpoint(session, url) for url in urls] + results = await asyncio.gather(*tasks) + return results diff --git a/codeflash/before_web_server.py b/codeflash/before_web_server.py new file mode 100644 index 00000000..551fa58e --- /dev/null +++ b/codeflash/before_web_server.py @@ -0,0 +1,12 @@ +async def get_endpoint(session, url): + async with session.get(url) as response: + return await response.text() + + +async def some_api_call(urls): + async with aiohttp.ClientSession() as session: + results = [] + for url in urls: + result = await get_endpoint(session, url) + results.append(result) + return results From 558e975134452bfedbad54f71aff78cb2130f835 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 17:28:57 -0700 Subject: [PATCH 3/8] web servers Signed-off-by: Saurabh Misra --- codeflash/after_web_server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/codeflash/after_web_server.py b/codeflash/after_web_server.py index 8b6dea3e..8286fb58 100644 --- a/codeflash/after_web_server.py +++ b/codeflash/after_web_server.py @@ -6,5 +6,6 @@ async def get_endpoint(session, url): async def some_api_call(urls): async with aiohttp.ClientSession() as session: tasks = [get_endpoint(session, url) for url in urls] + # Run requests concurrently results = await asyncio.gather(*tasks) return results From c9576e800da2018f9e1614c25d745779fef96c07 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 18:00:18 -0700 Subject: [PATCH 4/8] ml code Signed-off-by: Saurabh Misra --- codeflash/after_ml.py | 11 +++++++++++ codeflash/before_ml.py | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 codeflash/after_ml.py create mode 100644 codeflash/before_ml.py diff --git a/codeflash/after_ml.py b/codeflash/after_ml.py new file mode 100644 index 00000000..010ea725 --- /dev/null +++ b/codeflash/after_ml.py @@ -0,0 +1,11 @@ +import jax.numpy as jnp + + +def sum_hand(hand): + """Returns the total points in a hand.""" + return jnp.sum(hand) + (10 * usable_ace(hand)) + + +def usable_ace(hand): + """Checks to se if a hand has a usable ace.""" + return jnp.logical_and(jnp.any(hand == 1), jnp.sum(hand) + 10 <= 21) diff --git a/codeflash/before_ml.py b/codeflash/before_ml.py new file mode 100644 index 00000000..6449a3a5 --- /dev/null +++ b/codeflash/before_ml.py @@ -0,0 +1,11 @@ +import jax.numpy as jnp + + +def sum_hand(hand): + """Returns the total points in a hand.""" + return sum(hand) + (10 * usable_ace(hand)) + + +def usable_ace(hand): + """Checks to se if a hand has a usable ace.""" + return jnp.logical_and((jnp.count_nonzero(hand == 1) > 0), (sum(hand) + 10 <= 21)) From 730689b64574640fa435a82a6a21946036f4da08 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 18:23:49 -0700 Subject: [PATCH 5/8] ai agents Signed-off-by: Saurabh Misra --- codeflash/after_aiagents.py | 15 +++++++++++++++ codeflash/before_aiagents.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 codeflash/after_aiagents.py create mode 100644 codeflash/before_aiagents.py diff --git a/codeflash/after_aiagents.py b/codeflash/after_aiagents.py new file mode 100644 index 00000000..31395b60 --- /dev/null +++ b/codeflash/after_aiagents.py @@ -0,0 +1,15 @@ +def _estimate_string_tokens(content: str | Sequence[UserContent]) -> int: + if not content: + return 0 + if isinstance(content, str): + return len(_TOKEN_SPLIT_RE.split(content.strip())) + tokens = 0 + for part in content: + if isinstance(part, str): + tokens += len(_TOKEN_SPLIT_RE.split(part.strip())) + elif isinstance(part, BinaryContent): + tokens += len(part.data) + return tokens + + +_TOKEN_SPLIT_RE = re.compile(r'[\s",.:]+') diff --git a/codeflash/before_aiagents.py b/codeflash/before_aiagents.py new file mode 100644 index 00000000..f5d0f8ea --- /dev/null +++ b/codeflash/before_aiagents.py @@ -0,0 +1,16 @@ +def _estimate_string_tokens(content: str | Sequence[UserContent]) -> int: + if not content: + return 0 + if isinstance(content, str): + return len(re.split(r'[\s",.:]+', content.strip())) + tokens = 0 + for part in content: + if isinstance(part, str): + tokens += len(re.split(r'[\s",.:]+', part.strip())) + if isinstance(part, (AudioUrl, ImageUrl)): + tokens += 0 + elif isinstance(part, BinaryContent): + tokens += len(part.data) + else: + tokens += 0 + return tokens From 0b2017fdf03b7f6ab810d1cbcf390507320c1ca0 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 18:44:37 -0700 Subject: [PATCH 6/8] sql orm Signed-off-by: Saurabh Misra --- codeflash/after_db.py | 3 +++ codeflash/before_db.py | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 codeflash/after_db.py create mode 100644 codeflash/before_db.py diff --git a/codeflash/after_db.py b/codeflash/after_db.py new file mode 100644 index 00000000..ee8a2d48 --- /dev/null +++ b/codeflash/after_db.py @@ -0,0 +1,3 @@ +def get_authors(session): + query = session.query(Author).join(Book).distinct(Author.id).order_by(Author.id) + return query.all() diff --git a/codeflash/before_db.py b/codeflash/before_db.py new file mode 100644 index 00000000..68ef3449 --- /dev/null +++ b/codeflash/before_db.py @@ -0,0 +1,6 @@ +def get_authors(session): + books = session.query(Book).all() + _authors = [] + for book in books: + _authors.append(book.author) + return sorted(list(set(_authors)), key=lambda x: x.id) From a0650a81fd03154a4483b6a0f28ede91144b1c3d Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 19:20:03 -0700 Subject: [PATCH 7/8] data processing Signed-off-by: Saurabh Misra --- codeflash/after_data.py | 3 +++ codeflash/before_data.py | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 codeflash/after_data.py create mode 100644 codeflash/before_data.py diff --git a/codeflash/after_data.py b/codeflash/after_data.py new file mode 100644 index 00000000..d80080b2 --- /dev/null +++ b/codeflash/after_data.py @@ -0,0 +1,3 @@ +def check_missing_data(data: pd.DataFrame): + """Check if there is any missing data in the DataFrame""" + return data.isnull().values.any() diff --git a/codeflash/before_data.py b/codeflash/before_data.py new file mode 100644 index 00000000..8134136a --- /dev/null +++ b/codeflash/before_data.py @@ -0,0 +1,4 @@ +def check_missing_data(data: pd.DataFrame): + """Check if there is any missing data in the DataFrame""" + missing_data = data.isnull().sum().sum() > 0 + return missing_data From 159867a3a5a37dd39a3f92d19dc0f183afe0c6d6 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Thu, 4 Sep 2025 19:36:01 -0700 Subject: [PATCH 8/8] numerical Signed-off-by: Saurabh Misra --- codeflash/after_numerical.py | 17 +++++++++++++++++ codeflash/before_numerical.py | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 codeflash/after_numerical.py create mode 100644 codeflash/before_numerical.py diff --git a/codeflash/after_numerical.py b/codeflash/after_numerical.py new file mode 100644 index 00000000..98bfc9f4 --- /dev/null +++ b/codeflash/after_numerical.py @@ -0,0 +1,17 @@ +def _equalize_cv(img: np.ndarray, mask: np.ndarray | None = None) -> np.ndarray: + histogram = cv2.calcHist([img], [0], mask, [256], (0, 256)).ravel() + + # Find the first non-zero index with a numpy operation + i = np.flatnonzero(histogram)[0] if np.any(histogram) else 255 + + total = np.sum(histogram) + if histogram[i] == total: + return np.full_like(img, i) + + scale = 255.0 / (total - histogram[i]) + + # Optimize cumulative sum and scale to generate LUT + cumsum_histogram = np.cumsum(histogram) + lut = np.clip(((cumsum_histogram - cumsum_histogram[i]) * scale) + .round(), 0, 255).astype(np.uint8) + return sz_lut(img, lut, inplace=True) diff --git a/codeflash/before_numerical.py b/codeflash/before_numerical.py new file mode 100644 index 00000000..ba6a19db --- /dev/null +++ b/codeflash/before_numerical.py @@ -0,0 +1,22 @@ +def _equalize_cv(img: np.ndarray, mask: np.ndarray | None = None) -> np.ndarray: + histogram = cv2.calcHist([img], [0], mask, [256], (0, 256)).ravel() + i = 0 + for val in histogram: + if val > 0: + break + i += 1 + i = min(i, 255) + + total = np.sum(histogram) + if histogram[i] == total: + return np.full_like(img, i) + + scale = 255.0 / (total - histogram[i]) + _sum = 0 + + lut = np.zeros(256, dtype=np.uint8) + + for idx in range(i + 1, len(histogram)): + _sum += histogram[idx] + lut[idx] = clip(round(_sum * scale), np.uint8) + return sz_lut(img, lut, inplace=True)