Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce All Numbers to Floats to Maintain Consistency Between JS, JSON Schema, and Python #405

Merged
merged 6 commits into from
Sep 29, 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
22 changes: 16 additions & 6 deletions pyflask/manageNeuroconv/manage_neuroconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,32 @@ def replace_none_with_nan(json_object, json_schema):
dict: The modified JSON object with None values replaced by NaN.
"""

def replace_none_recursive(obj, schema):
def coerce_schema_compliance_recursive(obj, schema):
if isinstance(obj, dict):
for key, value in obj.items():
if key in schema.get("properties", {}):
prop_schema = schema["properties"][key]
if prop_schema.get("type") == "number" and value is None:
obj[key] = math.nan
obj[
key
] = (
math.nan
) # Turn None into NaN if a number is expected (JavaScript JSON.stringify turns NaN into None)
elif prop_schema.get("type") == "number" and isinstance(value, int):
obj[key] = float(
value
) # Turn integer into float if a number, the JSON Schema equivalent to float, is expected (JavaScript coerces floats with trailing zeros to integers)
else:
replace_none_recursive(value, prop_schema)
coerce_schema_compliance_recursive(value, prop_schema)
elif isinstance(obj, list):
for item in obj:
replace_none_recursive(item, schema.get("items", {}))
coerce_schema_compliance_recursive(item, schema.get("items", {}))

return obj

return replace_none_recursive(copy.deepcopy(json_object), resolve_references(copy.deepcopy(json_schema)))
return coerce_schema_compliance_recursive(
copy.deepcopy(json_object), resolve_references(copy.deepcopy(json_schema))
)


def locate_data(info: dict) -> dict:
Expand Down Expand Up @@ -363,7 +373,7 @@ def update_conversion_progress(**kwargs):
info["metadata"].update(Ecephys=dict())

resolved_metadata = replace_none_with_nan(
info["metadata"], converter.get_metadata_schema()
info["metadata"], resolve_references(converter.get_metadata_schema())
) # Ensure Ophys NaN values are resolved

# if is_supported_recording_interface(recording_interface, info["metadata"]):
Expand Down
2 changes: 1 addition & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const exitPyProc = async () => {
const killAllPreviousProcesses = async () => {

const fetch = globalThis.fetch

if (fetch){
console.log("Killing all previous processes");

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/progress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function decode(message) {

function drill(o, callback) {
if (o && typeof o === "object") {
const copy = Array.isArray(o) ? [...o] : { ...o } ;
const copy = Array.isArray(o) ? [...o] : { ...o };
for (let k in copy) copy[k] = drill(copy[k], callback);
return copy;
} else return callback(o);
Expand Down
Loading