MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / getSampleRowHandler

Function getSampleRowHandler

data-csv/index.js:104–143  ·  view source on GitHub ↗

* Accesses the CSV to collect a single specified row. The row index * is specified by the UI element managed in the ui library.

()

Source from the content-addressed store, hash-verified

102 * is specified by the UI element managed in the ui library.
103 */
104async function getSampleRowHandler() {
105 const url = ui.getQueryElement().value;
106 ui.updateStatus(`Attempting to connect to CSV resource at ${url}`);
107 const myData = tf.data.csv(url);
108 ui.updateStatus('Got the data connection ... getting requested sample');
109 const sampleIndex = ui.getSampleIndex();
110 if (sampleIndex < 0 || isNaN(sampleIndex)) {
111 const msg = `Can not get samples with negative or NaN index. (Requested ${
112 sampleIndex}).`;
113 ui.updateStatus(msg);
114 ui.updateSampleRowMessage(msg);
115 ui.updateSampleRowOutput([]);
116 return;
117 }
118 let sample;
119 try {
120 sample = await myData.skip(sampleIndex).take(1).toArray();
121 } catch (e) {
122 let errorMsg = `Caught an error retrieving sample from ${url}. `;
123 errorMsg +=
124 'This URL might not be valid or might not support CORS requests.';
125 errorMsg += ' Check the developer console for CORS errors.';
126 errorMsg += e;
127 ui.updateSampleRowMessage(errorMsg);
128 return;
129 }
130 if (sample.length === 0) {
131 // When samples are requested beyond the end of the CSV, the data will
132 // return empty.
133 const msg = `Can not get sample index ${
134 sampleIndex}. This may be beyond the end of the dataset.`;
135 ui.updateStatus(msg);
136 ui.updateSampleRowMessage(msg);
137 ui.updateSampleRowOutput([]);
138 return;
139 }
140 ui.updateStatus(`Done getting sample ${sampleIndex}.`);
141 ui.updateSampleRowMessage(`Done getting sample ${sampleIndex}.`);
142 ui.updateSampleRowOutput(sample[0]);
143};
144
145/** Clears output messages and tables. */
146const resetOutputMessages = () => {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected