Watts-Lab / commonsense-platform

Commonsense platform
https://commonsense.seas.upenn.edu
1 stars 0 forks source link

Treatment protocol #119

Closed markwhiting closed 3 weeks ago

markwhiting commented 3 months ago

Realized that the treatment protocol can be a bit simplified:

treatments either

The fancy stringify also works now, I think:

function ordered_stringify(object, before = "{", after = "}") {
  return (
    before +
    Object.keys(object)
      .sort()
      .reduce(
        (list, item) =>
          list.concat(
            `"${item}":${
              typeof object[item] == "object"
                ? "{" + orderd_stringify(object[item], "", "") + "}"
                : typeof object[item] == "string"
                ? '"' + object[item] + '"'
                : object[item]
            },`
          ),
        ""
      )
      .replace(/,$/, "") +
    after
  );
}

(one caveat I noticed: arrays turn into objects with indices, e.g., [a,b,c] turns into {0:a,1:b,2:c}. Not sure if we care, also not yet sure how to fix that, but haven't looked into it much.)

markwhiting commented 3 months ago

Just trying to jot down some of our discussion today @amirrr — please add anything I missed.

markwhiting commented 3 months ago

Improved the string comparator to remove the array issue:

function ordered_stringify(object) {
  return typeof object == "object"
    ? Array.isArray(object)
      ? `[${object.map(ordered_stringify)}]`
      : `{${Object.entries(object)
          .sort((a, b) => a[0] > b[0])
          .map((a) => `${a[0]}:${ordered_stringify(a[1])}`)}}`
    : object;
}
markwhiting commented 3 weeks ago

Is this done? I think we have kind of settled on an approach, right?