Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Standardize to all pascalcase #70

Merged
merged 5 commits into from
Aug 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ See https://github.com/flojoy-io/flojoy-python/issues/4

Flojoy nodes should try to accomodate any reasonable combination of inputs that a first-time Flojoy Studio user might try.

For example, the ADD node should make a best effort to do something reasonable when a matrix is added to a dataframe, or a 2 matrices of a different size are added.
For example, the ADD node should make a best effort to do something reasonable when a matrix is added to a DataFrame, or a 2 matrices of a different size are added.

For this reason, we've created the `Reconciler` class to handle the process of turning different data types into compatible, easily added objects.

Expand Down
118 changes: 60 additions & 58 deletions flojoy/data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ def find_closest_match(given_str: str, available_str: list[str]):


DCType = Literal[
"dataframe",
"grayscale",
"image",
"matrix",
"ordered_pair",
"ordered_triple",
"plotly",
"bytes",
"text_blob",
"scalar",
"surface",
"vector",
"parametric_dataframe",
"parametric_grayscale",
"parametric_image",
"parametric_matrix",
"parametric_ordered_pair",
"parametric_ordered_triple",
"parametric_plotly",
"parametric_scalar",
"parametric_surface",
"parametric_vector",
"DataFrame",
"Grayscale",
"Image",
"Matrix",
"OrderedPair",
"OrderedTriple",
"Plotly",
"Bytes",
"TextBlob",
"Scalar",
"Surface",
"Vector",
"ParametricDataFrame",
"ParametricGrayscale",
"ParametricImage",
"ParametricMatrix",
"ParametricOrderedPair",
"ParametricOrderedTriple",
"ParametricPlotly",
"ParametricScalar",
"ParametricSurface",
"ParametricVector",
]

DCNpArrayType = np.ndarray[Union[int, float], np.dtype[Any]]

DCKwargsValue = Union[
list[Union[int, float]],
int,
Expand All @@ -53,6 +54,7 @@ def find_closest_match(given_str: str, available_str: list[str]):
str,
None,
]

ExtraType = dict[str, Any] | None


Expand All @@ -72,7 +74,7 @@ class DataContainer(Box):

v.y = np.sin(v.x)

v.type = 'ordered_pair'
v.type = 'OrderedPair'

"""

Expand Down Expand Up @@ -111,18 +113,18 @@ class DataContainer(Box):
"fig": ["t", "extra"],
}
type_keys_map: dict[DCType, list[str]] = {
"dataframe": ["m"],
"matrix": ["m"],
"vector": ["v"],
"grayscale": ["m"],
"image": ["r", "g", "b", "a"],
"ordered_pair": ["x", "y"],
"ordered_triple": ["x", "y", "z"],
"surface": ["x", "y", "z"],
"scalar": ["c"],
"plotly": ["fig"],
"bytes": ["b"],
"text_blob": ["text_blob"],
"DataFrame": ["m"],
"Matrix": ["m"],
"Vector": ["v"],
"Grayscale": ["m"],
"Image": ["r", "g", "b", "a"],
"OrderedPair": ["x", "y"],
"OrderedTriple": ["x", "y", "z"],
"Surface": ["x", "y", "z"],
"Scalar": ["c"],
"Plotly": ["fig"],
"Bytes": ["b"],
"TextBlob": ["text_blob"],
}

SKIP_ARRAYIEFY_TYPES = [
Expand Down Expand Up @@ -166,7 +168,7 @@ def _ndarrayify(
)

def __init__( # type:ignore
self, type: DCType = "ordered_pair", **kwargs: DCKwargsValue
self, type: DCType = "OrderedPair", **kwargs: DCKwargsValue
):
self.type = type
for k, v in kwargs.items():
Expand Down Expand Up @@ -263,7 +265,7 @@ class OrderedPair(DataContainer):
def __init__( # type:ignore
self, x: DCNpArrayType, y: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="ordered_pair", x=x, y=y, extra=extra)
super().__init__(type="OrderedPair", x=x, y=y, extra=extra)


class ParametricOrderedPair(DataContainer):
Expand All @@ -278,7 +280,7 @@ def __init__( # type:ignore
t: DCNpArrayType,
extra: ExtraType = None,
):
super().__init__(type="parametric_ordered_pair", x=x, y=y, t=t, extra=extra)
super().__init__(type="ParametricOrderedPair", x=x, y=y, t=t, extra=extra)


class OrderedTriple(DataContainer):
Expand All @@ -293,7 +295,7 @@ def __init__( # type:ignore
z: DCNpArrayType,
extra: ExtraType = None,
):
super().__init__(type="ordered_triple", x=x, y=y, z=z, extra=extra)
super().__init__(type="OrderedTriple", x=x, y=y, z=z, extra=extra)


class ParametricOrderedTriple(DataContainer):
Expand All @@ -311,7 +313,7 @@ def __init__( # type:ignore
extra: ExtraType = None,
):
super().__init__(
type="parametric_ordered_triple", x=x, y=y, z=z, t=t, extra=extra
type="ParametricOrderedTriple", x=x, y=y, z=z, t=t, extra=extra
)


Expand All @@ -327,7 +329,7 @@ def __init__( # type:ignore
z: DCNpArrayType,
extra: ExtraType = None,
):
super().__init__(type="surface", x=x, y=y, z=z, extra=extra)
super().__init__(type="Surface", x=x, y=y, z=z, extra=extra)

def validate(self):
if self.z.ndim < 2:
Expand All @@ -349,7 +351,7 @@ def __init__( # type:ignore
t: DCNpArrayType,
extra: ExtraType = None,
):
super().__init__(type="parametric_surface", x=x, y=y, z=z, t=t, extra=extra)
super().__init__(type="ParametricSurface", x=x, y=y, z=z, t=t, extra=extra)

def validate(self):
if self.z.ndim < 2:
Expand All @@ -361,7 +363,7 @@ class Scalar(DataContainer):
c: int | float

def __init__(self, c: int | float, extra: ExtraType = None): # type:ignore
super().__init__(type="scalar", c=c, extra=extra)
super().__init__(type="Scalar", c=c, extra=extra)


class ParametricScalar(DataContainer):
Expand All @@ -371,14 +373,14 @@ class ParametricScalar(DataContainer):
def __init__( # type: ignore
self, c: int | float, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_scalar", c=c, t=t, extra=extra)
super().__init__(type="ParametricScalar", c=c, t=t, extra=extra)


class Vector(DataContainer):
v: DCNpArrayType

def __init__(self, v: DCNpArrayType, extra: ExtraType = None): # type:ignore
super().__init__(type="vector", v=v, extra=extra)
super().__init__(type="Vector", v=v, extra=extra)


class ParametricVector(DataContainer):
Expand All @@ -387,14 +389,14 @@ class ParametricVector(DataContainer):
def __init__( # type: ignore
self, v: DCNpArrayType, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_vector", v=v, t=t, extra=extra)
super().__init__(type="ParametricVector", v=v, t=t, extra=extra)


class Matrix(DataContainer):
m: DCNpArrayType

def __init__(self, m: DCNpArrayType, extra: ExtraType = None): # type:ignore
super().__init__(type="matrix", m=m, extra=extra)
super().__init__(type="Matrix", m=m, extra=extra)


class ParametricMatrix(DataContainer):
Expand All @@ -404,14 +406,14 @@ class ParametricMatrix(DataContainer):
def __init__( # type: ignore
self, m: DCNpArrayType, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_matrix", m=m, t=t, extra=extra)
super().__init__(type="ParametricMatrix", m=m, t=t, extra=extra)


class DataFrame(DataContainer):
m: pd.DataFrame

def __init__(self, df: pd.DataFrame, extra: ExtraType = None): # type:ignore
super().__init__(type="dataframe", m=df, extra=extra)
super().__init__(type="DataFrame", m=df, extra=extra)


class ParametricDataFrame(DataContainer):
Expand All @@ -421,14 +423,14 @@ class ParametricDataFrame(DataContainer):
def __init__( # type:ignore
self, df: pd.DataFrame, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_dataframe", m=df, t=t, extra=extra)
super().__init__(type="ParametricDataFrame", m=df, t=t, extra=extra)


class Plotly(DataContainer):
fig: go.Figure

def __init__(self, fig: go.Figure, extra: ExtraType = None): # type:ignore
super().__init__(type="plotly", fig=fig, extra=extra)
super().__init__(type="Plotly", fig=fig, extra=extra)


class ParametricPlotly(DataContainer):
Expand All @@ -438,7 +440,7 @@ class ParametricPlotly(DataContainer):
def __init__( # type:ignore
self, fig: go.Figure, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_plotly", fig=fig, t=t, extra=extra)
super().__init__(type="ParametricPlotly", fig=fig, t=t, extra=extra)


class Image(DataContainer):
Expand All @@ -455,7 +457,7 @@ def __init__( # type:ignore
a: DCNpArrayType | None = None,
extra: ExtraType = None,
):
super().__init__(type="image", r=r, g=g, b=b, a=a, extra=extra)
super().__init__(type="Image", r=r, g=g, b=b, a=a, extra=extra)


class Bytes(DataContainer):
Expand All @@ -465,14 +467,14 @@ def __init__(
self,
b: bytes,
):
super().__init__(type="bytes", b=b)
super().__init__(type="Bytes", b=b)


class TextBlob(DataContainer):
text_blob: str

def __init__(self, text_blob: str):
super().__init__(type="text_blob", text_blob=text_blob)
super().__init__(type="TextBlob", text_blob=text_blob)


class ParametricImage(DataContainer):
Expand All @@ -491,14 +493,14 @@ def __init__( # type:ignore
t: DCNpArrayType,
extra: ExtraType = None,
):
super().__init__(type="parametric_image", r=r, g=g, b=b, a=a, t=t, extra=extra)
super().__init__(type="ParametricImage", r=r, g=g, b=b, a=a, t=t, extra=extra)


class Grayscale(DataContainer):
m: DCNpArrayType

def __init__(self, img: DCNpArrayType, extra: ExtraType = None): # type:ignore
super().__init__(type="grayscale", m=img, extra=extra)
super().__init__(type="Grayscale", m=img, extra=extra)


class ParametricGrayscale(DataContainer):
Expand All @@ -508,4 +510,4 @@ class ParametricGrayscale(DataContainer):
def __init__( # type:ignore
self, img: DCNpArrayType, t: DCNpArrayType, extra: ExtraType = None
):
super().__init__(type="parametric_grayscale", m=img, t=t, extra=extra)
super().__init__(type="ParametricGrayscale", m=img, t=t, extra=extra)
4 changes: 2 additions & 2 deletions flojoy/job_result_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def get_job_result(job_id: str) -> dict[str, Any] | DataContainer | None:

def get_text_blob_from_dc(dc: DataContainer) -> str | None:
match dc.type:
case "text_blob":
case "TextBlob":
return dc.text_blob
case "bytes":
case "Bytes":
return dc.b.decode("utf-8")
case _:
return None
Expand Down
27 changes: 12 additions & 15 deletions flojoy/plotly_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,34 @@
def data_container_to_plotly(data: DataContainer) -> dict[str, Any] | None:
data_copy = data.copy()
dc_type = data_copy.type
fig = go.Figure()
fig = go.Figure(layout=dict(template="plotly"))
if "x" in data_copy and isinstance(data_copy.x, dict): # type: ignore
data_keys = list(cast(list[str], data_copy.x.keys()))
data_copy.x = data_copy.x[data_keys[0]] # type: ignore

match dc_type:
case "image":
case "Image":
if data_copy.a is None:
img_combined = np.stack((data_copy.r, data_copy.g, data_copy.b), axis=2)
else:
img_combined = np.stack(
(data_copy.r, data_copy.g, data_copy.b, data_copy.a), axis=2
)
fig = px.imshow(img=img_combined) # type:ignore
case "ordered_pair":
case "OrderedPair":
if data_copy.x is not None and len(data_copy.x) != len(data_copy.y):
data_copy.x = np.arange(0, len(data_copy.y), 1)
try:
fig = px.line(x=data_copy.x, y=data_copy.y)
except Exception as e:
fig = px.line(x=data_copy.x, y=data_copy.y)
case "ordered_triple":
fig = px.line(x=data_copy.x, y=data_copy.y)
case "OrderedTriple":
fig = px.scatter_3d(x=data_copy.x, y=data_copy.y, z=data_copy.z)
case "scalar":
case "Scalar":
fig = px.histogram(x=data_copy.c)
case "vector":
case "Vector":
df = pd.DataFrame(data_copy.v)
fig = go.Figure(
data=[go.Table(header=dict(values=["Vector"]), cells=dict(values=[df]))]
)
case "dataframe":
case "DataFrame":
df = cast(pd.DataFrame, data_copy.m)
fig = go.Figure(
data=[
Expand All @@ -49,7 +46,7 @@ def data_container_to_plotly(data: DataContainer) -> dict[str, Any] | None:
)
]
)
case "grayscale" | "matrix":
case "Grayscale" | "Matrix":
y_columns: np.ndarray = data_copy.m
for i, col in enumerate(y_columns.T):
fig.add_trace(
Expand All @@ -60,13 +57,13 @@ def data_container_to_plotly(data: DataContainer) -> dict[str, Any] | None:
name=i,
)
)
case "surface":
case "Surface":
fig = go.Figure(
data=[go.Surface(x=data_copy.x, y=data_copy.y, z=data_copy.z)]
)
case "plotly":
case "Plotly":
fig = cast(go.Figure, data.fig)
case "bytes" | "text_blob":
case "Bytes" | "TextBlob":
return None
case _:
raise ValueError(
Expand Down
Loading