Skip to content

Commit

Permalink
Merge pull request #12 from C-Accel-CRIPT/feat/rcbc
Browse files Browse the repository at this point in the history
feat(typescript): add rcbc
  • Loading branch information
bearmit authored Apr 25, 2023
2 parents 4a02288 + effc40a commit d72f979
Show file tree
Hide file tree
Showing 23 changed files with 1,683 additions and 119 deletions.
273 changes: 234 additions & 39 deletions scripts/typescript/package-lock.json

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions scripts/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
"name": "cript-typescript",
"version": "1.0.0",
"description": "Set of typescript code to convert raw data (*.csv, *.ts) to CRIPT *.json format",
"description": "Set of typescript code to convert raw data (*.xlsx, *.csv, *.ts) to CRIPT *.json format",
"main": "index.js",
"scripts": {
"afrl": "npx ts-node ./src/afrl/index.ts",
"rcbc": "npx ts-node ./src/rcbc/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
"build": "npx tsc",
"afrl": "npx ts-node ./src/afrl",
"bcdb": "npx ts-node ./src/bcdb",
"rcbc": "npx ts-node ./src/rcbc",
"test": "bash ./test.sh"
},
"author": "Bérenger Dalle-Cort, CRIPT, MIT",
"license": "MIT",
"dependencies": {
"csvtojson": "^2.0.10",
"jsonstream-ts": "^1.3.6",
"ts-node": "^10.9.1",
"typescript": "^5.0.3"
"tsconfig-paths": "^4.2.0",
"typescript": "^5.0.3",
"xlsx": "^0.18.5"
},
"devDependencies": {
"tsconfig-paths": "^4.2.0"
"@types/node": "^18.15.12"
}
}
24 changes: 12 additions & 12 deletions scripts/typescript/src/afrl/afrl-csv-to-json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AFRLData } from "./types"
import { ICitation, ICollection, ICondition, IInventory, IMaterial, IProject, IProperty, IReference } from "../types/cript"
import csvtojson from "csvtojson";

import * as fs from 'stream';
/**
* This script is a typescript port of @see https://github.com/C-Accel-CRIPT/criptscripts/tree/master/scripts/python_sdk_scripts/AFRL
* The main difference is the source data is now read from the original CSV and not a preprocessed *.js.
Expand All @@ -12,6 +12,9 @@ export type Config = {
project_name: string;
// Destination inventory's basename
inventory_basename: string;
// to redirect errors to a stream
// By default it will be process.stderr
error_stream?: fs.Writable;
}

export class AFRLtoJSON {
Expand All @@ -34,9 +37,12 @@ export class AFRLtoJSON {
private inventory_polymers: IInventory;
private inventory_mixtures: IInventory;

private errors: Array<string> = [];
private error_stream: fs.Writable;

constructor(config: Config) {

constructor(config: Config = AFRLtoJSON.load_config()) {
this.error_stream = config.error_stream ?? process.stderr;
this.error_stream.on('error', e => console.error(e));

// Create inventories

Expand Down Expand Up @@ -83,10 +89,6 @@ export class AFRLtoJSON {

}

get_errors(): any {
return [...this.errors];
}

private get_citation(row: AFRLData): ICitation | undefined {

if(row.reference === undefined) {
Expand Down Expand Up @@ -388,11 +390,8 @@ export class AFRLtoJSON {
return `{{[]${bigsmiles}[]}}`;
}


private record_error(message: string): void {

this.errors.push(message)
console.error(message);
this.error_stream.write(`${message}\n`)
}

/**
Expand Down Expand Up @@ -577,6 +576,7 @@ export class AFRLtoJSON {
return safe_object;
});
console.log(`Assigning default value for string properties OK`);

return afrl_data;
}

Expand All @@ -603,7 +603,7 @@ export class AFRLtoJSON {
// Log failures
if (failed_rows.length != 0) {
console.log(`Loading failed. Some objects couldn't be loaded (${failed_rows.length} row(s) failed)`)
failed_rows.forEach(v => console.error(JSON.stringify(v)))
failed_rows.forEach(v => this.record_error(JSON.stringify(v)))
throw new Error(`${failed_rows.length} row(s) were not loaded.`)
}

Expand Down
8 changes: 3 additions & 5 deletions scripts/typescript/src/afrl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ grand_parent: CRIPT Scripts

# AFRL CSV to JSON script

> *Note: this script is a work in progress*
*note: this script is WIP*

This script converts the Air Force Research Laboratory's CSV file into a unique JSON file.
The file can be uploaded at once as a single project into CRIPT.
This script converts the AFRL CSV file into a unique JSON file. The file can be uploaded at once as a single project into CRIPT.

## TypeScript Script
* [TypeScript source code](https://github.com/C-Accel-CRIPT/criptscripts/tree/master/scripts/typescript/src/afrl)
See source code: [Link to TypeScript source code](https://github.com/C-Accel-CRIPT/criptscripts/tree/master/scripts/typescript/src/afrl)
23 changes: 13 additions & 10 deletions scripts/typescript/src/afrl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ import { output_dir_path, write_json_helper } from "@utilities";
const file_name = 'AFRL_linear_polymer_3pdb_data_csv_4_5_2023.csv'; // TODO: read this from command line argument

// Call main as a promise because low level await are not allowed.
main().then( code => process.exit(code) )
main().then( () => process.exit() )

async function main(): Promise<number> {
async function main() {

console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
console.log('=-=-=-=-=-=-=-=-=-=-=-=-= AFRL CSV to JSON -=-=-=-=-=-==-=-=-=-=-=-=-=')
console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')

// Pipe the errors in a file
const error_file_path = path.resolve(output_dir_path, 'afrl.errors.txt');
const error_file = fs.createWriteStream(error_file_path, { flags: 'w'});
error_file.write("Beginnin of the conversion...\n");
error_file.write("Second line")

// Instantiate AFRL to JSON serializer
const serializer = new AFRLCSVtoJSON({
inventory_basename: 'afrl-deleteme',
project_name: 'afrl-deleteme'
inventory_basename: 'AFRL (dev)',
project_name: 'AFRL (dev)',
error_stream: error_file
});

// Load the CSV into an AFRLData[]
Expand All @@ -38,11 +45,7 @@ async function main(): Promise<number> {
console.log('Output folder is ready')

// Write JSON files

write_json_helper(project, 'afrl-transformed', 'minified')
write_json_helper(project, 'afrl-transformed', 'human-readable')
write_json_helper(serializer.get_errors(), 'afrl-transformed.errors', 'human-readable')

return 0;
await write_json_helper(project, 'afrl', 'minified');
await write_json_helper(project, 'afrl', 'human-readable');
}

4 changes: 4 additions & 0 deletions scripts/typescript/src/rcbc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# RCBC

This script export to CRIPT JSON a statically
The JSON is produced by strigifying the main object (project) following a set of rules (cf ../utilities/cript-json.ts)
11 changes: 11 additions & 0 deletions scripts/typescript/src/rcbc/data/graph/collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ICollection } from "@cript";
import { default_notes } from "./shared";
import * as experiments from "./experiments";

export const collection: ICollection = {
name: "Olsen et al. PPV-b-PI",
notes: default_notes,
inventory: [],
experiment: [...Object.values(experiments)],
node: ['Collection']
};
38 changes: 38 additions & 0 deletions scripts/typescript/src/rcbc/data/graph/computations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IComputation } from "@cript";
import * as datasets from "./datasets";

export const analysis_PPVbPI_42 = {
name: '1/T Analysis PPV-b-PI-42',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_42]
} as IComputation;

export const peak_phase_id_PPVbPI_42 = {
name: 'Peak Phase ID PPV-b-PI-42',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_42]
} as IComputation;

export const analysis_PPVbPI_59 = {
name: '1/T Analysis PPV-b-PI-59',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_59]
} as IComputation;

export const peak_phase_id_PPVbPI_59 = {
name: 'Peak Phase ID PPV-b-PI-59',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_59]
} as IComputation;

export const analysis_PPVbPI_72 = {
name: '1/T Analysis PPV-b-PI-72',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_72]
} as IComputation;

export const peak_phase_id_PPVbPI_72 = {
name: 'Peak Phase ID PPV-b-PI-72',
node: ['Computation'],
input_data: [ datasets.saxs_ppvbpi_72]
} as IComputation;
81 changes: 81 additions & 0 deletions scripts/typescript/src/rcbc/data/graph/datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { IData } from "@cript";

export const nmr_diether: IData = {
name: "NMR diether",
node: ["Data"],
};

export const nmr_aldehyde: IData = {
name: "NMR aldehyde",
node: ["Data"],
};

export const gpc_ppv: IData = {
name: "GPC PPV",
node: ["Data"],
};

export const density_column_ppv: IData = {
name: "Density Column PPV",
node: ["Data"],
};

export const dsc_ppv: IData = {
name: "DSC PPV",
node: ["Data"],
};

export const nmr_ppv: IData = {
name: "NMR PPV",
node: ["Data"],
};

export const nmr_imine: IData = {
name: "NMR imine",
node: ["Data"],
};

export const pom_ppv: IData = {
name: "POM PPV",
node: ["Data"],
};

export const gpc_pi_synth_42: IData = {
name: "GPC PI-42",
node: ["Data"]
}

export const gpc_pi_synth_59: IData = {
name: "GPC PI-59",
node: ["Data"]
}

export const gpc_pi_synth_72: IData = {
name: "GPC PI-72",
node: ["Data"]
}

export const gpc_pi_synth_89: IData = {
name: "GPC PI-89",
node: ["Data"]
}

export const saxs_ppvbpi_42: IData = {
name: "SAXS PPV-b-PI-42",
node: ["Data"]
}

export const saxs_ppvbpi_59: IData = {
name: "SAXS PPV-b-PI-59",
node: ["Data"]
}

export const saxs_ppvbpi_72: IData = {
name: "SAXS PPV-b-PI-72",
node: ["Data"]
}

export const saxs_ppvbpi_89: IData = {
name: "SAXS PPV-b-PI-89",
node: ["Data"]
}
Loading

0 comments on commit d72f979

Please sign in to comment.