-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some refactoring, implemented base cell summary similarities and rui …
…location distances. WIP.
- Loading branch information
Showing
13 changed files
with
785,616 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { getAllCellSummarySimilarities } from './utils/cell-summary-similarity.js'; | ||
|
||
const OUTPUT = '../data/cell-summary-similarities.jsonld'; | ||
|
||
const allSummaries = [ | ||
'../data/dataset-cell-summaries.jsonld', | ||
'../data/as-cell-summaries.jsonld', | ||
'../data/rui-location-cell-summaries.jsonld', | ||
] | ||
.map((path) => JSON.parse(readFileSync(path))['@graph']) | ||
.reduce((acc, arr) => acc.concat(arr), []); | ||
|
||
const results = [...getAllCellSummarySimilarities(allSummaries)]; | ||
|
||
// Write out the new enriched_rui_locations.jsonld file | ||
const jsonld = { | ||
...JSON.parse(readFileSync('ccf-context.jsonld')), | ||
'@graph': results, | ||
}; | ||
writeFileSync(OUTPUT, JSON.stringify(jsonld, null, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { getAllSpatialEntityDistances } from './utils/spatial-entity-distance.js'; | ||
|
||
const OUTPUT = '../data/rui-location-distances.jsonld'; | ||
const donors = JSON.parse(readFileSync('../data/rui_locations.jsonld'))['@graph']; | ||
|
||
const ruiLocations = []; | ||
for (const donor of donors) { | ||
for (const block of donor['samples']) { | ||
const ruiLocation = block.rui_location; | ||
ruiLocations.push(ruiLocation); | ||
} | ||
} | ||
|
||
const results = []; | ||
for await (const distance of getAllSpatialEntityDistances(ruiLocations)) { | ||
results.push(distance); | ||
}; | ||
|
||
// Write out the new enriched_rui_locations.jsonld file | ||
const jsonld = { | ||
...JSON.parse(readFileSync('ccf-context.jsonld')), | ||
'@graph': results, | ||
}; | ||
writeFileSync(OUTPUT, JSON.stringify(jsonld, null, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { dot, norm } from 'mathjs'; | ||
|
||
/** | ||
* A function to return a cosine sim for two vectors | ||
* | ||
* @param {number[]} a | ||
* @param {number[]} b | ||
* @returns cosine similarity between a and b | ||
*/ | ||
function cosineSim(a, b) { | ||
return dot(a, b) / (norm(a) * norm(b)); | ||
} | ||
|
||
function getCellCountsSimilarity(cellsA, cellsB) { | ||
const keySet = new Set(Object.keys(cellsA)); | ||
let sharedKey = false; | ||
for (const key of Object.keys(cellsB)) { | ||
if (keySet.has(key)) { | ||
sharedKey = true; | ||
} | ||
keySet.add(key); | ||
} | ||
|
||
// Only compute cosine sim if there is at least one shared key | ||
if (sharedKey) { | ||
const keys = [...keySet]; | ||
const valuesA = keys.map((key) => cellsA[key] ?? 0); | ||
const valuesB = keys.map((key) => cellsB[key] ?? 0); | ||
return cosineSim(valuesA, valuesB); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
function getCellDistribution(summary) { | ||
return summary['summary'].reduce((acc, row) => ((acc[row['cell_id']] = row['percentage']), acc), {}); | ||
} | ||
|
||
export function getCellSummarySimilarity(summaryA, summaryB) { | ||
const cellsA = getCellDistribution(summaryA); | ||
const cellsB = getCellDistribution(summaryB); | ||
return getCellCountsSimilarity(cellsA, cellsB); | ||
} | ||
|
||
export function* getAllCellSummarySimilarities(summaries, minSimilarity = 0) { | ||
for (let i = 0; i < summaries.length; i++) { | ||
for (let j = i + 1; j < summaries.length; j++) { | ||
const summaryA = summaries[i]; | ||
const summaryB = summaries[j]; | ||
const similarity = getCellSummarySimilarity(summaryA, summaryB); | ||
if (similarity > minSimilarity) { | ||
yield { | ||
'@type': 'CellSummarySimilarity', | ||
cell_source_a: summaryA['cell_source'], | ||
cell_source_b: summaryB['cell_source'], | ||
similarity, | ||
}; | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.