Skip to content

Commit

Permalink
Handle large numbers that sometimes happen
Browse files Browse the repository at this point in the history
  • Loading branch information
odarbelaeze committed Dec 5, 2024
1 parent ce3c473 commit d693f18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
35 changes: 27 additions & 8 deletions functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from firebase_functions.options import MemoryOption
from firebase_functions.scheduler_fn import ScheduledEvent, on_schedule
from google.cloud.firestore import DocumentSnapshot
from google.cloud.firestore import DocumentReference, DocumentSnapshot
from pydantic import BaseModel, ValidationError

logging.basicConfig(level=logging.INFO)
Expand All @@ -35,7 +35,7 @@
INVOICING_LEEWAY_HOURS = 6


def set_analysis_property(collection: Collection, ref) -> None:
def set_analysis_property(collection: Collection, ref: DocumentReference) -> None:
cited = {str(key): value for key, value in collection.cited_by_year().items()}
published = {
str(key): value for key, value in collection.published_by_year().items()
Expand All @@ -47,7 +47,7 @@ def set_analysis_property(collection: Collection, ref) -> None:
ref.update({"_analysis": _analysis})


def tree_from_strings(strings: List[str], ref) -> nx.DiGraph:
def tree_from_strings(strings: List[str], ref: DocumentReference) -> nx.DiGraph:
"""Creates a ToS tree from a list of strings."""
collections = [read_any(StringIO(text)) for text in strings]
collection = reduce(lambda x, y: x.merge(y), collections)
Expand All @@ -58,6 +58,29 @@ def tree_from_strings(strings: List[str], ref) -> nx.DiGraph:
return sap.tree(graph)


def _round_to_signed_64bit(num: int):
min_int64 = -(2**63)
max_int64 = 2**63 - 1
if num < min_int64:
return min_int64
elif num > max_int64:
return max_int64
else:
return int(round(num))


def _clear_datum(datum: dict) -> dict:
clear = {
key: val
for key, val in datum.items()
if not key.startswith("_") and key != "extra"
}
clear[ROOT] = _round_to_signed_64bit(clear.get(ROOT, 0))
clear[TRUNK] = _round_to_signed_64bit(clear.get(TRUNK, 0))
clear[LEAF] = _round_to_signed_64bit(clear.get(LEAF, 0))
return clear


def convert_tos_to_json(tree: nx.DiGraph) -> Dict[str, List[Dict]]:
"""
Converts a ToS graph in the default format to be processed by the frontend.
Expand All @@ -69,11 +92,7 @@ def convert_tos_to_json(tree: nx.DiGraph) -> Dict[str, List[Dict]]:
[
{
"label": node,
**{
key: val
for key, val in data.items()
if not key.startswith("_") and key != "extra"
},
**_clear_datum(data),
}
for node, data in tree.nodes.items()
if tree.nodes[node][section] > 0
Expand Down
12 changes: 10 additions & 2 deletions src/components/tree/TreeVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ interface Props {
treeResult: TreeResult;
}

const clamp = (
n: number,
min: number,
max: number,
scale: number,
offset: number,
): number => ((n - min) / (max - min + 1)) * scale + offset;

const articlesToData = (
articles: ArticleWithMetrics[],
section: Section,
Expand Down Expand Up @@ -59,8 +67,8 @@ const articlesToData = (
} else {
r =
section === "leaf"
? ((article[section] - minRadius) / (maxRadius - minRadius)) * 15 + 12
: ((article[section] - minRadius) / (maxRadius - minRadius)) * 10 + 8;
? clamp(article[section], minRadius, maxRadius, 15, 12)
: clamp(article[section], minRadius, maxRadius, 10, 8);
}
return {
className: {
Expand Down

0 comments on commit d693f18

Please sign in to comment.